git,在保留历史记录的同时移动/重命名文件的可靠方法 [英] git, sure-fire way to move/rename files while keeping the history

查看:104
本文介绍了git,在保留历史记录的同时移动/重命名文件的可靠方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道现有问题"看起来很相似",所以让我在问我之前先对它们进行总结.

I know there are "lots" of existing questions that looks similar, so let me summarize them before asking mine.

  • 是否可以在git中移动/重命名文件并保持其历史记录?是不可能".
  • 在保留历史记录的同时移动文件的结论是,Git 可能将其视为移动,或者可能仍将其视为删除+添加.
  • The answer to Is it possible to move/rename files in git and maintain their history? is, "it is not possible".
  • The conclusion for git moving file while keeping history is, Git may see it as a move, or it may still treat it as a delete + addition.

第一个答案,我不同意,因为我之前已经做过.第二个答案是我问这个问题的原因.即

The answer to the first one, I don't agree, because I've done that before. The answer to the second one is the reason why I'm asking this question. I.e.,

我发现我一直在执行git mv,但有时将其视为移动/重命名,有时将其视为删除+添加.因此,我想知道如何使它始终保持移动/重命名状态?

I found I am doing git mv all the times, but sometime it is treated as move/rename, and sometime it is treated as delete + addition. Thus, I want to know how I can make it always a move/rename?

为例,在底部,我们可以看到一些举动/重命名大小写,例如easygenapi/tf-varcaser.go → tf-varcaser.go.请注意,这样的移动是在文件夹之间/之间!也就是说,我 了!

Take this one as an example, at the bottom, we can see several move/rename cases, like easygenapi/tf-varcaser.go → tf-varcaser.go. Note that such moves are across/between the folders! I.e., I did it!

但是还有许多其他情况git mv被视为删除+添加,显示在完全相同的更改日志中.同样,我一直在做git mv.为什么git表现不同?

But there are many other cases git mv were treated as delete + addition, showing in the exactly same change log. Again, I am doing git mv all the times. Why git behave differently?

在保留历史记录的同时,是否有任何 sure-fire 方法来移动/重命名git文件?

Is there any sure-fire way to move/rename git files while keeping the history?

推荐答案

tl; dr;否

更长的版本: 以我的经验,只要文件未修改,git就会很好地检测移动/重命名. Git使用试探法来尝试并找到移动位置.如果有几个太相似的文件,或者如果文件在移动过程中被修改,导致其与原始文件相差太远,则可能会愚弄它.

Longer version: In my experience, git is very good at detecting the move/rename as long as the file is unmodified. Git uses heuristics to attempt and locate the move. It can be fooled by having several files that are too similar, or if the file has been modified during the move, causing it to be too dissimilar from its original.

我发现做到这一点的最好方法是进行多阶段提交,将您的所有动作分成一个提交,然后再进行另一个更改.例如...

The best way I have found to do this is to do multi-stage commits, separating all of your moves into one commit, followed by the changes in another. For example...

git mv foo.txt bar.txt
git commit

... modify bar.txt ...

git add bar.txt
git commit

它不能保证正确检测到您的举动,因为当有多个候选者时,它仍然会造成混乱.但是,它对我来说效果很好,并能解决大多数情况.

It will not guarantee your move is detected correctly, as it can still get confused when there are multiple candidates. However it has worked very well for me and catches the majority of cases.

这篇关于git,在保留历史记录的同时移动/重命名文件的可靠方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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