1. Learn center
  2. Software Development
  3. Guides
  4. Git tutorial
  5. How to use Git
  6. How to manage history in Git
  7. Copy a commit to a different branch

Project and code management together.

Try it free

Copy a commit to a different branch

Go to the git-tutorial/tutorial4 directory you previously downloaded.

When you examine the history of this repository, it will look like the following:

Current history

We are going to create a new commit in the main branch that is a copy of a commit on a different branch of the same repository. In this case, let’s copy the commit with the message “append description of the commit command.”

Switch over to the main branch. Use the git cherry-pick command and specify the commit hash for “append description of the commit command.”

The commit hash 99daed2 may differ depending on your local repository. You can obtain the commit hash by running the git log command in that branch. Find the commit hash of your local repository and use that instead.

$ git checkout main
  Switched to branch 'main'
$ git cherry-pick 99daed2
  error: could not apply 99daed2... commit
  hint: after resolving the conflicts, mark the corrected paths
  hint: with 'git add <paths>' or 'git rm <paths>'
  hint: and commit the result with 'git commit'

As you can see above, a conflict has occurred. Manually resolve it and proceed committing the change.

$ git add sample.txt
$ git commit

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life