使用 Git 记录文件复制操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1043388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:38:30  来源:igfitidea点击:

Record file copy operation with Git

gitfilecopy

提问by Hexdoll

When I move a file in git using git-mv the status shows that the file has been renamed and even if I alter some portions it still considers to be almost the same thing (which is good because it lets me follow the history of it).

当我使用 git-mv 在 git 中移动文件时,状态显示该文件已重命名,即使我更改了某些部分,它仍然认为几乎相同(这很好,因为它让我可以跟踪它的历史) .

When I copy a file the original file has some history I'd like to associate with the new copy.

当我复制文件时,原始文件有一些历史记录,我想与新副本相关联。

I have tried moving the file then trying to re-checkout in the original location - once moved git won't let me checkout the original location.

我尝试移动文件,然后尝试在原始位置重新结帐 - 一旦移动,git 将不会让我结帐原始位置。

I have tried doing a filesystem copy and then adding the file - git lists it as a new file.

我试过做一个文件系统复制,然后添加文件 - git 将它列为一个新文件。

Is there any way to make git record a file copy operation in a similar way to how it records a file rename/move where the history can be traced back to the original file?

有什么方法可以让 git 以类似于记录文件重命名/移动的方式记录文件复制操作,其中历史可以追溯到原始文件?

采纳答案by Jakub Nar?bski

Git does not do rename tracking nor copy tracking, which means it doesn't recordrenames or copies. What it does instead is rename and copy detection. You can request rename detection in git diff(and git show) by using the -Moption, you can request additional copy detection in changed files by using the -Coption (-Cimplies -M), and you can request more expensive copy detection among all files with --find-copies-harderor -C -C(which implies -C, which implies -M). See the git-diffmanpage.

Git 不进行重命名跟踪或复制跟踪,这意味着它不记录重命名或副本。它所做的是重命名和复制检测。您可以使用该选项在git diff(and git show) 中请求重命名检测-M,您可以使用该-C选项 (-C暗示-M)请求在更改的文件中进行额外的复制检测,并且您可以使用--find-copies-harder或请求在所有文件中进行更昂贵的复制检测-C -C(这意味着-C,这意味着-M)。请参阅git-diff联机帮助页。

You can also configure git to always do rename detection by setting diff.renamesto a boolean true value (e.g. trueor 1), and you can request git to do copy detection too by setting it to copyor copies. See the git-configmanpage.

您还可以通过将 git 设置diff.renames为布尔真值(例如true1)来将 git 配置为始终进行重命名检测,并且您也可以通过将其设置为copy或来请求 git 进行复制检测copies。请参阅git-config联机帮助页。

Check also the -loption to git diffand the related config variable diff.renameLimit.

还要检查-l选项git diff和相关的配置变量diff.renameLimit



Note that git log <pathspec>works differently in Git: here <pathspec>is set of path delimiters, where path can be a (sub)directory name. It filters and simplifies history beforerename and copy detection comes into play. If you want to follow renames and copies, use git log --follow <filename>(which currently is a bit limited, and works only for single file).

请注意,它git log <pathspec>在 Git中的工作方式不同:这里<pathspec>是一组路径分隔符,其中路径可以是(子)目录名称。它会重命名和复制检测发挥作用之前过滤和简化历史记录。如果您想进行重命名和复制,请使用git log --follow <filename>(目前有点受限制,仅适用于单个文件)。

回答by Robert Pollak

2020-05-19: The following solution has the advantages of not changing the log of the original file, not creating a merge conflict, and being shorter.

2020-05-19:以下方案的优点是不改变原文件的日志,不产生合并冲突,更短。

You can force Git to detect the history of the copied file in three commits:

您可以强制 Git 在三个提交中检测复制文件的历史记录:

  • Instead of copying, switch to a new branch and movethe file to its new location there.
  • Re-add the original file there.
  • Merge the new branch to the original branch with the no-fast-forward option --no-ff.
  • 而不是复制,切换到一个新分支并将文件移动到那里的新位置。
  • 在那里重新添加原始文件。
  • 使用 no-fast-forward 选项将新分支合并到原始分支--no-ff

(Credits go to Raymond Chen.)

(版权归Raymond Chen 所有。)



The former solution had four commits:

前一个解决方案有四个提交:

  • Instead of copying, switch to a new branch and movethe file to its new location there.
  • Switch to the original branch and rename the file.
  • Merge the new branch into the original branch, resolving the trivial conflict by keeping both files.
  • Restore the original filename in a separate commit.
  • 而不是复制,切换到一个新分支并将文件移动到那里的新位置。
  • 切换到原始分支并重命名文件。
  • 将新分支合并到原始分支中,通过保留两个文件来解决琐碎的冲突。
  • 在单独的提交中恢复原始文件名。

(Solution taken from https://stackoverflow.com/a/44036771/1389680.)

(解决方案取自https://stackoverflow.com/a/44036771/1389680。)