Git-将文件标记为新文件,而不是移动/复制 [英] Git - Mark file as new instead of moved/copied

查看:63
本文介绍了Git-将文件标记为新文件,而不是移动/复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法强迫git将文件视为新文件,而不是移动/复制文件?

Is there any way to force git to consider a file as new instead of moved/copied?

用例:

  1. 我有一个大文件 index.js
  2. 我正在将该文件中的一小类分解为 Helper.js (仅占 index.js 的10%)并重命名 index.js MyLib.js . MyLib.js 将进行一些小的更改,这些更改与从 Helper.js 导入符号有关.
  3. 我需要将 index.js 重新创建为一个新的两行文件,该文件仅从 Helper.js MyLib.js中重新导出符号.
  1. I have a large file, index.js
  2. I'm factoring out a small class from that file into Helper.js (only 10% of the index.js) and renaming index.js to MyLib.js. MyLib.js will have some minor changes related to importing symbols from Helper.js.
  3. I need to recreate index.js as a new, 2-line file, that only re-exports symbols from Helper.js and MyLib.js.

我希望提交历史记录将 index.js 重命名为 MyLib.js ,并将两行 index.js 视为新,但git相反将 MyLib.js 视为全新内容,而将 index.js 视为丢失了99%的内容,仅丢失了这两行.

I want the commit history to record renaming index.js to MyLib.js and treat the 2-line index.js as new, but git instead treats MyLib.js as completely new, and index.js as having lost 99% of its contents, down to only those 2 lines.

推荐答案

简短的答案是:不, git 不允许您存储有关文件移动方式的信息.

The short answer is : no, git does not allow you to store information about how files were moved.

git 跟踪内容,而不是 diff .

git 显示信息时:

$ git diff --name-status HEAD^ HEAD
M    fileA          # fileA has been modified
R    oldB -> fileB  # fileB has been renamed
A    fileC          # fileC has been created

通过比较两个提交的内容,

它实际上已经计算了此信息.它尚未存储以下信息:实际上是从fileA复制了fileC,并且已将fileA重新创建为新文件".

it actually has computed this information by comparing the contents of the two commits. It has not stored the information : "actually fileC was copied from fileA, and fileA was re-created as a new file".

如果两个文件在两次提交中都具有相同的名称,则 git diff 将始终计算此文件已被修改".

If two files have the same name in both commit, git diff will always compute "this file has been modified".

选项1:保留您的历史记录,并加以保留.

Option 1 : keep the history you have, and live with it.

选项2:您可以尝试两次提交

Option 2 : you could try to do it in two commits

  • 创建第一个提交,唯一的操作是将文件 index.js 重命名为 MyLib.js

  • 如果您需要工作"的代码(例如,以便可以在此提交上运行单元测试或集成测试),请更新其他模块,以便它们导入 MyLib.js 而不是 index.js

创建第二个提交,在其中应用您实际想要查看的修改

create a second commit, where you apply the modifications you actually want to see

  • 将一个小类从 MyLib.js 提取到 Helper.js
  • 用两行和导出的符号创建一个新的 index.js 文件,
  • 如果您在第一次提交中修改了导入,请在第二次提交中再次修改它们.

使用选项2,某些 git 命令(例如 git rebase git log --follow )会检测到其中的重命名步骤回购的历史记录,因为他们总是一次检查一次提交的历史记录.

With option 2, some git commands (git rebase, or git log --follow for example) would detect the renaming step in the repo's history, because they always inspect the history one commit at a time.

其他一些命令(不是每个提交差异的提交,而是全局"差异)仍将作为选项1.

Some other commands, that do not look at a commit per commit diff but at a "global" diff, would still behave as option 1.

例如:如果您打开一个合并请求(请考虑 github gitlab Azure Devops ...),则合并请求界面仍会为您提供:

For example : if you open a Merge Request (think github, gitlab, Azure Devops ...), the Merge Request interface would still present you with :

  • 文件 index.js 已被修改
  • 文件 MyLib.js 是一个全新的文件
  • file index.js has been modified,
  • file MyLib.js is an entirely new file

这篇关于Git-将文件标记为新文件,而不是移动/复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