Reputation: 2811
I was accidentally working on the wrong branch. Now I want to transfer all my changes to the correct branch.
If I stash the changes and apply them on the correct branch, will it only add the uncommitted changes to the correct branch or every change/commit from the wrong branch that doesn't exist on the correct branch?
For example
Wrong branch has:
Commit a
Uncommitted Changes b
Correct branch has
If I do git stash on the wrong branch and git apply stash in the correct branch, will it transfer commit a to the correct branch?
Upvotes: 29
Views: 41664
Reputation: 1524
If your branch doesn't exist yet:
if it does:
Upvotes: 15
Reputation: 9733
What you can also do:
Upvotes: 0
Reputation: 1024
I would do one stash, then reset (mixed so you don't lose the changes) the a commit, stash that, then change to the correct branch and pop both stashes.
git stash save "b"
git reset HEAD~
git stash save "a"
git checkout correct-branch
git stash pop
git commit -m "a"
git stash pop
Upvotes: 15
Reputation:
No it won't. Commits are not put in the Stash. I also sometimes just switch branches with my changes uncommitted and unstashed and it also works (not sure if in every case, though).
Upvotes: 1
Reputation: 1661
Workaround
git cherry-pick 23h123kjb
(<-- replace this hash with the one found in a git log
specific to the commit you want to bring in)Upvotes: 6