2015-10-07 109 views
4

我非常熟悉git樱桃挑选。目前我正在尝试从其他git存储库挑选几个提交。 方案是如下:樱桃挑选承诺从其他回购的git回购特定文件夹

A - > git仓库( “A /富/ B”,其中B是内部FOO的目录)

乙 - > git仓库

我的意图是将git repo B提交到A/foo/B目录的cherry-pick/apply-patches/merge提交。

A /富/ B

我知道它可以通过多种方式来实现,像合并,摘樱桃和应用补丁。

我也曾尝试下面的命令,这是实现我的意图:

git --git-dir=../B/.git format-patch --stdout sha1^..sha1 | git am --directory='B/' 

但是,有没有什么办法让同摘樱桃一样的东西,以获得预期的解决方案或其他任何完美的解决方案,使它起来。

请建议!!

谢谢:)

+0

子模块怎么样? – CodeWizard

+0

如果您正在提交的提交移动目录,则移动目录和樱桃提取提交只有一些共同之处。一般来说,你应该避免做大量的樱桃选择,如果你需要这个,你可能想要考虑一个选择。 –

+0

@codeWizard我尝试使用子模块,链接https://groups.google.com/forum/#!topic/git-users/HXoX-kpkYkM,但问题是我无法保留文件级历史记录。我想要保留文件级历史记录。 – love

回答

2

您可以使用checkout-index或只是checkout。两者都有优点和缺点。

使用Google Checkout指数

  1. 更改你的工作目录,以回购A
  2. git --git-dir=../B/.git checkout-index -a --prefix=B/
  3. git add B
  4. git commit

checkout-index检查出来(一名字暗示)存储库的索引。因此,您必须确保回购B的索引看起来像您想要的。亲是你可以修改索引之前,你检查出你的工作目录。

使用Google Checkout

  1. 更改你的工作目录,以回购A
  2. mkdir B
  3. git --git-dir=../B/.git --work-tree=B checkout HEAD -- .
  4. git add B
  5. git commit

checkout的专业人士是你可以选择任何提交,因为它是。使用分支,提交id或标记而不是HEAD

确认是,如果您签出除HEAD之外的任何其他提交,HEAD和存储库B的索引将会更改。因此,如果您回到存储库B,您将看到此更改,如果您执行git status

如果我在它已经有一个目录名B和我只是想摘樱桃几次提交从库B到B目录

与已经在文件夹中存在checkout-index文件B不会被覆盖,直到您指定--force或只是-f

上面的checkout命令将覆盖已存在的文件,因为我在末尾使用了-- .。您可以通过将路径替换为.来选择特定的文件。例如。如果你只想结账src目录。

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

感谢Rene的回答,如果我已经有一个目录名称B,我只想挑选一些从存储库B到B目录的提交。对不起,我已经修改了一下这个问题。 – love

0

您可以使用submodules

Submodules/subtree基本上git仓库内的另一个git仓库。

子树和子模块之间的主要区别在于您的文件被管理的位置(作为独立存储库或在父存储库中)。

这是一个简单的脚本,它创建2个存储库,然后将其中的一个添加为第二个存储库的子模块。

在这一点上并改变其子模块文件夹内取得的“透明”回购(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