# You work on master, then after a while you realize you should have worked in a branch. # You want to move the last X commits to a branch and remove them from master # commit what you got, even if it doesn't work git commit -m "work in progress, doesn't work" -a # create the new branch - it will have everything master has git checkout -b MY_NEW_BRANCH # go back to master and remove the extra commits # be extra careful if other people have pushed / pulled git checkout master git reset --hard HASH_OF_COMMIT # or HEAD^ if you just want to undo the last commit # or HEAD~3 if you just want to undo the last 3 commits git push --force origin master # now carry on working on new branch git checkout MY_NEW_BRANCH # if you had commit things that did not work, uncommit, keeping the changes on your filesystem git reset HEAD^ # ...do work... # commit working files git commit -m "now it works" -a # push it to the remote git push origin MY_NEW_BRANCH