How can I prevent git from thinking I did a rename

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

Why Git Thinks Your Files Are Copies

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:

> 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.

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.

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:

-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 how similar they need to be in order to be considered copies or renames. Your mileage may vary.

Solution 2 - Git

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:

   --no-renames
       Turn off rename detection, even when the configuration
       file gives the default to do so.

Solution 3 - Git

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 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.

Solution 4 - Git

If you have modified file A, B and C; and deleted file D, E and F; there is a chance git thinks D is renamed to A, or something similar.

The simplest solution is to split file modifications and deletions into two commits.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKit SundeView Question on Stackoverflow
Solution 1 - GitTodd A. JacobsView Answer on Stackoverflow
Solution 2 - GitmatView Answer on Stackoverflow
Solution 3 - GitBasil MusaView Answer on Stackoverflow
Solution 4 - GitHarryView Answer on Stackoverflow