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
Sadly this happens to me quite regularly as well and I use git stash
if I realized my mistake before git commit
and use git cherry-pick
otherwise, both commands are explained pretty well in other answers
I want to add a clarification for git checkout targetBranch
: this command will only preserve your working directory and staged snapshot if targetBranch has the same history as your current branch
If you haven't already committed your changes, just use git checkout to move to the new branch and then commit them normally
@Amber's statement is not false, when you move to a newBranch,git checkout -b newBranch
, a new pointer is created and it is pointing to the exact same commit as your current branch.
In fact, if you happened to have an another branch that shares history with your current branch (both point at the same commit) you can "move your changes" by git checkout targetBranch
However, usually different branches means different history, and Git will not allow you to switch between these branches with a dirty working directory or staging area. in which case you can either do git checkout -f targetBranch
(clean and throwaway changes) or git stage
+ git checkout targetBranch
(clean and save changes), simply running git checkout targetBranch
will give an error:
error: Your local changes to the following files would be overwritten by checkout: ... Please commit your changes or stash them before you switch branches. Aborting
git stash
is 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:
# work on some code
git stash
git checkout correct-branch
git stash pop
If you haven't already committed your changes, just use git checkout
to move to the new branch and then commit them normally - changes to files are not tied to a particular branch until you commit them.
If you have already committed your changes:
git log
and remember the SHA of the commit you want to move.git cherry-pick SHA
substituting the SHA from above.git reset HEAD~1
to reset back before your wrong-branch commit.cherry-pick
takes a given commit and applies it to the currently checked-out head, thus allowing you to copy the commit over to a new branch.
A soft git reset will put committed changes back into your index. Next, checkout the branch you had intended to commit on. Then git commit with a new commit message.
git reset --soft <commit>
git checkout <branch>
git commit -m "Commit message goes here"
From git docs:
git reset [<mode>] [<commit>]
This form resets the current branch head to and possibly updates the index (resetting it to the tree of ) and the working tree depending on . If is omitted, defaults to --mixed. The must be one of the following:
--soft
Does not touch the index file or the working tree at all (but resets the head to , just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.