1

Git seems to be tracking the java folder, but I can't commit changes to the specific file MainActivity.java. I get a "Changes not staged for commit" The diff shows the changes, but I can't commit it.

Also, I can't go down into the folder in Github.

Project structure Commit error

Github structure That folder is not clickable.

Edit: When I do git status in the command line, I get

user:JustJava juil$ git status
HEAD detached at 93ea9a1
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)

        modified:   app/src/main/java/com/example/android/justjava (modified content)

no changes added to commit (use "git add" and/or "git commit -a")

Edit 2: I have checked out the latest commit as some have suggested but still get the detached head error.

Current git log

Edit 3: I tried adding the file itself w/ git add but got an error that the directory is a submodule.

JustJava juil$ git add app/src/main/java/com/example/android/justjava/MainActivity.java
fatal: Pathspec 'app/src/main/java/com/example/android/justjava/MainActivity.java' is in submodule 'app/src/main/java/com/example/android/justjava'

I tried running git submodule status and got the error

fatal: no submodule mapping found in .gitmodules for path 'app/src/main/java/com/example/android/justjava'

I tried deleting .git in the submodule, but the same error keeps popping up.

juil
  • 2,408
  • 3
  • 18
  • 18

3 Answers3

0

To correct this using just the Android Studio interface, try the following:

  1. Make a backup copy of your changed files, just in case this method doesn't merge your changes correctly.
  2. In the version control pane, select the Log tab. It will show you a list of commits.
  3. The top commit should be the newest one. Right click it and select Checkout Revision in the context menu.
  4. If a dialog appears, select Smart Checkout. This will merge your changes with the selected commit.
  5. Your HEAD should now be attached, so you can commit normally now.
Fusiongate
  • 11
  • 4
  • I tried this and it didn't do anything. I have updated the question with a screenshot of my current git log. – juil Jun 15 '16 at 21:52
  • It looks like MainActivity.java was never actually added to git. Try doing `git add app/src/main/java/com/example/android/justjava/MainActivity.java` – Fusiongate Jun 16 '16 at 00:22
  • Tried it but got a submodule error. Updated in the question. – juil Jun 16 '16 at 01:57
  • 1
    You might try one of these answers to remove the submodule: [link](http://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule/) but it may be as simple as `git rm -rf --cached justjava` – Fusiongate Jun 16 '16 at 03:06
0

When your HEAD is detached, it means you did the commandline equivalent of:

git checkout <UUID of previous commit>

The grey folder that was created is called a submodule, it happens when you initialize a git repository inside another git repository. I would suggest removing the .git folder inside JustJava/app/src/main/java/com/example/android/justjava.

Using git through terminal, follow these steps:

  1. Backup all the code in a different folder

  2. **Remove JustJava/app/src/main/java/com/example/android/justjava/.git"

  3. Checkout out master or the current branch you're on (default: master)

    In order to checkout master, go to the folder where you git project is through the terminal (linux or mac) or git bash (if you're on windows), then do:

    git checkout master
    
  4. Comparing backed up code with the current code and make the changes you want (if any)
  5. Commit new code
  • I tried checking out the master, but still got the same errors as stated in my newest edit to my question. – juil Jun 16 '16 at 02:05
0

Since justjava folder turned out to be a submodule, in the super directory, I called git rm -rf --cached justjava as suggested by @fusiongate and answered in this question.

This works in that it allows the file to be added and committed, but the commit history on this file is lost.

juil
  • 2,408
  • 3
  • 18
  • 18