Two useful scenarios of git stash

Stashing conveniently stashes away uncommitted changes. As far as I know, there are two typical useful application scenarios:

  • Emergency fix to our project.
  • Add new trivial changes to any of non-latest commits without creating a redundant new commit before publishing.

Emergency fix to our project

After we changing some file such as style.css, then we have to make an emergency fix to our project.We don’t want to commit an unfinished feature, and we also don’t want to lose our current CSS addition. The solution is to temporarily remove these changes with the git stash command.

git status
git stash
git status

Before the stash, style.css was listed as “Changes not staged for commit:” The git stash command hid these changes, giving us a clean working directory. We’re now able to switch to a new hotfix branch to make our important updates—without having to commit a meaningless snapshot just to save our current state.

Let’s pretend we’ve completed our emergency update and we’re ready to continue working on our CSS changes. We can retrieve our stashed content with the following command.

git stash apply

The style.css file now looks the same as it did before the stash, and we can continue development as if we were never interrupted. Whereas patches represent a committed snapshot, a stash represents a set of uncommitted changes. It takes the uncommitted modifications, stores them internally, then does a git reset --hard to give us a clean working directory. This also means that stashes can be applied to any branch, not just the one from which it was created.

In addition to temporarily storing uncommitted changes, this makes stashing a simple way to transfer modifications between branches. So, for example, if you ever found yourself developing on the wrong branch, you could stash all your changes, checkout the correct branch, then run a git stash apply.

Add new trivial changes to any of non-latest commits without creating a new commit before publishing.

Sometimes after you did some commits, you ever found there are some trivial changes needed to be added that related to some specific commit. But you didn’t want to create a new commit, so git stash comes into play.

git status
git stash
git status

Now the changes are hidden.

git rebase -i <the hash before the to-be-revised commit>

Change pick to edit symbol before <the hash of the to-be-revised commit> in the automatically launched editor, save and close the editor.

Now you are back to some previous state, you should unhidden the changes:

git stash apply

And save the changes to the index:

git add <some file>

Then you could commit the new changes, if you don’t want to revise the message, --no-edit option is preferred.

git commit --amend --no-edit
git rebase --continue

If you have pushed the commits to remote repository and it’s shared by others, this method is not recommended.

That’s all!

本作品采用《CC 协议》,转载必须注明作者和本文链接
附言 1  ·  1周前

Some update:

When do you need git stash?

➜  learning git:(daily) ✗ git rebase -i d79d771
error: cannot rebase: You have unstaged changes.
error: additionally, your index contains uncommitted changes.
error: Please commit or stash them.
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

访问:11.4 万