热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用Git记录文件复制操作-RecordfilecopyoperationwithGit

WhenImoveafileingitusinggit-mvthestatusshowsthatthefilehasbeenrenamedandevenifI

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记录文件复制操作的方式与记录文件重命名/移动的方式类似,历史记录可以追溯到原始文件?

2 个解决方案

#1


102  

Git does not do rename tracking nor copy tracking, which means it doesn't record renames 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 -M option, you can request additional copy detection in changed files by using the -C option (-C implies -M), and you can request more expensive copy detection among all files with --find-copies-harder or -C -C (which implies -C, which implies -M). See the git-diff manpage.

Git不会重命名跟踪也不会复制跟踪,这意味着它不会记录重命名或副本。它的作用是重命名和复制检测。您可以使用-M选项在git diff(和git show)中请求重命名检测,您可以使用-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.renames to a boolean true value (e.g. true or 1), and you can request git to do copy detection too by setting it to copy or copies. See the git-config manpage.

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

Check also the -l option to git diff and the related config variable diff.renameLimit.

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


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

请注意,git log 在Git中的工作方式不同:此处 是路径分隔符集,其中path可以是(子)目录名。在重命名和复制检测发挥作用之前,它会过滤并简化历史记录。如果要关注重命名和副本,请使用git log --follow (目前有点限制,仅适用于单个文件)。

#2


13  

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

您可以强制Git检测复制文件的历史记录:

  • Instead of copying, switch to a new branch and move the 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。)


推荐阅读
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了Perl的测试框架Test::Base,它是一个数据驱动的测试框架,可以自动进行单元测试,省去手工编写测试程序的麻烦。与Test::More完全兼容,使用方法简单。以plural函数为例,展示了Test::Base的使用方法。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • 在Kubernetes上部署JupyterHub的步骤和实验依赖
    本文介绍了在Kubernetes上部署JupyterHub的步骤和实验所需的依赖,包括安装Docker和K8s,使用kubeadm进行安装,以及更新下载的镜像等。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文详细介绍了PHP中与URL处理相关的三个函数:http_build_query、parse_str和查询字符串的解析。通过示例和语法说明,讲解了这些函数的使用方法和作用,帮助读者更好地理解和应用。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
author-avatar
Annie千寻_535
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有