How to Move the Recent Git Commits to New or Existing Branch

There are cases when after committing on a particular branch you realize that you want to commit on another one. In such occasions, Git comes to the rescue allowing to move your commit to another existing branch or a new one.

Below, we will show you how to do it.

Watch a course Git & GitHub - The Practical Guide

Moving to a New Branch

Here, we will discuss the scenario of moving the current state of your branch to a new branch. Follow the steps below, to do that.

Creating a branch

Create a new branch, containing all the current commits using the git branch command, which, on contrast to git checkout -b doesn't switch to the newly created branch:

git branch <new-branch-name>

Resetting the commits

Move the current branch back by the number of the mistaken commits:

git reset --keep HEAD~N
The --keep option preserves the uncommitted changes in unrelated files, or aborts if the changes would have to be overwritten. If it aborts, git stash your changes and retry, or you can use the --hard option to lose the changes even from files that didn't change between the commits.

Alternatively, instead of HEAD~N, you can use the hash of the commit you want to revert back:

git reset --keep <sha1-commit-hash>

So, your recent mistaken commits have been moved to a new branch.

Moving to an Existing Synchronized Branch

Here, we will discuss a scenario of working on a feature on a particular branch but making a commit on another one, by mistake. Now, the task is to bring back the commit to the right branch. Suppose that the two branches are synchronized.

Switching to the existing branch

Check out the existing branch with the git checkout command:

git checkout <existing-branch>

Integrating the commits

Run the git merge command to bring back the commits to the wrong branch:

git merge <wrong-branch>

Checking out to the wrong branch

Then, check out to the branch, on which you have made the mistaken commits:

git checkout <wrong-branch>

Resetting the commits

Move the current branch back by the number of the mistaken commits by using the git reset command:

git reset --keep HEAD~N

Moving to an Existing Branch

There are cases when you want to move a particular commit, instead of merging the wrong branch with the existing one.

Below, we will demonstrate how to move commits in such cases:

Switching to the existing branch

Check out the existing branch with the git checkout command:

git checkout <existing-branch>

Integrate a particular commit

The next step is bringing back the particular commit by using the git cherry-pick command:

git cherry-pick <sha1-commit-hash>

To complete you need to checkout to the wrong branch and reset the commits. To do that, see the third and fourth steps of the section above.

The git reset and git checkout Commands

At first sight, the git reset and git checkout commands have some similarities since they both operate on HEAD. If git checkout operates exclusively on the HEAD reference pointer, git reset passes the HEAD reference pointer and the current branch reference pointer.

One of the most commonly used options of git reset is --hard. With --hard, the commit history reference pointers start pointing to the stated commit. After, the staging area and the working directory are reset to correspond to the stated commit. Changes that have been previously pending to the staging area and the working directory, are reset to match the commit tree state. Any pending commit in the staging index and working directory will be lost.

The --keep option resets index entries and updates files in the working tree that are different between commit and HEAD. When a file is different between commit and HEAD has local changes, reset is aborted.