Disable rename detection during the Git commit to preserve history

Viewed 413

I am new in Git and I am looking for a way to disable the automatic rename detection during operations like add, rm, commit.

It is mainly for reasons of clarity and maintenance of a computing code. Even if a new file B is quite similar to another existing Git file A at this moment, I would like to record B separately with no history coming from A.

With Git v2.31.1, it seems to work for add/rm operations thanks to the option "renames = 0". The .gitconfig file :

[diff]
    tool = tkdiff
    renames = 0
[merge]
    renames = 0
[difftool]
    prompt = false

Here is the situation I would like to avoid.

> cat FileA.txt 
This is a first line
Values are 2.D-4 1.D-1 5.D-3
This is the end

> git init
> git add FileA.txt
> git commit -m " Step 1 "

> cat FileB.txt
This is a first line
Values are 1.D-4 4.D-1 9.D-3
This is the end

> git add FileB.txt
> git rm FileA.txt
> git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    FileA.txt
    new file:   FileB.txt

> git commit -m " Step 2 "
 [master 4fe24cf]  Step 2
   1 file changed, 1 insertion(+), 1 deletion(-)
   rename FileA.txt => FileB.txt (56%)

> git log --oneline FileB.txt
  4fe24cf (HEAD -> master)  Step 2

> git log --oneline --follow FileB.txt
  4fe24cf (HEAD -> master)  Step 2
  ccf5076  Step 1

I do not know if git commit relies on git diff to compute similarity indexes, but options used for git diff, like "renames = 0" or "find-renames=100", have no impact here. And I did not find relevant options to test in the Git documentation for the commit command.

How can I proceed ? are there any others options ?

1 Answers

git does not store any information about how files were moved ; it tracks content, not diffs.

When git displays the information :

$ git diff --name-status HEAD^ HEAD
R    FileA.txt -> FileB.txt

it actually has computed this information by comparing the contents of the two commits. It has not stored the information : "actually, the user deleted FileA.txt, and FileB.txt was re-created as a new file".


As far as committing goes : there are no options there ; I don't think you can avoid the message printed by git commit.

Commands, like git diff or git log, which deal with inspecting and displaying diffs, have a --no-renames option :

# will not look for renames at all :
git diff --no-renames

You may be fooled by the syntax for -M | --find-renames :

Without a % sign, the number is to be read as a fraction, with a decimal point before it. I.e., -M5 becomes 0.5, and is thus the same as -M50%. Similarly, -M05 is the same as -M5%. To limit detection to exact renames, use -M100%.

# So :
#  '100' will get translated to '.100' which is 10% ... :/
git diff -M100     # same as : git diff -M10%
#   add a '%' after the 100 :
git diff -M100%

Extra notes :

The difference between --find-renames=100% and --no-renames is :
if a file is moved (renamed without any modification)

  • the former will display it as a Renamed file
  • the latter will display it as one Added file/one Deleted file

If you use git log --follow : --no-renames is ignored, but renaming detection takes into account -M. For example : -M100% will only "follow" accross commits where that file was moved without modification.
Do note, once again, that --follow is computed after the facts, and is only a heuristic.

Related