No and No. git stash is per-repository.
Here is a nice page on how to use it.
To see the current stash stack:
git stash list
To pick a specific stash from the stack, refer to it by the stash@{number}
shown by the above.
If you want the behavior to be per-branch, you can just make a commit (or multiple commits) on the branch. You can always "unmake" the commit(s) later (e.g., with git reset
, either --soft
or --mixed
; see the git reset documentation; or with git rebase -i
to keep only the eventual "real" commit(s) while discarding the temporaries).
(To really emulate git stash
you need at least two commits, one for the index state and one for the work-tree state. If you're not planning to save and restore the index state, though, you can just git add -A
the entire work-tree state and put that in the temporary commit. Alternatively, git stash
is a shell script so you could copy and modify it pretty easily to make it work per-branch by default, using, e.g., refs/pb-stash/branch
as its working name-space, rather than the single global refs/stash
for the entire repo. You'd still be able to bring a stash from one branch to another by naming it explicitly.)
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
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.
git stash
(which can be lost easily when you have lots of stashes and branches) 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 backgit commit
and git reset
when used together correctly can simulate a git stash
for a specific branchHere is a common real-life scenario that demonstrates the value and the usage the commit
and reset
commands:
git commit
on feature branch X
COMMIT_HASH_VALUE
for latergit reset ${COMMIT_HASH_VALUE}
(FYI the default for git reset
is --mixed
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With