Git Stash
There are many cases where a clean working copy is needed: when checking out a different branch, when merging branches, or just when pulling from a remote.
The git stash
command can help you stash (put aside) changes you've made to your working directory and the index,
so you can come back after and re-apply them.
How Stashing work
The git stash
command selects all the uncommitted changes from the working directory and the staging area and saves them from later use.
To explain how stashing works, we can say that we have a couple of local modifications:
$ git status
modified: file1.txt
modified: file2.txt
If you have to switch branches to work on another feature or fix a bug on prod, you need to put these changes away. If it's a work in progress, you shouldn't just commit them.
This is where git stash
can be useful:
$ git stash
Saved working directory and index state WIP on master: 6b5548f first commit
So now the working copy is clean. All the uncommitted changes have been put away in a safe place, and we can bring the changes back when we need them.
$ git status
On branch master
nothing to commit, working tree clean
Saving stashes with a message: git stash save
Git stash gives the possibility to stash changes of all the uncommitted changes with a message.
The command to stash with a message run as follows:
$ git stash save "a stashing message"
Saved working directory and index state On master: a stashing message
View the stored stashes: git stash list
To view stashes, we can use the command git stash list
as follows:
$ git stash list
stash@{0}: On master: a stashing message
stash@{1}: WIP on master: 6b5548f first commit
The output of the command above shows all the stashes with their indexes (stash@{0}, stash@{1}).
Note: the stash with the index {0} is the recent stash, so it's a LIFO stack (Last In First Out). So the stash in Git uses a LIFO stack to save all the stashes.
Applying the stored stashes: git stash apply
To re-apply the changes from stash, Git offers the git stash apply
command.
The command git stash apply
restores the last stash and re-apply it.
The command run as follows:
$ git stash apply
modified: file1.txt
If you want to apply a specific stash, you can follow the command git stash apply
by the specific stash index id.
The command will be used as follows :
$ git stash apply stash@{1}
Viewing stash diffs: git stash show
Git stash offers a show option to see the diff between the stashed content, and the commit back when the stash entry was first created. The command will run as follows :
$ git stash show
file1.txt | 2 ++
1 file changed, 2 insertions(+)
If you want to see the full diff of a stash, the -p
will come in handy. It runs as follows:
The command to view the second most recent entry in the Stash stack will run as follows:
$ git stash show -p stash@{1}
Re-applying your stashed changes: git stash pop
The command git stash pop
removes the last stash from the stash list and applies the changes to the working directory.
The command to reapply the last stashed changes run as follows:
$ git stash pop
If some conflicts occur when applying to the last stashed changes, the concerned stash will not be removed from the stash list.
Removing a Stash: git stash drop
The git stash drop
command deletes the most recent stash. it used as follows:
$ git stash drop
If you want to delete a particular stash from the available stashes, pass the stash ID to the stash drop command.
The command to remove a particular stash will run as follows:
$ git stash drop stash@{1}
Removing all stashes: git stash clear
The git stash clear
will delete all the stashes at once. The command run as follows:
$ git stash clear
Creating a branch from a stash: git stash branch
When changes in your current branch diverge from the changes from your stash, and you are afraid to run into conflicts when applying or popping your stash. Git gives you the possibility to create a new branch from your stash.
The command to create a new branch from the stash with id one run as follows:
$ git stash branch name_of_your_new_branch stash@{1}
Switched to a new branch 'name_of_your_new_branch '
On branch new_branch
modified: file1.txt