No matter how much preparation goes before you start typing the first line of code on a project, the pathway to getting the result you desire is never straightforward. Even the small projects will require detours and might need you to create and switch branches on Git not to break the main project. Another key functionality that Git gives developers is the ability to stash changes; this article is an overview of what stashing is and how to stash changes in Git.
What is Git Stash?
Git stash is a command in Git that lets you set aside changes on a particular branch while switching to another without losing your work on that branch.
You should use Git stash to save work in progress that you are not yet ready to commit, but you will need later. Adding changes to Git stash takes them away from the flow of the working directory.
Stash Changes In Git
For the sake of this tutorial, I will be using a working directory named git-stash located on my desktop; it contains two files, text1.txt and text2.txt. Both files are empty text files with initial commits on the main branch and are tracked by git. The commands to achieve the above result are as follows:
$ cd C:/Users/Clouddevelop/Desktop
$ mkdir git-stash
$ cd git-stash/
$ touch text1.txt text2.txt
$ git init
$ git add .
$ git commit -m "Initial commit"
To try out the stash functionality, I will make some changes to the working directory:
- Type a short line of text in the text1.txt file and save it.
- Create a new file called text3.txt by using the
touch text3.txt
command.
If you use the git status command, you will get a response notifying you of the unstaged changes and the untracked file in the working directory.
$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: text1.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
text3.txt
no changes added to commit (use "git add" and/or "git commit -a")
To stash the changes you have made, you need to type the simple command git stash in the working directory. You will get the following success message:
Saved working directory and index state WIP on main: b3a74fb Initial commit
You can also add a stash message by adding the push -m <stash message>
; for instance:
git stash push -m "this is a sample stash"
Stashing Untracked Files
If you rerun the git status
command in the working directory, the message you will get is this:
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
text3.txt
nothing added to commit but untracked files present (use "git add" to track)
You would observe from the message above that the text3.txt file wasn’t a part of the stash like the modified text1.txt. It wasn’t a part of the stash because it is an untracked file, and by default, the git stash
command only works on tracked files.
To stash an untracked file, you will need to add the -u
flag to the git stash
command; like this:
git stash -u
To check the record of the stashes you have made in the working directory, you should use the git stash list
command. In this instance, the response should be:
stash@{0}: WIP on main: b3a74fb Initial commit
stash@{1}: WIP on main: b3a74fb Initial commit
On each list item, the stash numbering is 0-indexed, and the ‘WIP’ stands for ‘work in progress’; you will also see the commit it is related to along with its commit message. If you look in your working directory, you will find that the text3.txt file no longer exists, and the text1.txt file is empty again.
Applying The Stashed Changes
When you finish whatever activity was the reason behind you stashing your changes, you might want to apply the changes from the stash. To apply stashed changes, you should run the git stash apply
command and put the index number of the stash you want to apply. In our case, it will be as follows:
$ git stash apply 0
$ git stash apply 1
The last stash will automatically apply to the branch if you run the git stash
command without an index number.
Alternatively, you can use the git stash pop
command with the index number in front. The git stash pop
command applies the specified stash and deletes it from the list of stashes; if you don’t give an index number, the last stash is applied and deleted.
Evaluate And Compare Stashes
If you want to look at what is inside a particular stash, you can use the git stash show <index number>
command. The above command will indicate the changes made in the command line; however, if you want to see the changes in the command line, you should use the git stash show -p <index number>
.
Create Branch From Stash
When you use git apply
, you are applying the stash to its default branch; however, you might want to move the changes you stashed to a different branch to continue development. To create a new branch from a stash, you will use the following command git stash branch <new branch name> <stash index you want to create a branch from>
. Here’s an example:
git stash branch new-branch 0
Delete Stashes
To delete a particular stash in your working directory, you will use the git stash drop <stash index>
command; for example:
git stash drop 0
If you want to clear all the stashes you have saved in the working directory; you should use the git stash clear
command.
Read More: How To Switch Branches In Git