I am very much familiar with git cherry-pick. Currently i am trying to cherry-pick few commits from other git repository. Scenario is as below:
A -> git repo ("A/foo/B" where B is a directory inside foo)
B -> git repo
My intention is to cherry-pick/apply-patches/merge commits of git repo B to A/foo/B directory.
A/foo/B
I know it can be done in many ways, like with merge, cherry-pick and applying patch.
I have also tried below command, which is fulfilling my intention:
git --git-dir=../B/.git format-patch --stdout sha1^..sha1 | git am --directory='B/'
But is there any way to get the same thing with cherry-pick to get the intended solution Or any other perfect solution to make it up.
Please suggest!!
Thank you :)
It is possible to cherry pick from another repo using the command line. You will first need to add the other repository as a remote and then fetch the changes. From there, you should be able to see the commit in your repo and cherry pick it.
In the list of branches, click the branch that has the commit that you want to cherry-pick. Click History. Drag the commit that you want to cherry-pick to the Current Branch menu and drop the commit on the branch that you want to copy the commit to.
You can either use checkout-index
or just checkout
. Both have pros and cons.
With checkout-index
A
git --git-dir=../B/.git checkout-index -a --prefix=B/
git add B
git commit
checkout-index
checks out (as the name implies) the index of a repository. Thus you must ensure that the index of repo B
looks like you want it. The pro is that you can modify the index before you check it out to your working directory.
With checkout
A
mkdir B
git --git-dir=../B/.git --work-tree=B checkout HEAD -- .
git add B
git commit
The pro of checkout
is that you can pick any commit as it is. Use a branch, commit id or tag instead of HEAD
.
The con is that if you checkout any other commit than HEAD
the HEAD
and index of repository B
will change. So if you go back to the repository B
you will see this changes if you do a git status
.
what if i already have a directory name B in it and i just want to cherry-pick few commits from repository B to B directory
With checkout-index
files that already exist in folder B
will not be overwritten until you specify --force
or just -f
.
The checkout
command above will overwrite files that already exist, because I use -- .
at the end. You can select specific files by replacing .
with the path. E.g. if you only want to checkout a src
directory.
git --git-dir=../B/.git --work-tree=B checkout HEAD -- src
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With