How do I move unpushed committed code to another branch?

If you made the branch after you committed it should contain the commit that you are wanting to move. You can verify this with git log, you should see your commit as the first on in the log.

On the branch that you no longer want the commit to be do git reset --hard HEAD~. This removes the commit from the branch and reset the branch so that you can now pull without any issue. (Be sure that your commit is on the other branch as after doing this your commit will be gone).

If the commit is not on your other branch, you can either delete the branch and create it again from the original branch with git checkout -b <branch name> or you can cherry-pick it into your branch with git cherry-pick <SHA of your commit> which will make a copy of the commit on your branch. Then you can reset the original branch with the steps above.


If it's just one commit, you can simply do

git reset HEAD~1
git stash
git checkout anotherbranch
git stash pop

And if you want to put it in a fresh new branch, another way is

git branch newbranch
git reset --hard HEAD~1

Tags:

Git