How to move accidentally committed changes to a brand new branch in git?

Create a new branch from the current state of master,
this is were your commit will live 🙂

$ git branch some-new-branch-name
# now go back to master
$ git checkout master
# remove the commit from the master branch
$ git reset HEAD~ --hard
# whoola!! you are back on track

the other way of doing this:

# undo the last commit, but leave the changes available
$ git reset HEAD~ --soft
$ git stash
# move to the correct branch
$ git checkout name-of-the-correct-branch
$ git stash pop
$ git add . # or add individual files
$ git commit -m "your message here"
# now your changes are on the correct branch

A lot of people suggest using cherry-pick for this situation too, so take your pick on whatever one makes the most sense to you!

yet another way:

$ git checkout name-of-the-correct-branch
# grab the last commit to master
$ git cherry-pick master
# delete it from master
$ git checkout master
$ git reset HEAD~ --hard

Leave a Reply