moving changed files to another branch for check-in
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
Update: No need to use stash
command. uncommitted changes do not belong to any branch so just use git checkout -b <new-branch>
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:
- Type
git log
and remember the SHA of the commit you want to move. - Check out the branch you want to move the commit to.
- Type
git cherry-pick SHA
substituting the SHA from above. - Switch back to your original branch.
- Use
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.