Git

How to Merge a Specific Commit in Git

Git merges feature is one of the notable features of Git and is widely used to integrate the branches, code, and functionalities. By merging the Git branches, git developers can get and copy another branch’s commits. But sometimes, Git users want to access only specific commits or changes of branches instead of copying all commits.

This post will elaborate on the method for merging a specific commit of another branch in Git.

How to Merge a Specific Git Commit?

To merge a Git-specific commit, open the Git branch of which you want to merge the commit and note the commit id. Afterward, move to the Git branch in which you are needed to merge the specific Git commit and use the β€œgit cherry-pick commit-id” command for merging the commit.

Step 1: Launch the Git Terminal

From the Windows β€œStartup” menu, launch the Git terminal:

Step 2: Go to Git Repository

Move to the Git working repository by processing the β€œcd” command:

$ cd "C:\Git"

Step 3: View Branches

View all branches of Git through provided command and choose the branch of which you want to merge the commit:

$ git branch

For instance, we will merge the commit of the β€œdev” branch:

Step 4: Move to Branch

Use the β€œgit checkout” command to switch to the required branch:

$ git checkout dev

Check the Git log and note the commit id of a specific commit as highlighted below:

$ git log --oneline

Step 5: Switch Branch

Now, move to the branch in which you want to merge the specific commit of another branch through the mentioned command:

$ git checkout features

Step 6: Merge Specific Commit

Merge the specific commit by utilizing the β€œgit cherry-pick” command along with the copied commit id:

$ git cherry-pick e116d26

Step 7: Verify Commit is Merged

Next, check the logs of the currently opened branch and verify if the specific commit is merged or not:

$ git log --oneline

It can be observed from the output that we have successfully merged the specific commit:

We have demonstrated the procedure to merge a specific commit in Git.

Conclusion

To merge the specific Git commit, first, visit the Git repository. Note the commit id you need to merge. After that, switch to the branch in which you are merging the specific commit of another branch. Next, merge the commit using the β€œ$ git cherry-pick <commit-id>” command. In this blog, we demonstrated the method of merging a specific commit in Git.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.