W3docs

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

This tutorial provides you with useful information about how to move the latest commits to a new or existing branch, without changing the original 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.

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:

create a new branchs git

git branch <new-branch-name>

Resetting the commits

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

Move the current branch back by n commits git

git reset --keep HEAD~N
Info

The <kbd class="highlighted">--keep</kbd> 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 <kbd class="highlighted">--hard</kbd> option to lose the changes even from files that didn't change between the commits.

Alternatively, instead of <kbd class="highlighted">HEAD~N</kbd>, you can use the hash of the commit you want to revert back:

add the hash of the commit git

git reset --keep &lt;sha1-commit-hash&gt;

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:

Check out the existing branch git

git checkout &lt;existing-branch&gt;

Integrating the commits

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

git merge

git merge &lt;wrong-branch&gt;

Checking out to the wrong branch

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

git checkout the master branch:

git checkout &lt;wrong-branch&gt;

Resetting the commits

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

Move the current branch back by n commits git

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:

Check out the existing branch git

git checkout &lt;existing-branch&gt;

Integrate a particular commit

The next step is bringing back the particular commit by using the <kbd class="highlighted">git cherry-pick</kbd> command:

git cherry-pick to integrate a particular commit

git cherry-pick &lt;sha1-commit-hash&gt;

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 <kbd class="highlighted">git reset</kbd> and <kbd class="highlighted">git checkout</kbd> commands have some similarities since they both operate on <kbd class="highlighted">HEAD</kbd>. If <kbd class="highlighted">git checkout</kbd> operates exclusively on the <kbd class="highlighted">HEAD</kbd> reference pointer, git reset passes the <kbd class="highlighted">HEAD</kbd> reference pointer and the current branch reference pointer.

One of the most commonly used options of <kbd class="highlighted">git reset</kbd> is <kbd class="highlighted">--hard</kbd>. With <kbd class="highlighted">--hard</kbd>, 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 <kbd class="highlighted">--keep</kbd> option resets index entries and updates files in the working tree that are different between commit and <kbd class="highlighted">HEAD</kbd>. When a file is different between commit and <kbd class="highlighted">HEAD</kbd> has local changes, reset is aborted.