How To Git Stash Changes


The git stash command is probably one of the most powerful commands in Git.

Git stash is used in order to save all the changes done to the current working directory and to go back to the last commit done on the branch (also called HEAD).

Stashing changes comes with a special set of Git commands designed to create, delete and apply stashes at will.

In this tutorial, we are going to learn about about git stash commands and how they can be used in practical cases.

Create a Git stash

The easiest way to create a git stash is to simply run the “git stash” command without any parameters.

$ git stash

As a consequence, all the changes staged for commit in your current working directory will be saved on the side for later use.

$ git stash
Saved working directory and index state WIP on branch1: 808b598 Initial commit

As you can see, my working directory as well as my index was saved for the “branch1” which is the current name of my branch.

After the colons, you can see the hash of the HEAD commit as well as the commit message : this is the name of your stash.

In this case, no names were assigned to our stash which might not be very handy if you want to pop your stash later on.

Create a Git stash with a name

In order to create a git stash with a name, use the “save” command and specify the name of your stash.

$ git stash save "my_stash_name"

Back to the example we gave before on the branch named “branch1”, we would run

$ git stash save "modified README.md"
Saved working directory and index state On branch1: modified README.md

Now, Git did not provide a default name (made by the last HEAD commit message) but it assigned a custom name to it.

Alternatively, you can use the “git stash push” command in order to create a stash with a name.

$ git stash push -m "modified the README.md" again
Saved working directory and index state On branch1: modified again the READ.me

Stash a specific file

Using the previous commands, you have stashed all the tracked files in your current working directory.

In some cases, you may want to stash a specific file in order to retrieve it later on.

To stash a specific file, use the “git stash push” command and specify the file you want to stash.

$ git stash push -m "message" <file>

For example, in order to stash the “README.md” file in our current working directory but keep changes done to the other files, we would run

$ git stash push -m "modified the README.md" README.md
Saved working directory and index state On branch1: modified README.md

However, the other tracked files that may be modified in your current working directory are untouched.

Stash untracked files

As you probably noticed before when creating stashes, stash only saves tracked files of your working directory by default.

But what if you wanted to stash untracked files of your current working directory?

In order to stash untracked files, add the “–include-untracked” option to your “git stash” initial command.

Alternatively, you can simply use the “-u” which is equivalent to the untracked longer version.

$ git stash --include-untracked
Saved working directory and index state WIP on branch1: 808b598 Initial commit

$ git stash -u

Stash in a specific branch

In some cases, you may want to stash your current changes into a specific branch.

Let’s say for example that you worked on the “master” branch for modifications, but you decide that your work may need a specific branch for integration.

This can be done with the “git stash branch” command.

$ git stash branch <branch_name>

$ git stash branch <branch_name> [email protected]{stash_index}

Let’s say for example that you want to stash your current changes in a branch named “branch1”, you would execute.

$ git stash branch branch1 [email protected]{0}

Switched to a new branch 'branch1'
On branch branch5
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
Dropped [email protected]{0} (8bf64dd0e0045069bf3b3e7d9e34f5e5227aefa7)

As you can see, the stash is dropped at the end of the process, essentially removing it completely from the stash stack.

List Git stashes

Now that you have created some stashes, it is time for you to list the stashes that you just created.

In order to list Git stashes, use the “git stash list” command.

$ git stash list

[email protected]{0}: WIP on branch1: 808b598 Initial commit
[email protected]{1}: On branch1: modified README.md
[email protected]{2}: On branch1: modified again the READ.me

As you can see, stashes are given an index starting at zero.

When creating new stashes, items are added to the stack meaning that the most recent stash has the index 0 while the oldest stash is at the bottom of the stack.

This is closely related to the concept of stack in software engineering. A link to an in-depth article is included if you are curious about stacks.

Apply Git stashes

Now that you have saved your Git stashes on the side, you might want to “take them out from the stack” and apply them to your current working directory.

In order to apply your Git stash to your current working directory, use the “git stash apply” command and specify the stash you want to apply.

If you don’t specify any arguments to the apply command, the top of the stack will be applied.

$ git stash apply [email protected]{stash_index}

$ git stash apply (shortcut for git stash apply [email protected]{0})

For example, in order to apply the changes done in the stash with the index 1, we would run

$ git stash apply [email protected]{1}

Already up to date!
On branch branch1
Your branch is up to date with 'origin/branch1'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        file

By default, the stash apply command will list your current working directory after apply the corresponding stashes.

Now if you were to list your stashes, you would notice that applying your stash did not delete or remove the stash from the list.

$ git stash list

[email protected]{0}: WIP on branch1: 808b598 Initial commit
[email protected]{1}: On branch1: modified README.md
[email protected]{2}: On branch1: modified again the READ.me

If you want your stashes to be removed after applying them, you need to use the “git stash pop” command.

Pop Git stashes

So what is the difference between git stash pop and git stash apply?

The main difference is in the fact that the “git stash pop” applies your changes to your current working directory but it also deletes the stash from the stash stack.

To pop Git stashes, simply use the “git stash pop” command and specify the stash index you want to pop.

$ git stash pop [email protected]{stash_index}

Back to our previous stash example, this would give us

$ git stash pop [email protected]{1}

Already up to date!
On branch branch1
Your branch is up to date with 'origin/branch1'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        file

Dropped [email protected]{1} (1adaf79224dca78aa6568b1e8154cbc4f747042f)

See the difference in the last line of the example?

The stash was dropped and removed from the stack.

Show Git stash differences

When you create a stash, it is most likely to perform some commits before going back to your stashed content.

As a consequence, you may want to see the differences between your stash and the most recent commit of your branch (also called HEAD)

To show differences between a stash and the most recent commit, use the “git stash show” command.

$ git stash show [email protected]{stash_index}

README.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

As a consequence, you will be listed with the differences between files, the insertions and the deletions done.

To see all the differences including content, add the “-p” option.

$ git stash show -p [email protected]{stash_index}

diff --git a/README.md b/README.md
index f25b874..1088f9a 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,3 @@
-# private-repo
\ No newline at end of file
+The file was modified!

Drop Git stashes

In some cases, you may want to delete a Git stash entry from your stack.

In order to drop stashes, you have two options : with drop or clear.

If you want to drop a specify stash from your stack, use the drop option and specify the stash index.

$ git stash drop [email protected]{stash_index}

For example, in order to delete stash with an index 1 from the previous example, you would run

$ git stash drop [email protected]{1}
Dropped [email protected]{1} (c11c6ae6c347d23dff8fbbf79d54a9e6e2e79b1c)

Drop all stashes using clear

If you want to delete all the Git stashes in your stack, you have to use the clear command.

$ git stash clear

Make sure that all your stashes were deleted with the list command.

$ git stash list

Conclusion

In this tutorial, you learnt about git stash : how you can create stashes, delete stashes, and pop them in order to recover your work.

Git stash is pretty useful, but there are many other commands that you may find useful when using Git :

If you are interested in software engineering, we have a complete section dedicated to it on the website, so make sure to check it out!


3 responses to “How To Git Stash Changes”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.