Is git stash branch-specific or for the whole repository?

Viewed 38204

I went into a branch and did some work. I wanted to go into another branch but didn't want to commit so I did git stash. Then I did git checkout <otherbranch>. I did some work there and, just like in the first branch, I wanted to switch out of it before committing the work. So I did git stash there too. I switched back to the first branch and tried to unstash it (git stash pop) thinking it would get the stash from that specific branch. I was surprised that it unstashed the stash from <otherbranch> (latest stashed). I was under the impression that stash is branch-specific but this behavior indicates that there is only one stash for the whole local repository.

Is git stash branch-specific or for the whole repository? If it is for the whole repository, can I pass options to it to make it branch-specific?

4 Answers

I am not sure why every answer here suggest to emulate stash with commit+reset. Stash is perfectly fine to use.

So here is the stash workflow:

Whenever you have to switch the branch and your not ready to commit, save your changes to the stack

git stash save "Your custom stash message"

(if you dont want a custom message, simply use git stash).

When you return to a branch, you can see the stash list like this:

git stash list

enter image description here

If you on branch FixIssue0203 you could use use git stash pop because this will apply the top stash@{0} and remove it from stash.

However, if your in branch ImproveReadme you should first apply the stash 1 git stash apply stash@{1} and then remove stash 1 from stack git stash drop stash@{1}.

That's it!

git stash is not per-branch.

  • Instead of git stash (which can be lost easily when you have lots of stashes and branches)
  • I suggest doing a git commit to save the unfinished code in your branch and when you are ready to finish the code do a git reset ${COMMIT_HASH_VALUE} to get the unfinished code back
  • git commit and git reset when used together correctly can simulate a git stash for a specific branch

Here is a common real-life scenario that demonstrates the value and the usage the commit and reset commands:

  • you are working on feature branch X and your code doesn't even compile or pass the tests
  • there is a bug that is higher priority than the current new feature and so you must start work immediately on the bug fix
  • rather than do a git stash (and the stash gets lost in the mix because you have many stashes and many branches)
  • you can do a git commit on feature branch X
    • write down the COMMIT_HASH_VALUE for later
  • checkout a new branch Y for the hot fix
  • finish the hot fix on branch Y (do a merge request to get the hot fix into the baseline and delete the hot fix branch)
  • then checkout the feature branch X again
  • to pop your unfinished work that didn't compile or pass testing --> just do a git reset ${COMMIT_HASH_VALUE}

(FYI the default for git reset is --mixed)

Related