Git Stash Like a Pro: Tips for Every Developer
Learn How to Save Time and Keep Your Commits Clean
If you've spent any time working with Git, chances are you've come across the git-stash
command. This handy tool lets you temporarily save your local changes, storing them in a stack of unfinished work. It's perfect for those moments when you need to switch gears to another branch but don't want to lose track of what you're currently working on.
In this quick guide, we'll dive into both basic and advanced git-stash
techniques that can streamline your workflow. From listing your stashes without applying them to selectively unstashing specific files, we've got you covered.
Ready to master git-stash
? Let's dive in!
Use Cases
1. Create a stash
To save your modifications into a stash, type:
$ git stash
What about the newly created (untracked) files? They remain in the working tree. If you want to stash them as well, add the — all
option:
$ git stash --all
2. List all stashes
$ git stash liststash@{0}: On master: demo_master2
stash@{1}: On test: demo_test
stash@{2}: On master: demo_master
In the example above, I have modified files in two branches named test
and master
.
3. Apply a stash
Apply the latest stash:
$ git stash apply
To apply a specific stash, you have to provide the stash ID:
$ git stash apply stash@{0}
This will apply the stash with the ID 0
to the working tree without deleting it from the stash list.
4. Pop a stash
If you want to apply the changes and delete the stash from the stash list, use git-stash pop
:
$ git stash pop stash@{0}
Note that without providing a specific stash ID, it would delete the topmost stash from the list:
$ git stash pop
5. Show changes without applying them
Sometimes we just want to see what’s changed in the code without re-applying it. The show
command does that for us.
To show changes in the last stash:
$ git stash show
It would print something like this:
.../main/java/jpa/AddressJpaController.java
| 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
To view the content of the most recent stash:
$ git stash show -p
The result would be something like this:
Note that I’ve shortened the output for the sake of brevity.
For me, it’s more useful to see the modifications in a particular stash. I just have to provide the desired stash ID:
$ git stash show -p stash@{1}
Example output:
You’ll be able to revise the changes in your terminal.
6. Create a new branch from stash
Sometimes you can run into conflicts when you try to apply a stash because the branch on which you ran the stash has significantly changed. The git stash apply
operation would fail.
In this case, you can avoid conflicts by creating a new branch with the desired stash changes. The git stash branch
command restores the originally stashed state without conflicts:
$ git stash branch new_branch stash@{2}
Note that this command will delete the stash from the list, just like the git stash pop
command.
If you don’t provide a stash ID, it applies the latest one from the stash list.
7. Show modified file names
To see which files have changed add the — name-only
option to git stash show
:
$ git stash show --name-onlymyproject/src/main/java/AddressJpaController.java
8. Apply stashed changes from a particular file
Suppose that you’ve made changes to several files and stashed them. Later on, you would like to re-apply the changes, but only from a certain file/files.
One way to go is to apply the whole stash, commit the required files, and unstash the others. But there is a more efficient way to do that. You can use the restore
command (available since Git 2.23+) and provide the file name:
$ git restore -s stash@{0} -- <filename>
You’ve already learned how to get the names of the modified files. So, let’s try it:
$ git stash show --name-only stash@{1}myproject/src/main/java/jpa/AgeController.java
myproject/src/main/java/jpa/AddressJpaController.java
Unstash a specific file:
$ git restore -s stash@{1} -- src/main/java/jpa/AgeController.java
9. Delete all stash entries
If you want to clean up your stashes, use the clear
command. Note that this operation may be unrecoverable.
$ git stash clear
10. Delete a single stash entry
The drop
command deletes the latest stash from the stack:
$ git stash drop
You can also provide an ID to delete a specific stash:
$ git stash drop stash@{2}
Again, proceed with caution, as it might be impossible to recover the stash.
Conclusion
In conclusion, the git-stash
command is a powerful tool for keeping your code organized and your commit history tidy. Understanding how to use it effectively in different scenarios can significantly enhance your workflow.
I hope this guide has provided you with some valuable insights. Thanks for reading, and I look forward to sharing more tips with you next time!