Guys who are new to Git should aware of the git stash command as it is the most important command in Git. It is performed to protect all the changes made with the current working directory and to go back to the last commit done on the branch (also known as HEAD).
In this tutorial, we guys will definitely come to know about git stash commands and how to do git stash changes in practical cases. Just make use of the available links and jump into the exact stuff you required to learn about Git stashing.
- Introduction to Git Stash
- What is Stashing?
- Create a Git stash
- Stashing your work
- Stash a specific file
- Stash untracked files
- Specific Git Stash Branch
- Git Stash Changes
- Git stash list
- Git stash apply
- Git stash pop
- Git stash show differences
- Git stash drop
- Git stash clear
- Conclusion
Introduction to Git Stash
In some cases, you need to change the branches however you are operating an unfinished part of your current project. In that situation, you don’t want to do a commit to half-done work. Here, Git stashing permits you to do so. The git stash command allows you to change branches without committing the current branch.
Also Check: Git Commands
The following image shows the properties and role of stashing concerning the repository and working directory.
Usually, the meaning of stash is “store something safely in a hidden place.” The sense in Git is also the same for stash; Git temporarily saves your data safely without committing.
- How To Delete Local and Remote Tags on Git | Git Delete Tag for local & remote
- How To Switch Branch on Git | What is Git Switch? | Git Switch vs Checkout
- How To Create Git Tags | Types of Tags in Git | Creating Git Tags with Examples
What is Stashing?
Stashing takes the messy state of your working directory, and temporarily saves it for further use. Several options are available with git stash. Some of the helpful options are provided here:
- Git stash
- Git stash save
- Git stash list
- Git stash apply
- Git stash changes
- Git stash pop
- Git stash drop
- Git stash clear
- Git stash branch
Stashing changes come with a special set of Git commands intended to create, delete and apply stashes at will.
Do Check: How To Set Upstream Branch on Git
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
Stashing your work
The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. For instance:
$ git status On branch main Changes to be committed: new file: style.css Changes not staged for commit: modified: index.html $ git stash Saved working directory and index state WIP on main: 5002d47 our new homepage HEAD is now at 5002d47 our new homepage $ git status On branch main nothing to commit, working tree clean
Currently, you’re free to make changes, create new commits, switch branches, and perform any other Git operations; then come back and re-apply your stash when you’re ready.
Remark that the stash is local to your Git repository; stashes are not given to the server when you push.
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
Specific Git Stash 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> stash@{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 stash@{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 stash@{0} (8bf64dd0e0045069bf3b3e7d9e34f5e5227aefa7)
As you can see, the stash is dropped at the end of the process, essentially removing it completely from the stash stack.
Git Stash Changes
We can trace the stashes and their changes. To view & understand the changes in the file before stash and after stash operation, execute the below command:
Syntax:
$ git stash show
This command will help you display the file that is stashed and changes made on them. Take a look at the following output:
This result explains that there are two files that are stashed, and performed two insertions on them.
Tracking Git Stash Changes
Also, we can precisely track what changes are performed on the file. To show the changed content of the file, execute the below command:
Syntax:
$ git stash show -p
Here, -p stands for the partial stash. The given syntax will display the edited files and content, view the below output:
The above output is bestowing the file name with changed content. It runs as same as the git diff command and also gives the exact output.
Also, look at the below video tutorial to gain more knowledge on how to git stash changes:
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 stash@{0}: WIP on branch1: 808b598 Initial commit stash@{1}: On branch1: modified README.md stash@{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 a 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 stash@{stash_index} $ git stash apply (shortcut for git stash apply stash@{0})
For example, in order to apply the changes done in the stash with index 1, we would run
$ git stash apply stash@{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 applies command will list your current working directory after applying 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 stash@{0}: WIP on branch1: 808b598 Initial commit stash@{1}: On branch1: modified README.md stash@{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 stash@{stash_index}
Back to our previous stash example, this would give us
$ git stash pop stash@{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 stash@{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 the differences between a stash and the most recent commit, use the “git stash show” command.
$ git stash show stash@{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 stash@{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 a drop or clear.
If you want to drop a specific stash from your stack, use the drop option and specify the stash index.
$ git stash drop stash@{stash_index}
For example, in order to delete stash with an index 1 from the previous example, you would run
$ git stash drop stash@{1} Dropped stash@{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 acquired basic knowledge about git stash such as 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 :
- You can learn how to set the upstream branch on Git;
- If you are just starting out, you can start by cloning repositories into your system.
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!