git mv and refactoring in Eclipse
How do I solve this "chicken & the egg" situation?
I decided to rename a Java class in Eclipse (say, from one.java
to two.java
). Eclipse refactoring let me do that without a hitch.
Then, I went to git and typed:
git m开发者_如何学Gov myproj/src/com/ate/lib/one.java myproj/src/com/ate/lib/two.java
and received the error:
fatal: bad source, source=myproj/src/com/ate/lib/one.java, destination=myproj/src/com/ate/lib/two.java
I understand why this is happening, but if I do git mv
before refactoring, Eclipse will not like this...
What is a good approach to tackle this?
git mv
is merely a convenience method. git does not "track" renames (that is, it can detect them, but they are not recorded as an operation like an add or remove). To stage and commit your refactoring:
git rm myproj/src/com/ate/lib/one.java
git add myproj/src/com/ate/lib/two.java
git commit
git rm
tells git to stage removing the file in the index. Although you have already "removed" the file (by moving it) in the working directory, you have not told git that you want to version this removal. The difference between rm
and git rm
is that the first works on the working dir, and the second works on the index too (changes to be versioned by git).
git add
simply adds the file content at the new location.
EDIT:
I previously had git rm --cached
, out of personal habit, but apparently git rm
does not complain if the file does not exist in the working dir. git rm --cached
is still useful when you want to remove a file in versioning, but keep the file in the working dir.
精彩评论