How to move commit to another branch in GIT


To move a commit to another branch in Git, you can use the git cherry-pick command to apply the commit to the new branch, and then use the git reset command to remove the commit from the original branch.

Here are the steps to move a commit to another branch:

  1. Identify the commit that you want to move to the new branch by its hash value. You can use the git log command to view the commit history and find the hash value of the commit.
  2. Create a new branch from the current branch that will receive the commit. You can use the git branch command to create a new branch:
$ git branch <new-branch-name>
  1. Checkout the new branch using the git checkout command:
$ git checkout <new-branch-name>
  1. Use the git cherry-pick command to apply the commit to the new branch:
$ git cherry-pick <commit-hash>
  1. Verify that the commit has been applied to the new branch by using the git log command.
  2. Switch back to the original branch that you want to remove the commit from using the git checkout command:
$ git checkout <original-branch-name>
  1. Use the git reset command with the --hard option to remove the commit from the original branch:
$ git reset --hard HEAD~1

This will remove the last commit from the original branch and reset the branch to the state it was in before the commit was made.

Note that when you move a commit to another branch, the commit will be applied to the new branch as a new commit with a new hash value. Therefore, any changes that were made to the original commit after it was made will not be included in the new commit.

Example:

Assume we have a Git repository with two branches named master and feature. The current branch is master, and we want to move a commit from master to feature.

  1. First, we identify the commit hash that we want to move to the feature branch by using the git log command:
$ git log
commit 1234567890abcdefg (HEAD -> master)
Author: John Doe <[email protected]>
Date:   Fri Apr 30 13:05:00 2023 -0700

    Add new feature

commit 9876543210fedcba (origin/master)
Author: John Doe <[email protected]>
Date:   Fri Apr 30 12:00:00 2023 -0700

    Initial commit

In this example, we want to move the Add new feature commit with the hash 1234567890abcdefg to the feature branch.

  1. We create a new branch named feature using the git branch command:
$ git branch feature
  1. We checkout the feature branch using the git checkout command:
$ git checkout feature
  1. We use the git cherry-pick command to apply the commit to the feature branch:
$ git cherry-pick 1234567890abcdefg

This applies the Add new feature commit to the feature branch as a new commit with a new hash value.

  1. We verify that the commit has been applied to the feature branch by using the git log command:
$ git log
commit 0987654321dcbafe (HEAD -> feature)
Author: John Doe <[email protected]>
Date:   Fri Apr 30 13:10:00 2023 -0700

    Add new feature

commit 9876543210fedcba (origin/master), 1234567890abcdefg (master)
Author: John Doe <[email protected]>
Date:   Fri Apr 30 12:00:00 2023 -0700

    Initial commit

We can see that the Add new feature commit has been applied to the feature branch as a new commit with the hash 0987654321dcbafe.

  1. Finally, we switch back to the master branch using the git checkout command and use the git reset command to remove the commit from the master branch:
$ git checkout master
$ git reset --hard HEAD~1

This removes the Add new feature commit from the master branch and resets the branch to the state it was in before the commit was made.