How to Copy Commits From Another Branch in Git

Abdul Jabbar Feb 02, 2024
How to Copy Commits From Another Branch in Git

There comes the point while working in Git where we want to integrate specific, individual commits from one branch to our current HEAD branch. It could be because the client wants some changes in a product or any bug that has been reported.

Either we committed it mistakenly, or we do not want that commit anymore in our previous branch and want it in our current HEAD branch.

The answer to the above problem is CHERRY PICK. So, in the following sections, we will discuss the cherry-pick command in detail.

Copy Commits From Another Branch in Git

Picking up a particular commit from one branch and copying it into our current HEAD branch is called Cherry Picking.

Git has a particular command for this purpose, that is, Git’s cherry-pick command. The other use of cherry-picking is to apply specific changes before we merge or develop a pull request.

This can be very useful for undoing changes.

This article will discuss cherry-picking a specific commit from one branch into another.

View Commits Using the git log Command

First, we will use the git log to investigate which commit we want to pick. The following is the branch result of the git log command:

d23216 - 953222	- 953219 - aa3s36 - 532d37 [master]
           \
            76cada - 66ecb3	- b886a0 [feature]

Here in the feature branch, there is a commit 66ecb3 from another branch that we only want in our master branch.

Let us cherry-pick that specific commit and transfer it to the current branch, the master branch. Then, we can use that commit changes in the future for our project.

Run the git cherry-pick Command to Copy Commits

Here, git cherry-pick will come to our rescue. 66ecb3 is the cherry, and we want to pick it from lots of pushes has been done in the past.

Let us pick it from another branch by running the following command.

git checkout master
git cherry-pick 66ecb3

Once the above-listed command is run successfully, now, 66ecb3 acts as a new commit in our master branch.

So Git made a copy of the commit that we needed, with the same commit messages and changes on the master branch. Eventually, a new commit with its new ID is created.

Clean Up the Other Branch Using the git reset Command

If we switch to the feature branch, we will see the same commit at its old place. This is because Git has copied it to the other branch rather than moving it.

It has left the original one untouched.

Now, for cleaning and undoing, we will use the command git reset after checking out to the required branch.

$ git checkout Test
Switched to branch 'Test'
$ git reset --hard HEAD~1
HEAD is now at 66ecb3 Change the title and delete the error page
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