13


For example, I have a file named FOOBar.java that I want to rename to FooBar.java. After trying lots of stuff, I get the error:

enter image description here

Error:error: pathspec 'app/src/main/java/blahblah/FooBar.java' did not match any file(s) known to git.


Things I have tried (not working, all produce the same error):

from Android Studio:
  • deleting FOOBar.java, re-creating FooBar.java, adding/committing with Git
  • refactoring/renaming the file, adding/committing with Git
  • File --> Invalidate Caches / Restart..., then trying one of the above
  • Rebuild Project before/after any of the above

in the file system:

  • deleting the .gradle folder in the project folder, then trying one of the above in Android Studio

from the Git command line:

  • git mv FOOBar.java FooBar.java --force then git commit FooBar.java -m 'renamed from FOOBar.java to FooBar.java'
Ian Campbell
  • 2,678
  • 10
  • 56
  • 104

8 Answers8

32

Windows’ file system is mostly case-insensitive, so you cannot rename a file by just changing its capitalization. Instead, you will have to use a temporary name in between.

Try the following from the command line:

git mv FOOBar.java FooBar.java.new
git mv FooBar.java.new FooBar.java
git commit -m 'Rename file'
poke
  • 369,085
  • 72
  • 557
  • 602
  • Thanks @poke! I am using Windows, however when doing this I am still getting the same `error: pathspec 'FooBar.java' did not match any file(s) known to git` error... – Ian Campbell Apr 07 '15 at 06:50
  • 1
    Do you get that inside the command line or in Android Studio? Try closing Android Studio while you do that; and also, are you in the correct directory? And what does `git status` tell you? – poke Apr 07 '15 at 06:51
  • 1
    Thanks @poke, that did it! I closed Android Studio, renamed as you mention above, then was able to commit and push from the Git command-line successfully. – Ian Campbell Apr 07 '15 at 07:01
  • 1
    Also, to add to what poke said: Make sure the parent directories of the problem file(s) are named exactly as they appear according to Git (eg, look at what Github sees). For example, you could have a directory named "Folder" in windows, and git will happily continue to track it as if it was "folder" if it wasn't moved/renamed properly. To fix: git mv folder/ folder_new git mv folder_new/ Folder git commit -m 'Rename file' – sersun Jul 18 '15 at 05:31
11

I found out a very simple solution to this. Say you need to rename a java package "Activities" to "activities". This package contains several files and sub-packages

Follow the sequence of steps

  1. Refactor "Activities" to "ActivitiesTemp" (Shift + F6)
  2. Commit and push these changes
  3. Refactor again from "ActivitiesTemp" to "activities"
  4. Commit and push these changes

Thats All Folks !

Sanket Berde
  • 6,555
  • 4
  • 35
  • 39
  • Similar to what I did. Rename all files. Copy elsewhere. Deleted high level folder and delete all files. Commit. Then copy files back in and add and commit. Yes, probably loses commit history. But at least it works. – maxweber Nov 17 '15 at 16:08
7

I had a similar problem and i fixed it changing git config:

git config core.ignorecase false
Kaizie
  • 3,748
  • 4
  • 20
  • 19
  • This will not work as expected. If you rename the file from FOO.xyz to Foo.xyz then git will assume you have added a new file called Foo.xyz but will not delete the FOO.xyz. So you will end up with both the files FOO.xyz and Foo.xyz on you server repo. – Vinayak Bevinakatti Jun 06 '17 at 17:53
3

Rename the file back to the original one, then rename it to a different name, then back to the one with the correct capitalization. Git will not throw the bug anymore, it's the same solution as Poke pointed out, but using android studio.

Example:

Created FOOBar class.
Renamed it to FooBar and then got the error.
Rename it back to FOOBar.
Rename to FooBarTest.
Rename to FooBar.
Git works now.
Daniel Silva
  • 960
  • 10
  • 15
3

I also faced the almost same situation, in my case I have created a file and added to git (using git add ), after adding to git I have renamed the file. While committing I got the same type error. I solved with the following step

use git status to see the staged files you can see your old file in the list

- 
-
app/src/main/java/blahblah/FOOBar.java
-
-

use git reset to remove file from staging

git reset app/src/main/java/blahblah/FOOBar.java

After removing from staging you can add your new file

git add app/src/main/java/blahblah/FooBar.java

after this you can commit

Bikesh M
  • 8,163
  • 6
  • 40
  • 52
0

A really simple fix I found was to do this command in terminal

git reset

adisai1
  • 3
  • 1
0

Try to restart the Android Studio!

I had this problem on a repository with no commits.

Paulo Pereira
  • 1,750
  • 1
  • 12
  • 23
-1

http://binaryjeremys.blogspot.com/2015/03/android-studio-doesnt-like-it-when-you.html

To fix this issue, I had to reset to head with the steps below:

Backup the files first: Right-click one of the files and chose "Show in Explorer". Make copies of any files that have changed or the entire project to be safe. In Android Studio Delete all conflicting/erroring objects. VCS -> Git -> Reset Head WARNING: This will reset everything to the last time you did a commit to head. Once this is complete, just to be sure: Close the project, close Android studio, then reopened both. Create new classes to replace the deleted conflicting objects with the correct name. Replace the contents of these files, or copy/paste the backup files into the folder. Commit and push your changes now, and it should work.

Follow the link for main problem cause.

Saad Bilal
  • 1,767
  • 1
  • 17
  • 31