如何复制Git文件并让git blame识别其原始提交历史?
在Git中复制文件后保留原始历史的方法
直接复制文件并提交后,Git无法自动识别新文件与原始文件的关联,导致git blame只能显示复制操作对应的提交,无法追溯到原始文件的修改历史,就像示例中的情况:
$ git log --oneline --graph --name-status master * 70f03aa (master) COpying file straight | A new_file.txt * efc04f3 (first) First commit for file A hello_world.txt $ git blame -s master -- new_file.txt 70f03aab 1) I am here 70f03aab 2) 70f03aab 3) Yes I am
方法一:使用git blame的-C参数追踪复制历史
git blame的-C参数会让Git检测文件内容的来源,识别出文件是从其他文件复制而来的,从而显示原始文件的历史记录。
执行命令:
git blame -C -s master -- new_file.txt
执行后即可看到原始文件的提交历史:
^efc04f3 1) I am here ^efc04f3 2) ^efc04f3 3) Yes I am
如果需要更深度的检测(比如复制后对文件做过修改),可以使用三次-C参数,Git会跨提交检测文件内容的来源:
git blame -CCC -s master -- new_file.txt
方法二:提交时标记文件来源(辅助手段)
如果希望在提交阶段就明确文件的复制关联,可以在提交信息中说明来源,比如:
git commit -m "Copy hello_world.txt to new_file.txt (origin: hello_world.txt)"
这种方式不会让Git自动追踪关联,但能帮助团队成员快速理解文件的由来,配合git blame -C使用效果更佳。
内容的提问来源于stack exchange,提问作者eftshift0
相关产品推荐
相关产品推荐

