unable to commit a file, accidentally renamed with mv

≯℡__Kan透↙ 提交于 2019-12-23 01:12:33

问题


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.


回答1:


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..




回答2:


You can do:

$ git mv {abc,ABC}.java



回答3:


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




回答4:


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 :)



来源:https://stackoverflow.com/questions/35790113/unable-to-commit-a-file-accidentally-renamed-with-mv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!