git stash command: save, drop, pop, cancel, list, apply
The topic of this article is the git stash
command. It's a handy command that you'll use more than you think, even more than git add and git commit, so learn it to increase your development productivity.
1. What is git stash?
The git stash
command temporarily stores the changes you're working on.
This is useful because there are some commands that can only be executed when the working tree is cleaned up by committing the current change, such as a branch change.
In these cases, git stash
can be used to temporarily save the state of your work, and then you can process a series of commands and come back to continue working.
When we issue the git stash
command, Git collects all the currently unstaged and staged changes (index) and stores them in a new stash object.
It then temporarily stores them in a separate location called stash
.
When stash is run, the current branch is in the last committed state. In this state you are free to use almost any Git command.
Let's take a look at the different ways to use the git stash
command with some real world examples.
2. Creating a new stash using the push command
The push
command is the beginning of the git stash
: it saves the current changes, and if you want, you can also enter a separate message to distinguish this stash.
We used to use the git stash save
command, but there is a new git stash push
command that adds a lot of functionality.
So now we will use the push
command.
The main feature added by push
is the ability to run stash on untracked files, all files, and a specific file or directory.
Use it like this
$ git stash push -m [message]
Let's see the results of the run
Let's start from the top. The git switch
command was aborted because we added a new change to the 1.md
file.
Git advises us to commit or stash if we want to switch.
I stash with the git stash
command, which clears the working tree and allows me to do the git switch
command.
The push
command can be used to stash any file, including untracked files with the -u
option and ignored files with the -a
option.
You can also specify a specific file or directory name to push, as shown below.
Unlike the example above, we don't specify a message with the -m
option, so you can see that Git generated a message with a commit ID for us.
3. Getting the stash list using the list command
As we briefly saw in the previous example, the list
command will show you a list of your saved stashes.
It will list all stashes created in all branches, with the more recent ones numbered closer to 0
.
The command can be used like this
$ git stash list
Let's create two more stashes, one in the main
branch and one in the develop
branch, and then run the list
command.
The git stash list
command shows a total of three stash lists.
Since we saved 2 stashes from the develop
branch without any messages, it's impossible to tell what the changes were by looking at the list.
Don't forget the -m
option of the push
command for cases like this.
4. Viewing stash contents with the show -p command
If you only see a stash list, like in the example we just saw, you may not know what changes are stored in each stash.
This is where the show -p
command comes in handy.
The show
command simply tells you the filenames of the changes stored in the stash and the number of lines modified.
With the -p
option, you can run the git diff
command to get a more detailed view of the changes inside the files.
The command looks like this If you don't enter [stash-number]
, it will automatically run against the most recent stash, number 0
.
$ git stash show -p [stash-number]
To see the results of the execution, add new content to the 2.md
file and issue the push
command.
Now issue the show
command.
As shown above, the output will be different depending on whether you have the -p
option or not.
5. Restoring your stash with the apply command
Once you've saved your stash with the push
command and you're done, you need to do something to undo your changes.
You can use the apply
and pop
commands to do this.
Of these, the apply
command ensures that the saved stash is not deleted after the stash is restored.
The command looks like this If you don't specify [stash-number]
, it will automatically run against the most recent stash, number 0.
$ git stash apply [stash-number]
Let's show the result of the execution
After saving the stash with the for stash
message, we imported it with the apply
command. The apply
command runs the git status
command, which shows the current status of the working copy.
to show the current status of the working tree.
If you check the stash list after apply
, you can see that the restored stash is still there.
6. Deleting the stash after recovery with the pop
command
Like the apply
command, the pop
command is also used to undo cached changes, and it deletes the existing stash after recovery.
The command is as follows, and again, if you don't specify [stash-number]
, it will automatically be run against the most recent stash, number 0
.
$ git stash drop [stash-number]
When executed, it looks like this
Once again we pushed a stash with the message for stash. We then used the pop
command to revert the changes and got the same output as when we used the apply
command.
The only difference is that the stash has been deleted and no longer appears in the stash list.
7. Deleting a stash without applying it with the drop
or clear
command
The drop
command deletes the changes in a specific stash without applying them to the working tree.
It is used with the following command, which automatically targets the most recent stash, number 0
, unless [stash-number]
is specified.
$ git stash drop [stash-number]
Let's see the result of the pop
command.
You can see that the stash in the for stash
message, which was number 0, has been removed.
If you no longer need all the stashes in the stash list and want to delete them, you can do so with the clear
command.
The command looks like this
$ git stash clear
You can just run it like this
After executing the clear
command, all stash listings have been removed.
8. How to move current changes to a new branch using the stash branch command
Sometimes you need to move all the changes you have been working on in the current branch to a new branch and work on them there.
This can be done with a combination of the git add command and the git checkout/git switch command.
However, the git stash branch
command makes this task possible with a single command.
Use the command below, and if you don't specify [stash-number]
it will automatically run against the most recent stash, number 0
.
$ git stash branch [new-branch-name] [stash-number]
And you can run it like this
9. Closing thoughts
Since many Git commands are executed in a committed state, the git stash
command is one of the commands that I reach for every day.
I hope this post helps you improve your daily development productivity.
