29

No idea why this is happening:

git status
On branch master
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:   file (modified content)

no changes added to commit (use "git add" and/or "git commit -a")
starkers@ubuntu:~/Documents/currentWork/protection_demo$ git add --all
starkers@ubuntu:~/Documents/currentWork/protection_demo$ git status
On branch master
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:   file (modified content)

No matter what I do, git commit -am, git commit -a the file will not be added to the commit. Any help?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Starkers
  • 10,273
  • 21
  • 95
  • 158
  • What about `git add ` ? – XhkUnlimit Sep 25 '14 at 22:42
  • 3
    Instead of "file", you should post the whole path of the file in question. Also, are you using submodules? – jub0bs Sep 25 '14 at 23:08
  • 1
    Is the file in your .gitignore file? – chooban Sep 25 '14 at 23:08
  • @chooban Whether or not a tracked file is listed in `.gitignore` doesn't the ability to stage changes in that file, though. – jub0bs Sep 25 '14 at 23:12
  • But do you not need to use the --force option? – chooban Sep 25 '14 at 23:16
  • @chooban No; no need if the files in question are *already* being tracked. The file in question here is listed as "modified", which indicates it's a tracked file. – jub0bs Sep 26 '14 at 08:17
  • Of course! That'll teach me to try answering something after midnight. – chooban Sep 26 '14 at 09:29
  • same issue here. Files are not ignored, I am in correct directory, correct branch but git add . will not stage them. I use git add . all the time for years and never had this issue – cd491415 Oct 18 '18 at 15:52
  • I have a similar but upside-down problem: I have a "modified" file that doesn't exist, cannot be staged and doesn't go away with `git checkout -- .` ... in my case it's a clone from overleaf, I wonder if their git implementation is still a bit buggy? – drevicko Mar 05 '19 at 10:49

15 Answers15

22

I think maybe Jubobs' comment is correct. Is your "file" a submodule?

This line (commit or discard the untracked or modified content in submodules) should not appear for normal files.

Here is what I get from git status:

$ git status
On branch master
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)

        modified:   README.txt

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

You will not see (commit or discard the untracked or modified content in submodules) and (modified content) after my README.txt

So you may need to do what git recommended, work on the content in submodules.

Edit: Previously I thought the "git add ." could solve the issue but now I think it could not.

palazzo train
  • 3,229
  • 1
  • 19
  • 40
  • 4
    `--all` is a valid option of `git-add`. From the `git-add` man page: *`--all` Update the index not only where the working tree has a file matching `` but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree.* – jub0bs Sep 26 '14 at 07:35
  • Oh yes, you are right! I always use "git add ." . I think now I have to use this option instead – palazzo train Sep 26 '14 at 08:00
  • 2
    in my case there was a hidden .git folder in my sub-folder. so i deleted it and the problem was solved – λraulain Sep 04 '18 at 14:35
15

I had this same problem. My personal "aha" was that I had previously added a tracking .git repository file to a subfolder and thus it was seen as a submodule. After removing the .git in the subfolder, it happily joined the rest of my folders as part of just one .git file in the root folder. I didn't even know about submodules so maybe this will help someone as well.

  • 2
    i removed the .git folder which was accidentally created, the status got fixed. in my case i had to rename the folder/file and then push to the remote. like a work around. – Tim Aug 22 '19 at 20:04
  • 1
    Same here. For some reason, after I deleted `.git` from the subfolder it stopped tracking it completely, like it didn't exist. So I removed the parent's `.git` as well and redid the initial commit, since that's all the history it had. – Nagev Nov 13 '21 at 15:57
8

had the same problem, no idea why but a .h file would not commit.

So, I renamed it, this deleted original and created new as far a git was concerned. Then I committed these changes. Git was happy.

Then I just renamed it back. Again git was happy to commit.

Cannot explain why this happened, but at least it was fixed.

GAllan
  • 409
  • 4
  • 11
  • For me there was something wrong with the path. I had two different paths to the same file MyDomain.Service/Folder/Folder/MyFile.cs and mydomain.service/Folder/Folder/MyFile.cs (notice the difference in cases). When I remove the file from File Explorer and added a new one with the same code it worked. Thanks! – Joel Wiklund Oct 18 '21 at 14:24
  • I believe this happened to me as well. I had this issue on windows. None of the existing solutions pointed to something wrong on my side at all. I renamed the files, staged, then renamed back to their previous file name, then staged, and all was good. – Ryanman Jan 04 '22 at 18:37
6

