Introduction to Git Stashing
Git stashing is a powerful feature that allows you to temporarily shelve (or stash) changes you’ve made to your working directory so you can work on something else, and then re-apply those changes later. This is incredibly useful when you need to quickly switch branches, pull in updates, or address a high-priority bug without committing incomplete work. This tutorial will guide you through the process of stashing changes and, importantly, how to recover those stashed changes.
Why Use Stashing?
Imagine you’re in the middle of developing a feature, but a critical bug is reported on the main branch. You don’t want to commit your half-finished work, but you need to fix the bug. Stashing allows you to:
- Save your current, incomplete changes.
- Switch to the main branch.
- Fix the bug and commit.
- Switch back to your feature branch.
- Re-apply your stashed changes and continue working.
Stashing Changes
The basic command to stash your changes is:
git stash
This command takes the modified tracked files and staged changes and saves them to a temporary stack. Your working directory reverts to the last committed state. Git will output a message confirming the stash creation, including a stash identifier like stash@{0}
.
You can also include untracked files with the -u
or --include-untracked
option:
git stash -u
This is helpful if you’ve created new files that haven’t been added to Git yet. To include ignored files as well, use the -a
or --all
option.
Listing Your Stashes
Over time, you might create multiple stashes. To see a list of your stashed changes, use:
git stash list
This will display a list of stashes, each identified by a stash@{n}
identifier (where n
is a number starting from 0). The list also includes a brief description of the changes in each stash.
Applying Stashed Changes
To re-apply the most recent stash (the one at the top of the list – stash@{0}
), use:
git stash apply
This will merge the changes from the stash into your current working directory. If there are conflicts, you’ll need to resolve them as you would with any other merge operation.
To apply a specific stash (e.g., the third stash in the list – stash@{2}
), use:
git stash apply stash@{2}
Preserving Index (Staging Area)
Sometimes, you want to re-apply the stash and also preserve the staging area (the index). This means that files that were staged before you created the stash will remain staged after applying the stash. To do this, use the --index
option:
git stash apply --index
This is particularly useful when you want to continue working on a feature with the same staged changes.
Popping a Stash
The git stash pop
command is a shortcut that combines git stash apply
and git stash drop
. It applies the most recent stash and then removes it from the stash stack.
git stash pop
Like git stash apply
, you can also specify a particular stash to pop:
git stash pop stash@{1}
Dropping a Stash
If you no longer need a stash, you can remove it from the stash stack using git stash drop
:
git stash drop stash@{0}
This will permanently delete the specified stash.
Clearing All Stashes
To remove all stashes from the stack, you can use:
git stash clear
Important: This action is irreversible, so be sure you no longer need the stashed changes before running this command.