Why can git log lose track of a file after a git mv rename?

March 7, 2025

“So do you loose the file history?” Will Hancock asked on Stack Overflow. It's a natural question: after renaming a file, can you still find its earlier changes? In 2013, Adam Nofsinger replied, “Well, yes and no.” He pointed to a discussion by Linus Torvalds about whether version control tools should track file identity. Two years later he came back to qualify his answer: “depending on your git client and its options”. Even the same client can fail to recognize the old file if you rename it and substantially rewrite it at the same time.123

What does git mv do beyond an ordinary mv?

Suppose you've committed draft.txt and have a clean working tree. Running git mv draft.txt guide.txt moves the file and stages both path changes for the next commit. Renaming it with ordinary mv and then running git add -A -- draft.txt guide.txt produces the same snapshot.4

A Git commit records the contents at each path at that moment. When comparing two versions, Git can match a deleted path with a newly added one based on their contents and display the change as a rename. The commit contains no permanent record of where a file came from.5

Why would the same git mv show two different histories?

I ran a comparison in temporary repositories using Git 2.43.0. Each trial started with a commit called add draft and a file containing three lines: one, two, and three. Each time, I renamed it to guide.txt, ran git diff --cached -M --name-status before committing to see how Git identified the change, and committed it as publish guide.

ChangeComparison of staged changesResult of running git log --follow -- guide.txt after committing
Rename with git mv aloneR100, identical contentsBoth publish guide and add draft appear
Rename with ordinary mv, then stage both pathsAlso R100Both commits appear again
Rename with git mv, replace the three lines with alpha, beta, and gamma, then stage the editsD for the old path, A for the new pathOnly publish guide appears

The third trial is the interesting one. Even though I'd used git mv, Git displayed a deletion and an addition. By default, -M uses a 50% content similarity threshold to detect renames. Even when it fails to match the files, git show HEAD^:draft.txt still returns the original three lines in this two-commit example. The old version is in the parent commit. Looking up the log by the new path just didn't connect the two.5

In practice, try git log --follow -- new-path first. If you've moved an entire directory, pick one file to investigate because --follow handles only one file at a time. If you also plan to rewrite the content, put the move and the edits in separate commits. It will be easier to see which step moved the file and which changed what it says.6

Footnotes

  1. Hancock, W. (2013, April 12). So do you loose the file history? [Comment on “What’s the purpose of git mv?”]. Stack Overflow. Opening line of the comment. ↩

  2. Nofsinger, A. (2013, May 7). Well, yes and no. [Comment on “What’s the purpose of git mv?”]. Stack Overflow. First reply to Hancock, including the opening line and mailing list source. ↩

  3. Nofsinger, A. (2015, March 6). Reply to Will Hancock on tracing history past a rename [Comment]. Stack Overflow. Follow-up on clients, options, and content similarity. ↩

  4. Git. (n.d.). git-mv. Git Documentation. Description. ↩

  5. Git. (n.d.). gitdiffcore. Git Documentation. diffcore-rename: For Detecting Renames and Copies. ↩ ↩2

  6. Git. (n.d.). git-log. Git Documentation. The --follow option. ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-03-07). Why can git log lose track of a file after a git mv rename?. https://caiborui.com/blog/2025-03-07-zh-git-mv-rename-history-entrans

@misc{borui2025-7fd2065b,
  author = {borui},
  title = {{Why can git log lose track of a file after a git mv rename?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-03-07-zh-git-mv-rename-history-entrans}
}