3

I've accidentally used mv to rename a file which was under git. I renamed the file from lower case to uppercase keeping the name same.

mv abc.java ABC.java

I've also made changes and committed the file after that .

How do I now make an actual git rename of this file? Git bash doesn't seem to understand the difference between ABC.java and abc.java.

I'm not sure what changed on master(by others) but after moving to a branch, I'm no more able to commit my changes to the file. It says the old file index still exists.

$ git commit -m "renamed to uppercase" ABC.java fatal: Will not add file alias 'dir1/ABC.java' ('dir1/abc.java' already exists in index)

When I do git status, it shows the renamed files but doesn't let me commit the renamed files. If I try to do a delete abc.java(which is actually not present at least locally), again (I think because of case insensitivity) git deletes the new one.

If I clone a new repo out of this, the repo still pulls out the files with old name(abc.java) but all my changes until the recently failing ones are there in it.

A.R.K.S
  • 1,692
  • 5
  • 18
  • 40
  • What version of git are you using? Woudl a git rm --cached abc.java help? (followed by git -A . and git commit) – VonC Mar 04 '16 at 07:30
  • $ git rm --cached abc.java fatal: pathspec 'abc.java' did not match any files I'm not sure if git thinks abc.java is in git or not. It says its not under git but also says it is in the index. – A.R.K.S Mar 04 '16 at 15:48

5 Answers5

5

A simple git commit -m "message" without any file path parameter did the trick for me. It updated the index I think or atleast finally it was able to recognize abc is to be updated to ABC.. Thank you all..

A.R.K.S
  • 1,692
  • 5
  • 18
  • 40
0

You can do:

$ git mv {abc,ABC}.java
Agis
  • 32,639
  • 3
  • 73
  • 81
  • could you please explain a bit on what the above expression means. I'm a bit scared to use mv without understanding. I shall update on the versions soon – A.R.K.S Mar 04 '16 at 08:46
  • @AshwiniR *"hey git, please rename the file `abc.java` to `ABC.java`"* – Agis Mar 04 '16 at 09:11
  • If it's same as git mv abc ABC then I can't do that. It says abc is not under source control – A.R.K.S Mar 04 '16 at 15:21
0

If you want to know in which commit you made changes to abc.java , you can use :

git log --follow abc.java

in the results, you can find the commit in which you renamed it.

For restoring the file to its original name, you can use git reset with the --soft flag, which will unstage your changes, without throwing them away, enabling you to make your correction and commit them again.

I recommend you to read the following links on git reset, to get a good grasp on it, before trying to do something that can be dangerous.

Git reset tutorial by Atlassian

Git reset Demystified

Farhad
  • 12,178
  • 5
  • 32
  • 60
0

Renaming foldername to folderName on case insensitive file systems

Simple renaming with a normal mv command(not git mv) won’t get recognized as a filechange from git. If you try it with the ‘git mv’ command like in the following line

git mv foldername folderName

If you’re using a case insensitive filesystem, e.g. you’re on a Mac and you didn’t configure it to be case sensitive, you’ll experience an error message like this one:

fatal: renaming ‘foldername’ failed: Invalid argument

And here is what you can do in order to make it work:-

git mv foldername tempname && git mv tempname folderName

This splits up the renaming process by renaming the folder at first to a completely different foldername. After renaming it to the different foldername the folder can finally be renamed to the new folderName. After those ‘git mv’s, again, do not forget to add and commit the changes. Though this is probably not a beautiful technique, it works perfectly fine. The filesystem will still not recognize a change of the letter cases, but git does due to renaming it to a new foldername, and that’s all we wanted :)

akshay_rahar
  • 1,661
  • 2
  • 18
  • 20
0

If you need to commit files renaming, just capitalization, and you have a problems with staging/commiting, as option

  1. Change capitalization and add "1" to the end (or other symbol)
  2. Stage (without commit) = result "R" - renaming that files
  3. Remove "1" from the end
  4. Stage (without commit) = result "R" - renaming that files (only capitalization now)
  5. Do commit.
SilverNak
  • 3,283
  • 4
  • 28
  • 44
user4i
  • 71
  • 3