Sunday, March 22, 2015

Android Studio doesn't like it when you rename and commit with git

Android Studio seems to handle Github/git a lot better than Eclipse's plugin for git does.  However, even Android Studio can get confused at times.

This issue was caused when a new uncommitted java class was renamed to change capitalization on windows.  I suspect it MAY have something to do with the fact windows is not case sensitive on files, but perhaps github is.

Story:
Today I created a class named [entity] which I realized I accidently didn't capitalize the first letter.  Android Studio prompted me to add it to git and I said yes.  Later I renamed the class correctly as [Entity], and eventually tried to commit the change.

Android Studio and Git reported the following error:
Error:error: pathspec 'core/src/com/twojeremys/merge/entity/Entity.java' did not match any file(s) known to git.
error: pathspec 'core/src/com/twojeremys/merge/entity/Projectile.java' did not match any file(s) known to git.

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

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

If that doesn't work, good luck, I wasted 24 minutes on this ridiculous issue.

2 comments:

  1. The same issue I've got every time when add file to git and rename it later. The simplest way to fix this i found was commit from the command line. I just use this way:
    git status //to check what is going on
    git add -A //will let you also record the removals
    git commit -m 'commit name' //just do the job

    ReplyDelete
    Replies
    1. Thank you for the work around. I wish Android Studio just handled this better :)

      Delete