简体   繁体   English

Cherry-pick从其他存储库提交到git存储库的特定文件夹

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

I am very much familiar with git cherry-pick. 我对git cherry-pick非常熟悉。 Currently i am trying to cherry-pick few commits from other git repository. 目前,我正在尝试从其他git存储库中挑选一些提交。 Scenario is as below: 场景如下:

A -> git repo ("A/foo/B" where B is a directory inside foo) A-> git repo(“ A / foo / B”,其中B是foo中的目录)

B -> git repo B-> git回购

My intention is to cherry-pick/apply-patches/merge commits of git repo B to A/foo/B directory. 我的意图是将git repo B的cherry-pick / apply-patches / merge提交合并到A / foo / B目录。

A/foo/B 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. 但是,有什么方法可以用Cherry-Pick来获得相同的东西,从而获得预期的解决方案,或者通过任何其他完美的解决方案来弥补它。

Please suggest!! 请建议!

Thank you :) 谢谢 :)

You can either use checkout-index or just checkout . 您可以使用checkout-index或仅使用checkout Both have pros and cons. 两者都有优点和缺点。

With checkout-index 带结帐索引

  1. Change your working dir to repo A 更改工作目录以回购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. checkout-index检出(顾名思义)存储库的索引。 Thus you must ensure that the index of repo B looks like you want it. 因此,您必须确保存储库B的索引看起来像您想要的那样。 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 更改工作目录以回购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. checkout是您可以按原样选择任何提交。 Use a branch, commit id or tag instead of HEAD . 使用分支,提交ID或标签代替HEAD

The con is that if you checkout any other commit than HEAD the HEAD and index of repository B will change. con是,如果你签任何其他承诺比HEADHEAD资源库和索引B将发生变化。 So if you go back to the repository B you will see this changes if you do a git status . 因此,如果返回到存储库B ,则执行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 如果我已经有了目录名称B并且只想从存储库B到B目录挑选一些提交怎么办

With checkout-index files that already exist in folder B will not be overwritten until you specify --force or just -f . 对于文件夹B中已经存在的checkout-index文件,除非您指定--force或just -f否则它们不会被覆盖。

The checkout command above will overwrite files that already exist, because I use -- . 上面的checkout命令将覆盖已经存在的文件,因为我使用-- . at the end. 在最后。 You can select specific files by replacing . 您可以通过替换选择特定文件. with the path. 与路径。 Eg if you only want to checkout a src directory. 例如,如果您只想签出src目录。

git --git-dir=../B/.git --work-tree=B checkout HEAD -- src

You can use submodules . 您可以使用子模块

Submodules / subtree are basically git repository inside another git repository. 子模块 / 子树基本上是另一个git仓库内的git仓库。

The main difference between subtree and submodule is where your files are managed (as standalone repository or in the parent repository). 子树和子模块之间的主要区别是文件的管理位置(作为独立存储库或在父存储库中)。

Here is a simple script which creates 2 repositories and then add one of them as submodule of the second repository. 这是一个简单的脚本,该脚本创建2个存储库,然后将其中一个添加为第二个存储库的子模块。

At this point and changes which are made inside the submodule folder are "transparent" to the parent repo (repo1) 此时,子模块文件夹中所做的更改对父存储 (repo1)是“透明的”


# Clear old repositories if any
rm -rf /tmp/repo1
rm -rf /tmp/repo2

# Creating new empty repositories
git init repo1
git init repo2

# commiting to the first repository
cd /tmp/repo1
echo 'a' > file1.txt
git add .
git commit -m "Commiting to repo1"

# commiting to the second repository
cd /tmp/repo2
echo 'b' > file2.txt
git add .
git commit -m "Commiting to repo2"

# Adding repo2 as submodule of repo1
cd /tmp/repo1
git submodule add /tmp/repo2 repo2
git submodule init
git submodule update

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM