Git stash
Last updated
Last updated
Git has an area called the stash
where you can temporarily store a snapshot of your changes without committing them to the repository. It’s separate from the working directory, the staging area, or the repository.
This functionality is useful when you’ve made changes to a branch that you aren’t ready to commit, but you need to switch to another branch.
To save your changes in the stash, run the command:
This saves your changes and reverts the working directory to what it looked like for the latest commit. Stashed changes are available from any branch in that repository.
To see what is in your stash, run the command:
This returns a list of your saved snapshots in the format stash@{0}: BRANCH-STASHED-CHANGES-ARE-FOR: MESSAGE
. The stash@{0}
part is the name of the stash, and the number in the curly braces ({ }
) is the index of that stash. If you have multiple change sets stashed, each one will have a different index.
If you forgot what changes were made in the stash, you can see a summary of them with git stash show NAME-OF-STASH
. If you want to see the typical diff-style patch layout (with the +‘s and -‘s for line-by-line changes), you can include the -p
(for patch) option. Here’s an example:
To retrieve changes out of the stash and apply them to the current branch you’re on, you have two options:
git stash apply STASH-NAME
applies the changes and leaves a copy in the stash
git stash pop STASH-NAME
applies the changes and removes the files from the stash
There may be conflicts when you apply changes. You can resolve the conflicts similar to a merge (see git merge
for details).
If you want to remove stashed changes without applying them, run the command:
To clear the entire stash, run the command: