How to Merge a Specific Commit in Git

Better Stack Team
Updated on August 12, 2024

Merging a specific commit from one branch into another in Git involves a few steps. This process typically requires using the cherry-pick command, which applies the changes from a specific commit onto your current branch. Here's a detailed guide on how to achieve this:

Steps to Merge a Specific Commit

1. Identify the Commit

First, identify the commit hash that you want to merge. You can find this by listing the commit history on the branch where the commit exists:

 
git log <branch-name>
  • Replace <branch-name> with the name of the branch containing the commit.
  • Note the commit hash (a long alphanumeric string) that you want to merge.

2. Checkout the Target Branch

Switch to the branch where you want to apply the commit. This is the branch where the changes will be merged.

 
git checkout <target-branch>
  • Replace <target-branch> with the name of the branch where you want to apply the commit.

3. Cherry-Pick the Commit

Use the cherry-pick command to apply the changes from the specific commit onto the current branch.

 
git cherry-pick <commit-hash>
  • Replace <commit-hash> with the hash of the commit you want to apply.

4. Resolve Conflicts (if any)

If the commit being cherry-picked causes conflicts with the current branch, Git will pause and prompt you to resolve them. To handle conflicts:

  • Check the Conflicted Files: Git will indicate which files have conflicts.
  • Resolve Conflicts: Edit the conflicted files to resolve the issues.
  • Stage the Resolved Files:

     
    git add <resolved-file>
    
  • Continue Cherry-Pick:

     
    git cherry-pick --continue
    

    If you want to abort the cherry-pick process:

     
    git cherry-pick --abort
    

5. Commit the Changes

If the cherry-pick was successful and there were no conflicts, Git will automatically create a new commit on your target branch. If conflicts were resolved, the changes will be committed as part of the cherry-pick process.

6. Push Changes to Remote (if necessary)

After cherry-picking the commit, you may want to push the changes to a remote repository:

 
git push origin <target-branch>
  • Replace <target-branch> with the name of your branch.

Example Workflow

Assume you want to cherry-pick a commit from feature-branch into master.

  1. Find the Commit Hash on feature-branch:

     
    git checkout feature-branch
    git log
    

    Note the commit hash you want to cherry-pick.

  2. Switch to master:

     
    git checkout master
    
  3. Cherry-Pick the Commit:

     
    git cherry-pick <commit-hash>
    
  4. Handle Conflicts (if any):

    Resolve conflicts, add resolved files, and continue:

     
    git add <resolved-file>
    git cherry-pick --continue
    
  5. Push the Changes:

     
    git push origin master
    

Summary

To merge a specific commit from one branch to another in Git:

  1. Find the Commit Hash: Use git log to get the hash of the commit you want.
  2. Checkout the Target Branch: Switch to the branch where you want the commit applied.
  3. Cherry-Pick the Commit: Apply the commit with git cherry-pick <commit-hash>.
  4. Resolve Conflicts: If there are conflicts, resolve them and continue.
  5. Push Changes: Push the updated branch to the remote repository if needed.

Using cherry-pick allows you to selectively integrate changes from one branch into another without merging the entire branch.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github