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 ofmaster
.mandeep
branch: Almost the same asmaster
.Goal: Merge specific commit(s) from
demo
tomandeep
, then push tomaster
.
Steps to merge a specific commit from demo
to mandeep
and then push to master
:
Switch to the
mandeep
branch:
First, ensure you are on themandeep
branch.git checkout mandeep
Find the commit hash on the
demo
branch:
Identify the specific commit(s) you want to merge from thedemo
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.Cherry-pick the specific commit(s):
Usegit cherry-pick
to apply the specific commit(s) from thedemo
branch to themandeep
branch.git cherry-pick <commit-hash>
Replace
<commit-hash>
with the actual commit hash from thedemo
branch.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
.Push the changes to
mandeep
:
After successfully merging the specific commit intomandeep
, push the changes to the remotemandeep
branch:git push origin mandeep
Switch to
master
branch:
Now that the changes are on themandeep
branch, you can switch to themaster
branch:git checkout master
Merge
mandeep
intomaster
:
Merge themandeep
branch into themaster
branch:git merge mandeep
Push the changes to
master
:
Finally, push the merged changes to themaster
branch:git push origin master
Summary:
Cherry-pick the specific commit from
demo
tomandeep
.Push the changes from
mandeep
to the remote.Merge the
mandeep
branch intomaster
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
.