简体   繁体   English

没有壁球的git-subtree:查看日志

[英]git-subtree without squash: view log

I merged a tree onto a my repository by using git subtree add without the squash option. 我使用git子树添加而不使用squash选项将树合并到我的存储库中。 A git log shows that the commits were successfully added to the repository. git日志显示提交已成功添加到存储库。 However, if I do a git log --follow filename , the history stops at the merge and does not show previous commits. 但是,如果我执行git log --follow filename ,则历史记录在合并处停止,并且不显示先前的提交。 I tried using -M instead of --follow and that doesn't work either. 我尝试使用-M而不是--follow ,这也不起作用。 How can I get a log of the commits for a specific file or files from before the merge? 如何在合并之前获取特定文件或文件的提交日志?

The commit created by git subtree merge or git subtree add does an "add" for the files coming from the subtree, not a "move". git subtree mergegit subtree add创建的提交对来自子树的文件执行“添加”,而不是“移动”。 This means that their history cannot be tracked as with other merges or movements. 这意味着无法像其他合并或移动一样跟踪其历史记录。

History for the file you want can still be displayed by looking directly in the subtree before the merge. 在合并之前直接查看子树,仍然可以显示所需文件的历史记录。 If your workspace is the merge commit that git subtree created then the second parent of it ( HEAD^2 ) will be the last commit of the original subtree. 如果您的工作区是git subtree创建的合并提交,那么它的第二个父级( HEAD^2 )将是原始子树的最后一次提交。 From here you can see the contents of the original subtree: 从这里您可以看到原始子树的内容:

# Display the contents of the original subtree
git ls-tree HEAD^2

From this commit you can track the changes of the file you are interested. 通过此提交,您可以跟踪您感兴趣的文件的更改。 Be careful that the path of your file will be different within the subtree that in your workspace. 请注意,文件的路径在工作区中的子树中是不同的。 You will need to remove the --prefix given to git subtree in order to have the correct path for your file. 您需要删除为git subtree提供的--prefix ,以便为您的文件提供正确的路径。

git log HEAD^2 --follow -- path-in-subtree/file

Actually, git log --follow should work with subtree merges, but it is known to be hackish for a long time [1-3]. 实际上, git log --follow 应该与子树合并一起使用,但是已知它很长时间都是hackish [1-3]。

One can stick with subtree merges and rest asured that the strategy is valid for tracking multiple histories, and patiently wait for the unavoidable event that git log --follow will improve. 可以坚持使用子树合并和休息,以确保该策略对于跟踪多个历史记录是有效的,并耐心地等待git log --follow将改善的不可避免的事件。 This may actually be a viable decision, since at present git log --follow can see some history in very useful cases. 这实际上可能是一个可行的决定,因为目前git log --follow可以在非常有用的情况下看到一些历史记录。 Say you moved a file from the toplevel repo to a sub-repo, then it can track the full move. 假设您将文件从顶级仓库移动到子仓库,然后它可以跟踪完整的移动。 When you want to track the history that is specific to a sub-repo, you really have to have a separate copy or check out a sub-repo branch. 当您想要跟踪特定于子仓库的历史记录时,您必须拥有单独的副本或签出子仓库分支。

Alternatives and Workarounds 替代方案和解决方法

You can get logs for the files like this [1]: 您可以获取这些文件的日志[1]:

git log -- '*filename'          # from the toplevel

This views each commit that touched a file whose name ends with filename . 这将查看触及名称以filename结尾的文件的每个提交。 It won't follow actual renames, and may show false positive if you have several files with the same basename [1]. 它不会遵循实际的重命名,如果你有几个具有相同basename [1]的文件,它可能会显示误报。

You can also merge the repositories using different strategies. 您还可以使用不同的策略合并存储库。 Ref [4] shows a way to do this which is very close to what you have with regular subtree merging, but with traceable histories. Ref [4]显示了一种方法,它与常规子树合并非常接近,但具有可追溯的历史。 Basically, you: 基本上,你:

  1. add, fetch and merge each sub-repository to the toplevel repository as a regular remote, no git subtree or readtree. 将每个子存储库添加,获取并合并到顶层存储库作为常规远程,无git子树或readtree。 At first, this will polute your root dir as if it was theirs, so this is to be done at the beginning of a project's life. 首先,这会将你的根目录视为他们的根目录,所以这应该在项目开始时完成。
  2. git mv the sub-repo files to separate folders git mv将子repo文件分离到单独的文件夹中

Then: 然后:

  • upstream changes can be fetched and merged normally, but with the -Xsubtree flag to git merge. 可以正常获取和合并上游更改,但使用-Xsubtree标志进行git merge。
  • other cases should be similar. 其他情况应该类似。 I've tested pushing upstream and it works, see comment in [4]. 我已经测试了推进上游并且它有效,请参阅[4]中的评论。

References 参考

[1] From the git mailing list http://git.661346.n2.nabble.com/Bug-Files-are-losing-history-after-subtree-merge-td7597197.html [1]来自git邮件列表http://git.661346.n2.nabble.com/Bug-Files-are-losing-history-after-subtree-merge-td7597197.html

[2] From the git mailing list http://git.661346.n2.nabble.com/gsoc-Better-git-log-follow-support-td6188083.html#a6188352 [2]来自git邮件列表http://git.661346.n2.nabble.com/gsoc-Better-git-log-follow-support-td6188083.html#a6188352

[3] git log --follow has been in Google Summer of Code https://git.wiki.kernel.org/index.php/SoC2011Ideas#Better_git_log_--follow_support [3] git log --follow已经在Google Summer of Code https://git.wiki.kernel.org/index.php/SoC2011Ideas#Better_git_log_--follow_support

[4] https://saintgimp.org/2013/01/22/merging-two-git-repositories-into-one-repository-without-losing-file-history [4] https://saintgimp.org/2013/01/22/merging-two-git-repositories-into-one-repository-without-losing-file-history

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

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