4

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 mv 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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You don't need the --cached, in fact - if the file's not present in the working tree, git rm won't complain.
@shelhamer Thanks. I understand the git add part, but why do I need to run git rm when git status already reports deleted: myproj/src/com/ate/lib/one.java ?
@Mark Longair, thanks for the tip! I guess I'm used to using --cached so put it in automatically.
@ateiob, see my updated answer. git rm tells git that you want to remove the file. git status is reporting that the file has been removed in the working directory, not that you have versioned its removal. If you have color enabled, "deleted" will be red meaning that the change has happened but is not staged, and after git rm deleted will be greed to indicate the change has been staged.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.