如何防止 git 认为我进行了重命名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15031576/
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
How can I prevent git from thinking I did a rename
提问by Kit Sunde
I have two files index.html
and template.html
. I moved most of index.html
into template.html
and now git thinks that I did a rename when I add both files. Is it possible to prevent this in specific cases?
我有两个文件index.html
和template.html
. 我最感动的index.html
到template.html
现在的git认为我做了一个重命名当我添加这两个文件。在特定情况下是否可以防止这种情况?
采纳答案by mat
There is an "accepted" answer, but it does not give any hint on how to answer the question.
有一个“接受”的答案,但它没有给出任何关于如何回答问题的提示。
The correct answer, from git-log(1) and git-diff(1) is:
来自 git-log(1) 和 git-diff(1) 的正确答案是:
--no-renames
Turn off rename detection, even when the configuration
file gives the default to do so.
回答by Todd A. Jacobs
Why Git Thinks Your Files Are Copies
为什么 Git 认为你的文件是副本
Git tracks content, not filenames. As a result, if two files have substantially similar content, git will think you copied or renamed the file. If you read git-log(1), you will learn:
Git 跟踪内容,而不是文件名。因此,如果两个文件的内容基本相似,git 会认为您复制或重命名了该文件。如果您阅读 git-log(1),您将了解到:
The similarity index is the percentage of unchanged lines, and the dissimilarity index is the percentage of changed lines. It is a rounded down integer, followed by a percent sign. The similarity index value of 100% is thus reserved for two equal files, while 100% dissimilarity means that no line from the old file made it into the new one.
相似指数是不变行的百分比,相异指数是变化行的百分比。它是一个向下舍入的整数,后跟一个百分号。因此,100% 的相似性指数值保留给两个相等的文件,而 100% 的不相似性意味着旧文件中没有行进入新文件。
So, assuming your similarity index is 100%, git will think that's a copy. Your best bet is to add a sensible log message or note (see git-notes(1) for more on that) to explain what's going on if you don't think git is doing the right thing.
所以,假设你的相似度指数是 100%,git 会认为这是一个副本。最好的办法是添加一个合理的日志消息或注释(有关更多信息,请参阅 git-notes(1))来解释如果您认为 git 做的事情不对,会发生什么。
Adjusting the Similarity Index
调整相似度指数
You might also try adjusting the values git uses for considering something a copy or rename. The manual for git-log(1) says:
您也可以尝试调整 git 用于考虑复制或重命名的值。git-log(1) 的手册说:
-M[<n>], --find-renames[=<n>]
If generating diffs, detect and report renames for each commit. For
following files across renames while traversing history, see --follow. If
n is specified, it is a threshold on the similarity index (i.e. amount
of addition/deletions compared to the file's size). For example, -M90%
means git should consider a delete/add pair to be a rename if more than
90% of the file hasn't changed.
-C[<n>], --find-copies[=<n>]
Detect copies as well as renames. See also --find-copies-harder.
If n is specified, it has the same meaning as for -M<n>.
Again, this won't help you if the files are mostly similar, but you can certainly use these values to tune howsimilar they need to be in order to be considered copies or renames. Your mileage may vary.
同样,这不会帮助你,如果这些文件大多是相似的,但你当然可以使用这些值来调整如何,他们需要类似是为了被认为是副本或重命名。你的旅费可能会改变。
回答by Basil Musa
If you are in a moment just before a commit and "you feel bad that git went mad", then just undo the addition of the ambiguous file git thought you renamed, perform a commit, then add the ambiguous file again and commit:
如果您在提交之前的某个时刻并且“您感到 git 发疯了”,那么只需撤消添加 git 认为您重命名的歧义文件,执行提交,然后再次添加歧义文件并提交:
git reset ambiguous_file_git_thought_you_renamed
git commit
git add ambiguous_file_git_thought_you_renamed
git commit
This worked for me.
这对我有用。
Double check no renaming took place:
仔细检查没有重命名发生:
git diff --name-status -C HEAD^^ HEAD
M ambiguous_file_git_thought_you_renamed
M original_file
"M" at the beginning means modified, "R" mean Renamed. Notice no Renamed exists here.
开头的“M”表示修改,“R”表示重命名。注意这里不存在重命名。
回答by wyu
To get around git thinking it's a rename in "one" commit.
为了绕过 git 认为它是“一个”提交中的重命名。
- Stage and commit your changes that don't trigger the erroneous rename
- Stage the remaining files that caused git to think it was rename and commit with
git commit --amend
- 暂存并提交不会触发错误重命名的更改
- 暂存导致 git 认为它被重命名并提交的剩余文件
git commit --amend