How to Move Commit to Another Branch in Git

Abdul Jabbar Feb 02, 2024
  1. Calculate How Many Commits to Moving in Git
  2. Moving Commits to a New Branch in Git
  3. Move Commits to an Existing Branch in Git
How to Move Commit to Another Branch in Git

Git is a very useful and powerful tool in the modern software world. Many types of files and codes can be stored via branching and committing in Git. Branching is a concept that is different depending on the version control system you are using. Many developers assume that it is the most preferred version control system due to its efficiency.

Most of the time, we face this situation when after completing some work and committing to a specific branch, we realize that we commit to the wrong branch mistakenly and want to commit that again on another branch. Here Git comes for our rescue, allowing us to move our commits to other existing branches or on the new branch.

This guide will give us a basic understanding of how to move our commits to another branch, it may be a new or existing one. In addition, we will discuss how to create a new branch with the git branch command, move a commit with the git reset command, and merge those changes back into the main branch with the git merge command.

Calculate How Many Commits to Moving in Git

Before starting the whole process, we need to judge the situation. Let’s assume that we have checked out the branch that we’re going to change; we need to see the history for this purpose.

We will use the following command to calculate how many commits to move.

git log

After executing, we can see that the HEAD is two commits at the head of origin/HEAD, and these are the two desired commits we need to move to another branch. Below are the remaining steps, in which we will cover how to move these commits to a new branch or an existing branch.

Moving Commits to a New Branch in Git

The below-mentioned steps will show us how to move our latest commits to a new branch.

  • Creating a new branch
    git branch feature/new branch
    

    This command will create a new branch that will include all of the commits of the master(current) branch.

  • Move the current branch back two commits
    git reset --keep HEAD~2
    
  • Check out the new branch
    git checkout feature/new branch
    

By executing these, our two latest commits have been removed from master(current branch) and added in a new branch called feature/new branch.

Move Commits to an Existing Branch in Git

The below-mentioned steps will show us how to move our latest commits to an existing branch. It will be useful for us if we have been working out of a feature branch, but we started making commits in the wrong branch by mistake. Let’s assume that the current branch, with the commits that we want to remove, is master.

  • Checking out the existing branch
    git checkout feature/existing branch
    

    The command git checkout is not limited to just working with the working directory. It can also be used to move the HEAD reference pointer to a reference point on a branch.

  • Merge master branch
    git merge master
    

    The git merge command can also be used to merge branches into the current branch, but only if the current branch has already been merged into the branches being merged.

  • Check out the master branch
    git checkout master
    

    The command checkout master pulls down the latest version of the code and creates a new branch called the master.

  • Move the current branch back two commits:
    git reset --keep HEAD~2
    

    The option --keep will reset index entries and update files in the working tree that are different between commit and HEAD. When the file is different between commit and HEAD has local changes, reset is terminated. Thus, the latest two commits have been removed from the master and added to an existing branch.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Commit

Related Article - Git Branch