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:
- 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. - 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>
- Checkout the new branch using the
git checkout
command:
$ git checkout <new-branch-name>
- Use the
git cherry-pick
command to apply the commit to the new branch:
$ git cherry-pick <commit-hash>
- Verify that the commit has been applied to the new branch by using the
git log
command. - Switch back to the original branch that you want to remove the commit from using the
git checkout
command:
$ git checkout <original-branch-name>
- 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
.
- First, we identify the commit hash that we want to move to the
feature
branch by using thegit 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.
- We create a new branch named
feature
using thegit branch
command:
$ git branch feature
- We checkout the
feature
branch using thegit checkout
command:
$ git checkout feature
- We use the
git cherry-pick
command to apply the commit to thefeature
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.
- We verify that the commit has been applied to the
feature
branch by using thegit 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
.
- Finally, we switch back to the
master
branch using thegit checkout
command and use thegit reset
command to remove the commit from themaster
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.