Git stash command

Git stash command

Author: Muhammad Khabbab

What is Git stash

Stash is a Git command to locally store your recent changes in a separate area so you can fetch those changes later. After taking a snapshot of your local files, it resets the state of your workspace to the previous commit state. You can save multiple stashes on your local computer and you can apply back any of the stashes at a later stage.

Stash is very simple to use. You can run git stash to save your changes and then use git stash apply or git stash pop to bring back those saved changes.
Note that git stash is only applied to locally versioned files, and developers using the same git repository will not be aware of these stash changes.

In this article, we will cover Git stash in detail. We will start with why it is needed, how to use it, what are its best practices and some example commands of Git stash.

Why do we need Git stash

Some of the use cases where you will need Git stash are the following:

Switching between branches

If you have two branches, let’s say branch A and branch B. A lot of work has been added to both branches, and they have diverged from each other quite a lot. Both have different heads.

If you are working on branch A and you want to make some changes to branch B, then you will have no other choice except to create a commit at that point in branch A, then commit the code and push your changes so that you can fix the bug in branch B. After fixing the bug in branch B, you will switch your active branch to branch A and then run git reset head… to get your changes back.

This is not a very flexible approach because your unfinished work will be considered a checkpoint instead of a patch that is still a work in progress. This is where Git stash comes in. With Git stash, you can save your changes locally without making a commit, switch branches, perform other operations, and then reapply the locally stashed changes when you need them.

You can transition between different tasks without needing to undo or redo the changes.

Pull latest remote changes

If more than one team member is working on the same code file and you want to fetch the changes made by other developers, then you will use git pull which will merge other developers' changes into the file.

However, that will only happen if their changes do not conflict with yours. If the incoming modifications are on the same lines you have changed, then using git pull will likely throw an error related to merging error. Git stash comes to the rescue here.

You can easily stash your local changes, pull the modifications made by other developers in the same file, and then you can apply your locally stashed changes to the same file. You might still have to resolve the conflicts manually, though.

How to use Git stash

Continuing the previous example of branch A and branch B, here is how you will use Git stash to switch between branches while saving your changes locally without making a commit.

  1. Save changes to branch A which is your working branch.
  2. Execute command git stash. It will preserve your changes locally in the stack.
  3. Check out branch B.
  4. Fix the bug in branch B.
  5. Commit the changes and if you want to push to remote, then do that as well.
  6. Switch back to branch A using checkout command
  7. Run git stash pop to retrieve your stashed changes back.

Your changes preserved through git stash are saved in your project’s .git directory, usually, the path is /.git/refs/stash. It is the fastest way to get your working directory clean while still keeping the changes.


Building a side project?

Meet the headless, React-based solution to build sleek CRUD applications. refine expertly combines cutting-edge technologies under a robust architecture, so you don’t have to spend time researching and evaluating solutions.

Try refine to rapidly build your next CRUD project, whether it's an admin panel, dashboard, internal tool or storefront.



Git stash vs commit

Both git commit and git stash take a snapshot of changed files in the git working tree and save that snapshot for later use. However, both are very different from each other in many aspects, such as:

• Commit is a part of the public git history so others can see it. Stash is always local. • The Stash command resets the files in the working tree to a previous commit point. A new commit leaves files in the working tree unmodified. • Commit creates a new save point on your branch, whereas stash reverts to a previous save point.

Git stash vs reset

The git reset command is similar to git stash in the sense that both commands revert the files in the working directory to a previous commit. However, there are many differences, such as:

• A hard reset will wipe out all the changes. A stash will save a snapshot of modified files on the local machine. • Stash does not create a new save point in the branch history because it is only to your local machine. A reset will create a new commit point in the branch history. • As the reset is based on the overall remote repository so you can use reset to jump back to any previous commit. A stash will only reset the files in your local workspace to a previous state.

Git stash vs stage

The command git stage might appear to be the same as git stash because it also stages your changes; however, that is not the case. The git stage is the step prior to the commit. You add new files to the “staged files” so that the next commit contains these files. Stash will move your files back to the stack so that you can bring these files back and see the modifications.

Create a Git stash with a name

Although it is not mandatory to name a stash, you can do that through git stash push -m "stashName" followed by git stash apply stash^{/stashName} . You can also use git stash save stashName however, the save command is deprecated. Note that a simple git stash would work perfectly fine as well.

Image 1

By default, the stash command stores uncommitted files and code while leaving any untracked or ignored files.

Stash untracked files

If you want to stash all your untracked files in the stash stack, then execute the command git stash --include-untracked or the short version git stash -u.

Image 2

Create branch from stash

It is very easy to create a new branch from your latest stash. If you run git stash branch <branchName>, it will create a new branch from the latest stash. You can use the command git stash branch <branch_name> stash@{revision} to generate a new branch from a specific stash.

Image 3

List stashes

Just execute the command git stash list to see the list of current stashes.

Image 4

Apply Git stash

You can apply the git stash by running the command git stash apply stash^{/MyStash1}.

Image 5

Pop Git stash

Note that git stash pop removes the stash after applying it. git stash apply leaves it in the stash list for later use. The below screenshot will remove the stash after applying it.

Image 6


Best Practices of using Git stash

Here are some of the best practices you should follow when using Git stash:

  • Git stash should be used only on a need basis; its scope is limited to local repository history. Any code part of the stash is not visible to other developers in your team.
  • Try to create temporary feature or development branches in your Git working tree and regularly commit to these branches instead of using stash.
  • Use stash only when you need the modified files temporarily stored on your machine for restoration later.

Conclusion

Git stash is a convenient command by Git. If used in the right situations, it can be very beneficial. If you switch between your branches frequently and need to safely stage your latest work for a short time, Git stash is for you. Too much usage of the stash is also discouraged as well. If you follow the best practices mentioned above, Git stash will improve your efficiency and result in increased productivity.