How to Merge a Specific Commit in Git
Sometimes it is necessary to merge a specific commit in Git. The below-given snippet is aimed at showing you how to do it in a rather fast and easy way.
There are cases when developers need to move a specific commit to another branch of the git repository. Below, you will find out how to do it by using git cherry-pick.
Steps to cherry-pick a commit
Now let’s follow these steps to get the job done appropriately:
Fetch the changes from the remote
First of all, you need to download all the changes from the remote by running the git fetch command:
git fetch pulls down the branch locally
git fetchIf you work with multiple remotes, then refer to How to Fetch All Git Branches.
Find the appropriate commit hash
If you don't know the hash of the commit you want to apply, then you can find it by following the steps below:
- With the first step, checkout the branch containing that commit:
git checkout the branch
git checkout <branch-name>- Then, run the git log command with the
<kbd class="highlighted">--oneline</kbd>option to get the compact view of the commits. Afterwards, you can find the appropriate commit hash.
git log to show the commits
git log --onelineApply a specific commit
Now, it is necessary to checkout the branch where you want to apply the specific commit:
git checkout to apply a specific commit
git checkout <branch-name>Run the <kbd class="highlighted">git cherry-pick</kbd> command to apply the commit to your branch:
git cherry-pick
git cherry-pick <sha1-commit-hash>Push the branch
The final step is pushing the branch by running the git push command (suppose, the name of remote is origin, which is by default):
git push the branch
git push origin <branch-name>