Understanding Git Stashes
Git stashes allow you to temporarily shelve (or save) changes you’ve made to your working directory so you can work on something else, and then easily re-apply those changes later. This is incredibly useful when you need to switch branches quickly without committing half-finished work, or when you encounter an urgent bug fix that requires a clean working directory.
Creating a Stash
By default, Git stashes are created without any specific names or identifying information beyond a timestamp and a generic message. While this works, it can become difficult to manage multiple stashes, especially when you need to retrieve a specific one.
Saving with a Message
You can add a descriptive message when creating a stash using the git stash push
command with the -m
flag:
git stash push -m "my_stash_name"
This creates a stash and assigns the message "my_stash_name" to it, making it easier to identify later. The older git stash save
command is deprecated, and git stash push -m
is the recommended approach.
Listing Your Stashes
To view a list of your saved stashes, use the git stash list
command:
git stash list
This will output a list similar to:
stash@{0}: On develop: perf-spike
stash@{1}: On develop: node v10
Each stash is identified by an index (e.g., stash@{0}
, stash@{1}
) and a message (or default message if none was provided when the stash was created). The message indicates the branch the stash was created on and provides a brief description.
Retrieving a Stash
There are several ways to re-apply a stashed changes:
git stash apply stash@{n}
: Applies the stash at indexn
to your working directory. The stash remains in the stash list after applying.git stash pop stash@{n}
: Applies the stash at indexn
and removes it from the stash list. This is a convenient shortcut if you know you won’t need the stash again.
Applying a Stash by Name
While Git doesn’t directly support applying a stash by name, you can achieve this using a combination of commands. This is particularly useful when you have many stashes and remembering the index is difficult.
git stash apply stash^{/my_stash_name}
This command searches for a stash with the name "my_stash_name" and applies it. Note the ^
character and the forward slash /
before the name. This syntax tells Git to search for the stash by its message.
Alternatively, you can use a more verbose, but potentially more reliable, approach that uses grep
to filter the output of git stash list
:
git stash apply $(git stash list --pretty='%gd %s' | grep "my_stash_name" | head -1 | awk '{print $1}')
This command first lists all stashes with their descriptions, filters the list to find the stash containing "my_stash_name," takes the first matching result, and extracts the stash index. It then applies the stash using git stash apply
.
Advanced Stash Management
-
Creating a Branch from a Stash: If you’ve made significant changes and want to work on them in isolation, you can create a new branch from a stash:
git stash branch <branchname>
This creates and checks out a new branch, applies the latest stash to it, and removes the stash from the stash list.
-
Patch Files: As an alternative to stashes, you can create and apply patch files using
git diff
andgit apply
. This can be useful for sharing changes or applying them to different repositories.# Save your working copy changes git diff > some.patch # Re-apply it later git apply some.patch