Not sure if this is relevant or not, but I had the same issue when switching between Linux and Windows. I had 2 files one WEB.config and another web.config since Windows is not case sensitive it seems to have gotten confused. I renamed the file commited renamed back and commited again seems to have works.

Apathy
  • 61
  • 1
  • 1
4

You can add files in three ways

  • If you want to add single file use git add filenamewithpath

  • If you want to add multiple files use git add .

  • at a time you can add all files and you can write the commit message through this command

    git commit -a -m 'your commit message'

Community
  • 1
  • 1
Anjaneyulu Battula
  • 1,910
  • 16
  • 33
3

This similar thing happened to me. No matter what I did the file appeared modified. When looking at the diff it was changed as I had done in a previous commit. The file always appeared to be modified, even after committing the first time. Finally I removed the file. Committed the removal,which appeared like I had removed two files. Then added the file again. Quite odd thing that I have never seen before.

antonkronaj
  • 1,237
  • 12
  • 13
3

guys! maybe it will help someone, in my case, git didn't want to add and commit files, because I was trying to commit and add from the deeply nested folder, which doesn't contain git files. when I returned to the root folder, it added and committed easily.

Alexandr Shmidt
  • 129
  • 1
  • 7
  • This is an overlooked solution, especially in Windows. I just moved up one level from my deeply-nested current location and it did the trick. Wondering if this is somehow related to a long filename limit in Windows or Git? Meaning, the entire path to my current location combined with the entire path to the other deeply-nested files I want to stage exceeds a limit? (I've enabled long filename support in both Windows and Git, but... maybe?) – Neman Oct 08 '21 at 01:38
1

I had this same error:

fatal: Cannot update paths and switch to branch 'develop' at the same time.

Then with git status:

$ git status
warning: LF will be replaced by CRLF in .gitmodules.
The file will have its original line endings in your working directory.
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitmodules
        new file:   submodule_dir

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:   submodule_dir (modified content)

and git diff showed:

$ git diff
diff --git a/somedir b/somedir
--- a/somedir
+++ b/somedir
@@ -1 +1 @@
-Subproject commit some-commit-hash
+Subproject commit some-commit-hash-dirty

The issue was that I was trying to add a branch that did not yet exist in the submodule repository.

modle13
  • 1,242
  • 2
  • 16
  • 16
1

So basically if you encounter (commit or discard the untracked or modified content in submodules) it means you have (internal repos) ie .git file in your subfolders; and recent changes to the files have not been registered to these (internal repos).

  1. One way is to update the (internal repos) i.e git [add & commit] all the changes in the internal repos.

or

  1. Discard the internal repos.
0

It had happened with me just yesterday dude, in my case, the reason was because I was trying to call git from a subdirectory of my project. Then I used the "git add --all" and it solved the problem, also I had gone to the right directory above it also would solved de problem.

But what I do not get was: why in the world the git status, and git --all command was working from a subdirectory anyway? .-.

Hope it help =)

0

Another option is that the same very file is in stash and is in conflict with the current copy.

If this is the case, git stash drop - will drop the stash (with the file in conflict) and allow you to add the modified file to the index.

Lior Bar-On
  • 10,784
  • 5
  • 34
  • 46
0
git reset .

This did the work for me. unstaging all files that were ready for commit the (modified content) file included

0

In my case, I had two files with the same name in the same location, just different capitalization and git seemed to be unable to distinguish between them so it was showing a misspelled file as being modified (it showed a proper diff), but I was unable to stage/commit it. The solution for me was to remove the incorrectly spelled file from git: git rm <incorrectly spelled file> this removed both files and so I needed to recover the correctly spelled one: git checkout <correctly spelled file>

user6096790
  • 380
  • 4
  • 8
0

It may be due to a conflict between more than one git repo reference in the same or sub-folders.

Could be solved by

  1. deleting the .git subfolder from the untracked or modified folder and
  2. adding it again by git add untrackedfolder.
SridharKritha
  • 8,481
  • 2
  • 52
  • 43
-1

Take a back up of the problematic files. Remove those set of files, commit the deletion, push once to origin.

Add back the files from back up and issue a fresh add , commit and push. The problem will be resolved.

  • This answer is incorrect. You shouldn't need to delete and re-add files to a Git repo. The correct answer is [the accepted one](//stackoverflow.com/a/26051233/2747593), which states that the "file" is actually a submodule. **Flaggers / reviewers:** [For incorrect answers such as this one, downvote, don't delete!](//meta.stackoverflow.com/q/287563/2747593) – Scott Weldon Nov 03 '16 at 00:17