Git Stash: How to Temporarily Save Changes

Learn how to temporarily save changes with Git stash and apply them later to ensure a more efficient and effective development workflow.
Published 2023-04-12 8 min read
Git Stash: How to Temporarily Save Changes

Git Stash is a feature in the Git version control system that allows you to temporarily save changes in your working directory that you don’t want to commit yet. Think of it as a temporary shelf to store your work-in-progress changes, allowing you to switch to another branch or task without losing your current progress.

Sometimes you might find yourself in the middle of working on a feature or bugfix, and you need to switch to another branch to fix a critical issue or review a colleague’s code. Git Stash comes in handy by allowing you to save your uncommitted changes, switch to another branch, and then come back and reapply those changes when you’re ready to continue.

Typical use cases for Git Stash include:

  • Switching between branches while working on a feature
  • Saving your work before pulling the latest changes from the remote repository
  • Keeping partially implemented ideas without committing them to the repository

 

Getting Started with Git Stash

Before diving into Git Stash, make sure you have Git installed on your computer and have basic knowledge of Git commands such as git add, git commit, and git checkout.

Here are the basic Git Stash commands you should know:

git stash save: Temporarily save your changes to a new stash, btw. git stash with no other parameters will do just that.

git stash list: Show a list of all your stashes

git stash apply: Apply the changes from a stash to your working directory

git stash drop: Remove a stash after you’ve applied it or no longer need it

git stash pop: Apply and then remove a stash (combines git stash apply and git stash drop)

Digging Deeper into Git Stash

Stashing changes with a custom message

When you create a stash, it’s a good idea to add a custom message to describe the changes you’re saving. This makes it easier to remember what’s in each stash. To create a stash with a custom message, use the following command:

git stash save "Your custom message here"

Stashing specific files or changes

If you want to stash changes only in specific files, use the -- delimiter followed by the file paths:

git stash save "Stashing specific files" -- file1.txt file2.txt

Viewing and managing multiple stashes

Use git stash list to see all your stashes:

$ git stash list
stash@{0}: On master: Your custom message here
stash@{1}: On master: Stashing specific files

Each stash has a unique identifier like stash@{0}. To apply a specific stash, use its identifier with git stash apply:

git stash apply stash@{1}

 

Integrating Git Stash with Branches

Stashing changes before switching branches

Imagine you’re working on a feature in the feature-x branch, and you need to switch to the master branch to review some changes. Here’s how to use Git Stash to save your progress and switch branches:

git stash save "Feature X work-in-progress"
git checkout master

Now, you can work on the master branch. When you’re ready to return to the feature-x branch and resume your work, switch back, and apply the stash:

git checkout feature-x
git stash apply

Applying stashed changes to a new branch

Sometimes, you might want to apply stashed changes to a new branch instead of the original branch. To do this, use the git stash branch command followed by the new branch name:

git stash branch new-feature-branch

This command will create a new branch, apply the stash to it, and then remove the stash.

Resolving conflicts when applying stashes

When applying a stash, you may encounter conflicts if the changes in the stash conflict with the changes in your working directory. In this case, Git will provide a message like this:

Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt

Resolve the conflicts by manually editing the conflicting files, then stage the changes using git add and complete the process with git stash drop:

git add file.txt
git stash drop

 

Advanced Git Stash Techniques

Creating a branch from a stash

If you want to create a new branch and apply a stash directly to it in one step, use the git stash branch command followed by the new branch name and the stash identifier:

git stash branch new-feature-branch stash@{1}

Stashing untracked and ignored files

By default, Git Stash only saves changes to tracked files. If you want to include untracked files (new files that haven’t been added to Git), use the --include-untracked or -u option:

git stash save --include-untracked "Stashing untracked files"

To stash ignored files as well, use the --all or -a option:

git stash save --all "Stashing all files, including ignored ones"

Git Stash and Git Hooks

Git Hooks are scripts that run automatically when certain Git events occur. You can use hooks to perform custom actions when stashing or applying changes. For example, you might want to run tests or lint your code before applying a stash.

To create a hook, place an executable script with the appropriate name (such as pre-applypatch or post-applypatch) in the .git/hooks directory of your repository.

 

Best Practices and Common Pitfalls

When to use Git Stash and when not to

Git Stash is helpful for temporarily storing work-in-progress changes. However, it’s not a substitute for proper branching and committing practices. Use Git Stash when you need to switch branches for a short period, but always aim to commit your changes to a feature branch as soon as possible.

Organizing and maintaining your stashes

It’s essential to keep your stashes organized to avoid confusion. Use descriptive messages when creating stashes, and periodically review and clean up your stash list with git stash list and git stash drop.

Avoiding common Git Stash mistakes

Don’t rely on stashes as a long-term storage solution. They are meant for temporary use.

Always check for conflicts after applying a stash and resolve them before continuing your work.

Be cautious when stashing changes in multiple branches, as it can be confusing to remember which stash belongs to which branch. Try to apply and remove stashes as soon as possible to avoid confusion.

 

Summary and Key Takeaways

Git Stash allows you to temporarily save changes in your working directory so you can switch branches or tasks without losing your progress. It’s a powerful tool when used correctly, enabling you to work on multiple tasks simultaneously without committing incomplete code.

By understanding and utilizing Git Stash commands and techniques, you can improve your workflow and switch between tasks seamlessly. Remember to use descriptive messages, resolve conflicts when applying stashes, and maintain your stash list to maximize this feature.

 

Frequently Asked Questions

How do I recover a dropped stash?

To recover a dropped stash, first, find the stash’s commit hash using the git fsck --unreachable | grep commit command. Note that you may see a lot of commits on this list, so it may not be the simplest task. Then, use the git show command followed by the commit hash to view the stash. Remember that the stash’s commit hash is presented when you drop it.

git stash drop
Dropped refs/stash@{0} (60cfdd75c9e23d8a0508d953fb56466ac2963097)

Finally, create a new stash using git stash apply or git stash branch with the commit hash.

Can I apply a stash to multiple branches?

Yes, you can apply a stash to multiple branches by checking out each branch and using the git stash apply command. Remember that the stash is not removed after applying, so you can continue to apply it to other branches as needed. Use git stash drop to remove the stash when you’re done.

How do I merge stashes?

Merging stashes is not a built-in Git Stash feature. However, you can apply multiple stashes one after the other and resolve any conflicts manually. Then, create a new stash from the merged changes, and drop the original stashes.

How can I undo a git stash apply or git stash pop?

To undo a git stash apply or git stash pop, use the git reset --hard command followed by the HEAD reference to revert your working directory to the state before the stash was applied. Be cautious when using this command, as it will discard any changes in your working directory.

#git #cli