Getting Only the Commits You Want Off of Another Persons Branch

Alright, so let's say you are in this situation. You have a master branch, and someone else has pushed a branch up with some changes:

Two of these changes look good, and you'd like to bring them into your master branch. However, one of the commits is "bad".

How can you bring in only certain commits of from someone else's branch?

git cherry-pick is one way to solve this problem. To use it, specify they SHA of each commit you would like to bring over to the branch that you are currently on. For example, while on the master branch:

git cherry-pick <sha-of-commit-D>
git cherry-pick <sha-of-commit-E>

This will result in the following:

Note that the SHAs of the new commits on master will be different! Even though the changes are identical, Git will treat these commits as separate, unique objects in its database.

It's possible that you don't have other_branch checked out locally, and that's OK! Even if the commits are on a remote repository, you can still reference them locally.

Hope that helps! Stay tuned for more Git tips in the future!

Published: 2022-04-30