Git cherry-pick From Another Repository
Sometimes I want to apply some commits
or patches
from another some repo.
For instance, I may want to get some commits that are not merged into upstream
repository, from a fork of this upstream.
So you may need apply some patches from someone else’s repository into your repository.
In order to achieve that, first, just switch to directory and branch that you want to apply changes.
$ cd your-project
$ git checkout your-branch
# or use switch if you have git >= 2.23.0
# $ git switch your-branch
Add the desired commit repository as a new remote that you want to cherry-pick from. Let’s say new remote’s name is somefork.
$ git remote add somefork [email protected]:someauthor/dotfiles.git
Now, if you look at your remotes, you will probably see 2 separate repository.
$ git remote -v
dotfiles [email protected]:someauthor/dotfiles.git (fetch)
dotfiles [email protected]:someauthor/dotfiles.git (push)
origin [email protected]:SerhatTeker/dotfiles.git (fetch)
origin [email protected]:SerhatTeker/dotfiles.git (push)
After hat, you can fetch the related branch, let’s say somebranch
.
$ git fetch somebranch
If you don’t already have the commit hash, you can get via log
:
$ git log somefork/somebranch
# Or in a oneline logs
$ git log --oneline --decorate --graph --all somefork/somebranch
Now cherry-pick
the commit:
$ git cherry-pick 17fdsta
As usual, you can push the repo after cherry-pick
.
$ git push origin master
When all done, if you don’t need the remote repository any more, just remove it:
$ git remote remove somefork
All done!