Tags: git
git stash is a powerful and nifty feature in Git that allows you to temporarily set aside changes you've made to your working directory. This can be incredibly useful when you need to switch branches but aren't ready to commit your changes. Here’s a comprehensive guide on how to use git stash effectively.
Imagine you are working on a branch and you need to check out another branch. However, you have uncommitted changes that you don't want to lose. This is where git stash comes into play.
To stash your changes, simply run:
$ git stash
This command will save your modified tracked files and staged changes in a new stash and revert your working directory to match the HEAD commit.
When you're ready to retrieve your stashed changes, you have a couple of options:
This command will apply the most recent stash and then remove it from the stash list.
$ git stash pop
If there are conflicts, you’ll need to resolve them manually before proceeding.
If you prefer to apply the stash but keep it in the stash list (perhaps you want to keep it for later use), use git stash apply:
$ git stash apply
After resolving any conflicts, you can remove the stash entry with:
$ git stash drop
If you only want to stash changes to a specific file or set of files, you can use the push option with the file path:
$ git stash push path/to/your/file
This will stash only the specified file while leaving other changes in your working directory.
You can view a list of all stashes you have saved with:
$ git stash list
This will show you a list of all stash entries, each with an index and a description.
To apply a specific stash from the list, use its index:
$ git stash apply stash@{index}
For example:
$ git stash apply stash@{1}
To drop a specific stash from the list, again use its index:
$ git stash drop stash@{index}
By default, git stash only stashes tracked files. If you want to stash untracked files as well, use the -u option:
$ git stash -u
You can add a custom message to your stash entry to make it easier to identify later:
$ git stash save "Testing my stash"
You're working on feature-branch and need to switch to main to review a pull request, but you have uncommitted changes:
$ git stash
$ git checkout main
After reviewing, you can return to your branch and reapply your changes:
$ git checkout feature-branch
$ git stash pop
You're working on multiple files but only want to stash changes to file1.txt:
$ git stash push path/to/file1.txt
The rest of your working directory remains unchanged.
To handle conflicts better, use git stash
apply followed by git stash drop
:
$ git stash apply
# Resolve conflicts
$ git stash drop
Sometimes, you might need to stash changes multiple times and later apply a specific stash. Here’s how you can manage multiple stashes:
$ git stash save "WIP: Implementing feature A"
$ git stash save "WIP: Fixing bug B"
$ git stash save "WIP: Refactoring module C"
To list all your stashes:
$ git stash list
This might output something like:
stash@{0}: WIP: Refactoring module C
stash@{1}: WIP: Fixing bug B
stash@{2}: WIP: Implementing feature A
To apply a specific stash, for example, the one for fixing bug B:
$ git stash apply stash@{1}
After resolving any conflicts, you can drop the applied stash:
$ git stash drop stash@{1}
git stash
is an essential tool for any developer using Git. It allows you to temporarily shelve your changes, giving you the flexibility to switch contexts without losing your progress.