This often happens to me: I write some code, go to check in my changes, and then realize I’m not in the proper branch to check in those changes. However I can’t switch to another branch without my changes reverting. Is there a way to move changes to another branch to be checked in there?
Share
Updated Answer
No need to use
stashcommand. uncommitted changes do not belong to any branch so just usegit checkout -b <new-branch>Orginal Answer
git stashis your friend.If you have not made the commit yet, just run
git stash. This will save away all of your changes.Switch to the branch you want the changes on and run
git stash pop.There are lots of uses for git stash. This is certainly one of the more useful reasons.
An example: