让 Git 确认以前移动的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1430749/
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
Getting Git to Acknowledge Previously Moved Files
提问by Michael
I've moved a bunch of files around manually without thinking, and can't find a way to get git to recognize that the files are just moved and not actually different files. Is there a way to do this other than removing old and adding the new (and thus losing the history), or redoing all the changes with git-mv?
我不假思索地手动移动了一堆文件,并且找不到让 git 识别出这些文件只是移动而不是实际上不同的文件的方法。除了删除旧的并添加新的(从而丢失历史记录)或使用 git-mv 重做所有更改之外,还有其他方法可以做到这一点吗?
采纳答案by Jorge Israel Pe?a
I think it already does this. Now, I could be wrong, but I've read that git tracks files based on their contents not based on their position in the file system or based on delta/differences. In the stack I think it shows it as if the files are being removed and the then re-added, but I think I've tried this once and it still maintained the history, due to the aforementioned way that git tracks things.
我认为它已经做到了。现在,我可能是错的,但我读过 git 根据文件的内容跟踪文件,而不是根据它们在文件系统中的位置或基于增量/差异。在堆栈中,我认为它显示的好像文件被删除然后重新添加,但我想我已经尝试过一次并且它仍然保留了历史记录,因为git跟踪事物的上述方式。
Still would be helpful for someone to verify if I'm correct or not. Sorry if I misunderstood your question.
仍然有助于某人验证我是否正确。对不起,如果我误解了你的问题。
回答by Hugo
To have git remove files that are removed or moved already, just enter
要让 git remove 已经删除或移动的文件,只需输入
git add -u
回答by CB Bailey
git
doesn't track the history of individual files and it doesn't treat moves and copies specially, that is there is no special metadata that indicates that a move or copy occurred. Instead each git commit is a complete snapshot of the working tree.
git
不跟踪单个文件的历史记录,也不特别处理移动和复制,即没有特殊的元数据表明发生了移动或复制。相反,每个 git commit 都是工作树的完整快照。
If you want to see moves in git log
you can supply -M
in addition to an option that lists which files have changed, e.g.
如果您想查看移动,除了列出哪些文件已更改的选项外,git log
您还可以提供-M
,例如
git log --summary -M
git
will look at the adjacent trees in the commit history and infer if any files where moved by each commit.
git
将查看提交历史记录中的相邻树,并推断每次提交是否移动了任何文件。
To find copies as well as renames you can use the -C
option, you can supply it twice to make git look harder for possible copy sources at the expense of some performance.
要查找副本以及重命名,您可以使用该-C
选项,您可以提供它两次,以使 git 更难查找可能的副本源,但会牺牲一些性能。
git log --summary -M -C -C
Note, that as git doesn't store file history (only commit history), even if you did git rm
and git mv
the file, you wouldn't lose any history. All changes to the path would still be recorded and visible in a git log
.
请注意,随着Git并不保存文件历史记录(仅提交历史),即使你没有git rm
和git mv
文件,你就不会丢失任何历史。对路径的所有更改仍将被记录并显示在git log
.
回答by Jakub Nar?bski
To better understand why Git does do rename detectioninstead of (more common) explicit rename tracking, and how git log
path limitingworks, you can read read Linus's ultimate content tracking toolblog post by Junio C Hamano, maintainer of Git (and references therein).
为了更好地理解为什么 Git 会进行重命名检测而不是(更常见的)显式重命名跟踪,以及git log
路径限制的工作原理,您可以阅读由 Git 维护者 Junio C Hamano 撰写的Linus 终极内容跟踪工具博客文章(以及其中的参考资料)。
回答by Kasper van den Berg
You can move/rename the new file back to its old name and path outside of git and then use git mv
to stage just the move; for example in bash:
您可以将新文件移动/重命名回 git 之外的旧名称和路径,然后仅用于暂存git mv
移动;例如在 bash 中:
mv $NEW $OLD && git mv $OLD $NEW
Its somewhat cumbersome1, especially if you have to do it by hand. But it has the advandage that it leaves other changes such as changing the namespace or the class name unstaged so that you can inspect them and only stage them when you intent to do so.
它有点麻烦1,特别是如果您必须手动完成。但它的优点是它保留了其他更改,例如更改名称空间或未暂存的类名,以便您可以检查它们并且仅在您打算这样做时才暂存它们。
1I hope to find a better alternative and will update my answer when I find it.
1我希望找到更好的替代方案,并在找到时更新我的答案。
Example:
I moved a bunch of files fom oldDir
to newDir
and started enthusiastically with some other changes. Now I want to check what other modifications were made. Using gitxargsgawkand bashresulted in the following (on a single line):
示例:我将一堆文件oldDir
移到newDir
并热情地开始了一些其他更改。现在我想检查进行了哪些其他修改。使用git xargs gawk和bash导致以下结果(在一行中):
git status --short |
gawk '/^\?\?/ && match(##代码##, /newDir\/(*.\.cs)/, a) {print "newDir/" a[1] " " "oldDir/" a[1]}' |
xargs -n 2 bash -c 'mv ##代码## ; git mv ##代码##'
Now git status
shows the renames as "Changes to be committed" and the textual modifications under "Changes not staged for commit"
现在git status
将重命名显示为“要提交的更改”和“未暂存的更改”下的文本修改
回答by Jaider
It happens to me, when I move and edit the file, it will no longer recognize it as moving file but a new one, so I lost history.
它发生在我身上,当我移动和编辑文件时,它不再将其识别为移动文件而是一个新文件,因此我丢失了历史记录。
What I do it is to create 2 separated commits, one when I move the file, and then another editing the file. In this way I keep the history.
我所做的是创建 2 个单独的提交,一个是在我移动文件时,另一个是在编辑文件时。通过这种方式,我保留了历史。