Let's say I'm on branch A and I do some changes on the branch. For some reason, I need to check out to branch B But the problem is I don't wanna commit them yet. I just wanna keep them without committing them.
I already tried stashing using git add .
and then git stash
. But I'm not sure if I do it right because after I run git taste
. it automatically removes my changes. and when I switch to another branch and then switch back to that branch, all the changes disappeared.
what should I do in this situation? Any advice?
Asked
Active
Viewed 635 times
-1

Kirk Beard
- 9,569
- 12
- 43
- 47

Elaammari Dahman
- 51
- 4
-
`git worktree` is one of the options. https://stackoverflow.com/questions/31935776/what-would-i-use-git-worktree-for – ElpieKay May 30 '22 at 08:30
-
3"I don't wanna commit them yet" -> Why? A local commit can't break anything. Just commit without pushing. When you'll resume work on branch A, just undo the commit while keeping changes in place with `git reset HEAD^`. Of course, stashing somewhat automates this, but stash entries come and go... a temp commit stays on its relevant branch. – Romain Valeri May 30 '22 at 08:37
-
You can have a look at `--include-untracked` and `--keep-index` options to `git stash` command. Also, use `git stash` to save changes and `git stash pop` to apply them back. – Krzysiek Karbowiak May 30 '22 at 09:03
-
2What is `git taste` above? Presumably it is some alias you have for some other Git command...? – torek May 30 '22 at 11:31
2 Answers
2
It sounds like you need to pop the stash - It doesn't automatically get applied to the branch after you check it out.
Use something like git stash pop
or git stash apply

Kyle Trauberman
- 25,414
- 13
- 85
- 121
2
I prefer committing to stashing, before fiddling with working tree/cache (unless you use git worktree
to compare another branch content in another folder).
Once your status is clean (you have committed your work in progress), you can restore (git restore
) files from another branch to have a look:
git restore --source <anotherCommit> -- .
# once you are done
git restore -- . # restore HEAD content

VonC
- 1,262,500
- 529
- 4,410
- 5,250