Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cherry-pick commits to a particular folder of git repo from other repo

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 :)

like image 319
love Avatar asked Oct 07 '15 06:10

love


People also ask

Can you cherry pick commits from another repo?

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.

How cherry pick single commit from another branch?

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.


1 Answers

You can either use checkout-index or just checkout. Both have pros and cons.

With checkout-index

  1. Change your working dir to repo A
  2. git --git-dir=../B/.git checkout-index -a --prefix=B/
  3. git add B
  4. 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

  1. Change your working dir to repo A
  2. mkdir B
  3. git --git-dir=../B/.git --work-tree=B checkout HEAD -- .
  4. git add B
  5. 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 checkoutcommand 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
like image 72
René Link Avatar answered Oct 01 '22 04:10

René Link