git 移动文件/目录但仍能轻松合并更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1739364/
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 07:20:55  来源:igfitidea点击:

Move a file/directory but still merge changes easily?

gitmergerename

提问by Chris Boyle

I've read in various FAQs that gitdoesn't explicitly track renames/moves, preferring to look for identical (or in some situations similar?) files. That's great, but will it cope with this situation: a friend's remote repository has a new feature (i18n) involving some new files at debian/po/*.po. I have my own fork of this project, and want to merge this feature but put the files at just po/*.po(I can do that as two commits, or whatever is necessary). I expect the remote repo will continue to receive updates to the feature, and I want to just merge/cherry-pick those commits and have them applied to the files in my new location. Can gitdo that, perhaps with some sort of mapping of "these files have moved over here now"? Or is it more pain than it's worth and should I just accept the slightly odd debianpath in my repo?

我已经阅读了各种git未明确跟踪重命名/移动的常见问题解答,更喜欢查找相同(或在某些情况下类似?)的文件。太好了,但它能否应对这种情况:朋友的远程存储库有一项新功能 (i18n),涉及debian/po/*.po. 我有我自己的这个项目的分支,并且想要合并这个功能,但是把文件放在那里po/*.po(我可以做两次提交,或者任何必要的东西)。我希望远程存储库将继续接收该功能的更新,我只想合并/挑选这些提交并将它们应用于我新位置的文件。可以git做到这一点,也许通过某种“这些文件现在已经移到这里”的映射?或者它比它更痛苦'debian我的回购中的路径?

回答by Pod

use git mvand everything will be AOK.

使用git mv,一切都会好起来的。

Why not just try it instead of asking? You can always reset easily in git. :)

为什么不试试而不是问呢?您始终可以在 git 中轻松重置。:)

回答by Philip Oakley

Git lacks a mechanism to indicate that you expect there to be pathrenames and updates when doing the diff between two projects/branches etc.

Git 缺乏一种机制来表明您希望在两个项目/分支等之间进行差异时进行路径重命名和更新。

There are the various filerename options available (such as -M and --patience) but can be slow.

有各种文件重命名选项可用(例如 -M 和 --patience),但速度可能很慢。

As already stated, the path renames don't affect the repository itself because it is simply a snapshot of your content (blobs) and structure (tree nodes). If all you have done is added an extra top level directory then all the trees and blobs below are unchanged and require zero additional storage. All you need is the one tree node for your commit and one tree node for the new tld. Dead easy. Git handles that part no problem.

如前所述,路径重命名不会影响存储库本身,因为它只是内容(blob)和结构(树节点)的快照。如果您所做的只是添加了一个额外的顶级目录,那么下面的所有树和 blob 都不会改变,并且需要零额外存储。您只需要一个用于提交的树节点和一个用于新 tld 的树节点。容易死。Git 处理那部分没问题。

It is only when you want to do a comparison (and any patches) that it matters. It would be nice to have say a -Poption that indicates that you expect some path renames and for the diffto thereby cope easily. It's not good seeing 200 file deletes and 200 new files ;-)

只有当您想要进行比较(和任何补丁)时才重要。最好能说出一个-P选项,表明您希望一些路径重命名,diff从而轻松应对。看到 200 个文件删除和 200 个新文件并不好;-)

Finding out how to add a -P option is another one of my 'to do' list items (I hope I get some time for it).

找出如何添加 -P 选项是我的另一个“待办事项”列表项(我希望我有时间)。

回答by Derek Mahar

Alternatively, rename the files using mv, and invoke git add --allto stage all the delete operations and add the untracked files (the new file names). Without --all, you must explicitly stage the delete operations and new file names separately. Don't use git add --update, either, as this will stage all the delete operations, but won't add the new file names.

或者,使用 重命名文件mv,并调用git add --all以暂存所有删除操作并添加未跟踪的文件(新文件名)。如果没有--all,您必须分别显式地暂存删除操作和新文件名。也不要使用git add --update,因为这将暂存所有删除操作,但不会添加新文件名。

Note that you'll likely want to perform these operations in a local branch so that you don't inadvertently pushthem to your friend's master branch or your friend doesn't pullthem into his master branch.

请注意,您可能希望在本地分支中执行这些操作,以免无意中将push它们发送到您朋友的 master 分支,或者您的朋友不会将pull它们发送到他的 master 分支。