Reputation: 72687
When I run git-mv file1 file2
, I get the file moved from file1
to file2
as I'd expect. Sometimes, though, my git status
gives me 'odd' output.
When I run git-mv f1 f2
, then git status
:
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: f1 -> f2
That's what I'd expect. Other times, though, after I've committed f2
, I get:
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: f1
This generally happens after I've committed the new file. I don't know why it happens - it seems to occur at random (because generally I get the renamed: f1->f2
message, like I'm expecting).
I'd like to know why I sometimes get messages saying I've deleted the file after I run git mv
, and what steps I'd have gone through to produce this - I've just tried to reproduce, and got renamed:..
; but 10 minutes ago I got a deleted:...
on a file I'd git-mv
ed about 10 minutes before that. It's confusing me greatly.
Upvotes: 4
Views: 1663
Reputation: 15290
Git doesn't track moves in the file history like Subversion does: the history just stores the content, and git log
&c. look at the file contents and what changed to deduce whether a change was a rename. Thus, git mv f1 f2
is equivalent to:
mv f1 f2
git rm f1
git add f2
The removal of f1 and the addition of f2 are completely different changes as far as Git is concerned, so if you now do git commit f2
, you only commit the addition of f2, and the removal of f1 is still an uncommitted change. To make sure you commit both changes, do git commit
with no arguments to commit everything, or git commit f1 f2
to commit just the changes to those two files, or use git commit --interactive
or some other tool to edit the index in a more complicated way. (The index is the list of "changes to be committed".)
Upvotes: 3
Reputation: 18430
Moving files in git really is just deleting the old + creating a new file.
The "renamed: ..:" output is just a heuristic, and apparently git gets it wrong sometimes, even if it's obvious.
Upvotes: 3
Reputation: 185841
It sounds like you renamed f1
to f2
, but only committed the addition of f2
and not the removal of f1
. This can happen if you use git mv f1 f2
but then type git commit f2
, or it can happen if you type mv f1 f2
and then do something like git add .; git commit
Upvotes: 5