【问题标题】:Moving a git subtree into different repository将 git 子树移动到不同的存储库中
【发布时间】:2013-05-06 15:45:34
【问题描述】:

我尝试将目录及其所有历史记录从存储库移动到另一个存储库。

使用git subtree split 可以轻松提取目录的完整历史记录。

这会创建一个新分支,可以轻松地将其提取到其他存储库。

现在我使用git subtree add 将目录粘贴回第二个存储库。

如果我查看gitkgit log --decorate --graph,一切看起来都很好。所有提交都按预期存在。此外,所有文件都按预期存在。

但是当我尝试使用git log -- transplanted_dir/somefile 查看移植文件的历史记录时,我只看到一个“合并”提交,这是由git subtree add 产生的。

为什么我在上面的 gitk 中看到了提交,但在单个文件的日志中却没有?

如果我做一个简单的git merge,我可以看到每个文件的历史记录,但这些文件当然不会存在于子文件夹中。

将移动的提交集成到其他存储库的正确方法是什么?

重现情况的详细示例命令:

#create two repositories:
git init a
git init b

# create directory dir with some history
cd a
mkdir dir
echo 1 > dir/file
git add .
git commit -am 1
echo 2 > dir/file
git commit -am 2
echo 3 > dir/file
echo 3 > otherfile
git add .
git commit -am 3

#split subtree
git subtree split --prefix dir -b split

#create a commit on repo b
cd ../b
mkdir otherdir
touch otherdir/file
git add .
git commit -am init

#fetch split branch of repo a
git fetch ../a split
git co -b split FETCH_HEAD
git log --decorate --graph --name-status 
git co master

# add commits to repo b
git subtree add --prefix somedir split

# this looks fine:
git log --decorate --graph --name-status

# no history here - Why?
git log --decorate --graph --name-status somedir/file
git log --decorate --graph --name-status --follow somedir/file

【问题讨论】:

  • 当你移动一个文件时,你重命名它。我很确定没有参数的git log 会在重命名时停止;再往前看,该文件不存在。
  • 不,没有重命名。 “合并”提交只是添加新文件。
  • @michas 将foo/bar/baz 移动到bar/baz 算作重命名。 git log --follow 的输出是什么?
  • 不,--follow 也不起作用。请按照上面给出的情况设置,然后自己尝试。

标签: git git-subtree


【解决方案1】:

好的,阅读源代码可以解决问题。

git subtree add 非常丑陋:它首先使用git read-tree当前 版本没有 任何历史记录添加到给定目录中。之后它使用git commit-tree 创建一个虚假的合并提交,以附加包含无前缀文件的旧历史记录。

另一方面,现在 HEAD 中的前缀文件和 HEAD^2 中的无前缀文件应该完全相同,并且应该被--follow 识别为移动。

不幸的是,它不被认可。不知道为什么。

最好的解决方案可能是添加显式提交,将文件移动到新目录并进行正常合并。 - 另一种方法是重写移植目录的历史记录,如How can I rewrite history so that all files, except the ones I already moved, are in a subdirectory? 所述。

对于我的情况 git subtree add 很糟糕,具有正确准备的子树的普通 git merge 似乎完全正确。

【讨论】:

  • 这里有一个非常相似的问题:stackoverflow.com/questions/10918244/…
  • 如果我有一个通过git subtree add 创建的存储库,有没有办法恢复历史以便git log 正常工作?
  • “正确准备的子树”是什么意思?您能否将问题中的命令/更改添加到答案中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
相关资源
最近更新 更多