How to cherry-pick commit from Github Repository

Many a times, there are changes made to the code on the GitHub repository which you would need to apply on your cloned git version to continue the work done by the others. This requires cherry-pick a commit hash from github. This page helps in knowing how you can cherry-pick commit from Github repository and start with your work on top of it.

How to cherry-pick commit from Github Repository

From the docs, git explains the process of cherry-pick as `Apply the changes introduced by some existing commits`.

The basic process and command statement that is used to cherry-pick is -

git cherry-pick <commit-hash>

But, if you directly hit this statement and try to retrieve the file changes from the commit, you'll probably end up with the below error.

fatal: bad object <commit-hash>

In order to fix the above, you need to first create a remote in your git for the user's repository that you need to cherry-pick from.

Eg, if the file changes were committed on https://github.com/username/reponame repository, you'll need to first create a remote using -

git remote add remote-name <ssh-repo-link>
  • remote-name can be anything so you can chose a proper value that you can identify at any point of time.
  • <ssh-repo-link> is the ssh cloned link that is used to clone the given reposoitory. You can also use the https link.

After creating the remote, fetch the repo changes using -

git fetch remote-name

You'll see the package getting unpacked on your local git setup.

Finally, you can use the same cherry-pick statement to retrieve the commit hash.

git cherry-pick <commit-hash>