antox antox - 1 month ago 7
Git Question

Why doesn't Git detect rename?

Reading around I found that using

git mv
for rename should make Git able to detect the renaming, but in my Git (2.7.4) it doesn't seem to work.

Here is what I ran:

~ $ mkdir git_tests
~ $ cd git_tests/
~/git_tests $ git init
Inizializzato un repository Git in /home/git_tests/.git/
~/git_tests $ echo "hello" > readme
~/git_tests $ git add readme
~/git_tests $ git status
Sul branch master

Commit iniziale

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: readme

~/git_tests $ git mv readme leggimi
~/git_tests $ git status
Sul branch master

Commit iniziale

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: leggimi


Is there something that I'm missing?

Answer

In complement to the other answers: using git mv is not what helps Git detect renames. Git detects them after the fact, using heuristics, so using git mv old new or git rm --cached old; mv old new; git add new do the same thing.

(Well, not exactly if you have unstaged changes in old, i.e. if the file is tracked by has changed not staged in the index. Then git mv old new would keep the file tracked and keep the unstaged changes unstaged while git add new would stage everything).