Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't the git stash unique per branch?

Tags:

git

git-stash

I suppose it allows for moving changes from one branch to the next but that's what cherry picking is for and if you're not making a commit of your changes, perhaps you shouldn't be moving them around?

I have on occasion applied the wrong stash at the wrong branch, which left me wondering about this question.

like image 690
iros Avatar asked Sep 18 '08 02:09

iros


People also ask

Does git stash work per branch?

In its simplest form, the git stash command creates a stash entry. To reapply our stashed changes at a later point, we can use git stash apply . We can apply the stash entry to a different branch – it doesn't have to be the branch that we created the stash from.

Does each branch have its own stash?

1 Answer. Show activity on this post. So there's only one "branch" containing all stashes.

Can you have more than one git stash?

If you want to git stash pop twice because you want both stashes in the same commit but you encounter "error: Your local changes to the following files would be overwritten by merge:" on your 2nd git stash pop , then you can: 1) git stash pop , 2) git add . , and 3) git stash pop . This helped me.


1 Answers

As mentioned, if you want a “per-branch stash,” you really want a new branch forking off from the existing branch.

Also, besides the already mentioned fact that the stash allows you to pull into a branch that you’re working on, it also allows you to switch branches before you have committed everything. This is useful not for cherry-picking in the usual sense so much as for cherry-picking your working copy.

F.ex., while working on a feature branch, I will often notice minor bugs or cosmetic impurities in the code that aren’t relevant to that branch. Well, I just fix those right away. When time comes to commit, I selectively commit the relevant changes but not the fixes and cosmetics. Instead I stash those, which allows me to switch to my minor-fixes-on-stable branch, where I can then apply the stash and commit each minor fix separately. (Depending on the changes in question, I will also stash some of them yet again, to switch to a different feature branch, where I apply those.)

This allows me to go deep into programming mode when I am working, and not worry about proper librarianship of my code. Then when I take a mental break, I can go back and carefully sort my changes into all the right shelves.

If the stash weren’t global, this type of workflow would be far more difficult to do.

like image 137
Aristotle Pagaltzis Avatar answered Sep 21 '22 17:09

Aristotle Pagaltzis