Video in progress

Strong Middle 🌿 Git

You moved a class to another package and changed it a bit. How does Git detect a rename, and why does `git log` stop showing the old history?

Git does not store a rename separately: a path disappears from the tree and another appears, and then it guesses "this is a rename" by content similarity (50% by default). git log -- new/path looks only at that path, and for the old history you need --follow.

📖 Answer

Many people think git mv records rename information. It does not: it is just shorthand for mv + git rm + git add. There is no such thing as a "rename" in a commit.

git log --oneline -- src/main/java/uz/shop/billing/InvoiceService.java
# only the commits after the move

git log --follow --oneline -- src/main/java/uz/shop/billing/InvoiceService.java
# the history in the old package shows up too

--follow works with only one file. For git diff and git log -p, rename detection is on by default (since Git 2.9), and the threshold can be changed: git diff -M80%.

Where it breaks:

  • if you move a file and rewrite half of it in the same commit, the similarity drops below 50%, and Git says "one was deleted, another was added". The history breaks, and during a merge it gives a conflict without noticing the rename;
  • if thousands of files are moved (for example, a javax → jakarta migration), Git stops looking for renames and warns because it exceeded diff.renameLimit.

So the rule is simple: move in one commit, change in the next. IntelliJ's Move refactoring can mix the two as well, so before committing, check that git status shows a renamed: entry.

git blame -C also tracks lines copied from another file. The two work well together when digging through history.