DEV Community

Cover image for How to copy / move a commit to another branch? — Advanced Git
5 Mins Learn
5 Mins Learn

Posted on • Edited on • Originally published at 5minslearn.Medium

How to copy / move a commit to another branch? — Advanced Git

Do you ever made a commit on feature branch and later realize that it should be made on master branch?

If you’re a beginner in learning git, you’ll definitely be confused with the answers from internet especially on Stack overflow.

But here’s a pretty simple and straight forward way to achieve this.

Cherry Pick

git cherry-pick command is used to apply the changes made on a commit to the current HEAD and create a new commit.

Let’s jump into an implementation example for a quicker understanding.

Before we begin, I made my repo clean

Git Status before begin

Let’s create a new branch named payment and switch to that branch

Git Create Branch

I made a couple of commits on payment branch containing the payment steps to be followed on Stripe and Razorpay payment gateways.

Changes made on payment branch

Let’s assume that a production bug arises in our project, which is so critical that it must be fixed and released as soon as possible.

Looking at the logs, I found the root cause and fixed the bug and made that as a commit.

PROD bug fix commit

A few seconds later, I realized that the patch was applied on the payment branch instead of master branch.

Since the fix commit involves changes in multiple files, I cannot start making those changes again in master branch.

This is where cherry-pick comes into play.

With cherry-pick you can pull the commit from any branch to any branch with it’s commit hash.

Let’s copy the commit from payment branch to master branch

Existing master branch commits

I’ve checked out to master branch and we can find that PROD fix commit is not here.

Git Cherry-Pick

Cherry-pick has completed without any errors. From the above screenshot, you can find that the same commit has been copied to master branch by comparing commit message. But, the commit id has changed ( f1a7384). It’s not the same commit id which we asked in cherry-pick ( 6475dba ).

It’s because, cherry-pick copies the changes made on that commit to this branch and creates a new commit.

PROD fix commit cherry-picked into master branch

It is confirmed from the above screenshot that the PROD fix has been pulled into master branch.

The lessons learned from mistakes and by raising questions can never be erased from our life. Leaving few unforgettable questions raised by my team here.

Interesting question from Naras was the first to hit my mind.

He asked, “Do you ever faced a scenario where your commit was deleted by mistake / intentionally and you brought it back with cherry-pick? ”

Though the answer to this question is quiet simple that it is “No”, a kind of such scenario happened to me once but I’m able to recover my commit.

When I was working on a company, I made few commits on a branch and made a pull request. Couple of hours later, the pull request was closed without merge.

I was shocked and managed to bring back the commits with the help of git log and git reflog commands and then applying cherry-pick for those commit ids.

“Can I edit the commit message for the cherry-picked commit?”, beautiful question from Udhaya.

Yes. Absolutely you can. By passing -edit flag with the cherry-pick command. During the cherry-pick process, git will prompt to enter the new commit message.

“I need all the changes from a commit in a working directory and not as a commit. Is it possible?”, another interesting question from Neevan.

Using --no-commit flag with the cherry-pick command ignores the commit message, instead it just copies the patch to the working directory in the current branch.

“What if there’s a conflict on cherry-pick? How git handles that effectively?”, Krish shooted with a sharp question.

This operation is similar to pull rebase operation. So, you’ll have options to continue, skip, quit or abort cherry-pick operation.

Here’s the explanation from git documentation site

--continue
Continue after resolving conflicts in a failed cherry-pick or revert.
--skip
Skip the current commit and continue with the rest of the sequence.
--quit
Forget about the current operation in progress. Can be used to clear the sequencer state after a failed cherry-pick or revert.
--abort
Cancel the operation.

You can run git cherry-pick --abort if you face conflict on cherry-pick and want to return back to the old state.

Note:

The answer to the next question covers a real time example to this.

“I want to cherry-pick 3 consecutive commits. How can I do that in a single command?”, question from curious Divad.

You can achieve that by adding .. between the 2 commit ids.

Here’s an example,

I switched to payment branch and made 3 commits.

Git cherry-pick series of commits

I want the top 3 commits (70e1ad5, d979f49, c16cc0d) to be cherry-picked to master branch. You can run the following command to achieve this.

git cherry-pick 70e1ad5..c16cc0d
Enter fullscreen mode Exit fullscreen mode

Git cherry-pick series of commits

I ran the command, and it resulted in a conflict. I have few options whether to continue further, or quit, etc.

I fixed the conflict and staged the changes and continued the cherry-pick operation by running git cherry-pick --continue

Git cherry-pick continue

The cherry-pick was successful. I can see all 3 the commits in master branch now.

Git log on master branch

Divad was amazed on hearing this.

Though cherry-pick is a handy tool for some scenarios. It’s always not recommended as alternative to merge and rebase , because it introduces a duplicate commit.

Reference

That’s all about git cherry-pick. Hope you enjoyed reading this article. Give a ❤️ if you like this article. Subscribe to our newsletter to receive more such insightful articles that get delivered straight to your inbox.

Top comments (0)