I am using Git and manually renamed a file that I had added to the repository. Now, I have added the "new" file which I renamed to the repository but Git complains that the "old" file has been deleted. So how can I make Git forget about the old file? Better yet, how do I tell Git that the "new" file really is the "new" file so that I can keep the change history intact?
-
4`git mv old new` is essentially just `git rm --cached old; mv old new; git add new`, with some extra smarts. Git identifies renames by looking for similar content, so it will detect them whether or not you used `git mv`. That is, there's no way to *tell* it that those two files correspond. – Cascabel Jan 17 '11 at 03:29
-
1Possible duplicate of [How to make git mark a deleted and a new file as a file move?](http://stackoverflow.com/questions/433111/how-to-make-git-mark-a-deleted-and-a-new-file-as-a-file-move) – Waldir Leoncio Jun 14 '16 at 15:34
-
2Weird that almost 8 years has past and git is still not able to detect the rename – RSFalcon7 Nov 14 '19 at 13:34
-
Git rename detection depends on a rename threshold git config setting. If you rename a file and also change the file contents beyond a certain threshold, git will not consider it a rename. Here's a starting point https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---find-renamesltngt – Devin Rhode Feb 25 '22 at 03:50
5 Answers
There is no problem. Simply git rm old
or even git add -A
and it will realize that it is a rename. Git will see the delete plus the add with same content as a rename.
You don't need to undo, unstage, use git mv
etc. git mv old new
is only a short hand for mv old new; git rm old; git add new
.

- 23,685
- 6
- 47
- 47
-
11If the old file's already gone, you have to use `git rm --cached old`, since there's no file in the work tree to remove. – Cascabel Jan 17 '11 at 03:26
-
6What if you move the file and change a few lines, then `git add -A`? Is Git smart enough to know that a _similar_ file is the same as the old file? – Phil Mar 26 '12 at 03:14
-
1Moreover, if there are some changes in the body of the files such as different #include statements due to file name change, git still understands this! – Vlad Sep 24 '16 at 00:48
-
git didn't seem to understand the renamed changed file when I tried - the other answer worked though. – mikemaccana May 21 '21 at 18:28
First, cancel your staged add for the manually moved file:
$ git reset path/to/newfile
$ mv path/to/newfile path/to/oldfile
Then, use Git to move the file:
$ git mv path/to/oldfile path/to/newfile
Of course, if you already committed the manual move, you may want to reset to the revision before the move instead, and then simply git mv
from there.

- 507,862
- 82
- 626
- 550
-
7
-
-
1Just noting that I initially saw `fatal: bad source, source=oldfile destination=newfile`, then realized the instructions in this answer already account for this by renaming the already-renamed file back to the original :) – mindthief May 13 '21 at 02:04
This has to be automated. I never remember git until I commit, and I don't want to.
#!/usr/bin/env python3
# This `git rm` wrapper respects the `--cached` option (like `git rm`).
import sys
import subprocess
if "--cached" in sys.argv:
dest = sys.argv[-1]
rest = sys.argv[1:-1]
subprocess.check_call(["git", "add", dest])
subprocess.check_call(["git", "rm"] + rest)
else:
subprocess.check_call(["git", "mv"] + sys.argv[1:])
I don't want to "remember git mv", because git mv
adds hidden state behind git diff
that is implicitly added to the next commit, even if you explicitly commit something totally unrelated. I know, it's the so called "staged" state, but I don't like it. I like to commit without surprises.

- 5,520
- 4
- 32
- 38
If this error occurs, it probably means that the file you are trying to move is not originally tracked by git or the directory you are trying to move the file into is not a git tracked directory.
-
As the OP mentioned, he moved the file which he previously had under version control already. What you describe, is the situation when trying to `git mv` a file, which is not under version control yet (so a file which hasn't been added using `git add` before). Nowadays, the git error message will be very explicit about the file not being checked into vc yet. – Kim Jun 07 '19 at 13:31