How to Merge Specific Commits from One Git Branch to Another: A Step-by-Step Guide

How to Merge Specific Commits from One Git Branch to Another: A Step-by-Step Guide

To merge a specific commit from the demo branch (which is 7 commits ahead of master) into the mandeep branch and then push it to master, follow these steps:

Scenario:

  • demo branch: 7 commits ahead of master.

  • mandeep branch: Almost the same as master.

  • Goal: Merge specific commit(s) from demo to mandeep, then push to master.

Steps to merge a specific commit from demo to mandeep and then push to master:

  1. Switch to the mandeep branch:
    First, ensure you are on the mandeep branch.

     git checkout mandeep
    
  2. Find the commit hash on the demo branch:
    Identify the specific commit(s) you want to merge from the demo branch by using the following command:

     git log demo
    

    This will show you the commit history of the demo branch. Note the hash of the specific commit(s) you want to merge.

  3. Cherry-pick the specific commit(s):
    Use git cherry-pick to apply the specific commit(s) from the demo branch to the mandeep branch.

     git cherry-pick <commit-hash>
    

    Replace <commit-hash> with the actual commit hash from the demo branch.

  4. Resolve any conflicts (if necessary):
    If there are any conflicts during the cherry-pick, Git will pause for you to resolve them. After resolving the conflicts, you can run:

     git cherry-pick --continue
    

    If there are no conflicts, Git will automatically apply the commit to mandeep.

  5. Push the changes to mandeep:
    After successfully merging the specific commit into mandeep, push the changes to the remote mandeep branch:

     git push origin mandeep
    
  6. Switch to master branch:
    Now that the changes are on the mandeep branch, you can switch to the master branch:

     git checkout master
    
  7. Merge mandeep into master:
    Merge the mandeep branch into the master branch:

     git merge mandeep
    
  8. Push the changes to master:
    Finally, push the merged changes to the master branch:

     git push origin master
    

Summary:

  • Cherry-pick the specific commit from demo to mandeep.

  • Push the changes from mandeep to the remote.

  • Merge the mandeep branch into master and push it to the remote.

This way, you ensure only the desired commit(s) from demo is merged into mandeep and then applied to master.

Did you find this article valuable?

Support Mandeep Singh Blog by becoming a sponsor. Any amount is appreciated!