Git won't add modified file
Asked Answered
T

15

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?

Toxemia answered 25/9, 2014 at 22:36 Comment(9)
What about git add <file> ?Semblable
Instead of "file", you should post the whole path of the file in question. Also, are you using submodules?Lucky
Is the file in your .gitignore file?Hydrophilous
@Hydrophilous Whether or not a tracked file is listed in .gitignore doesn't the ability to stage changes in that file, though.Lucky
But do you not need to use the --force option?Hydrophilous
@Hydrophilous 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.Lucky
Of course! That'll teach me to try answering something after midnight.Hydrophilous
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 issueHark
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?Pennsylvanian
H
23

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.

Homochromatic answered 26/9, 2014 at 2:40 Comment(3)
--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 <pathspec> but also where the index already has an entry. This adds, modifies, and removes index entries to match the working tree.Lucky
Oh yes, you are right! I always use "git add ." . I think now I have to use this option insteadHomochromatic
in my case there was a hidden .git folder in my sub-folder. so i deleted it and the problem was solvedCarnivore
A
17

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.

Active answered 21/1, 2016 at 3:10 Comment(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.Ellett
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.Scintilla
D
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.

Dishtowel answered 19/5, 2017 at 15:6 Comment(2)
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!Rosalinarosalind
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.Tahmosh
M
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.

Mannose answered 15/6, 2015 at 2:46 Comment(1)
same for Mac OSSoidisant
P
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'

Proceeding answered 26/9, 2014 at 2:55 Comment(1)
or just git commit -am 'your commit message' for shortClung
I
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.

Isaacs answered 25/10, 2016 at 17:17 Comment(2)
I had the same thing. In my case the file was renamed recently, idk what was happening but deleting and remaking (no commits in between) allowed it to be addedLeicester
Exact same thingOffering
G
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.

Geiger answered 16/11, 2019 at 13:11 Comment(1)
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?)Assert
A
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.

Alesandrini answered 26/1, 2017 at 20:9 Comment(0)
P
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.
Pickings answered 9/11, 2020 at 13:35 Comment(0)
G
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 =)

Glaive answered 13/2, 2016 at 13:30 Comment(0)
H
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.

Hauptmann answered 19/9, 2017 at 12:8 Comment(0)
F
0
git reset .

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

Forney answered 17/10, 2017 at 19:51 Comment(0)
H
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>

Higgledypiggledy answered 12/10, 2021 at 7:20 Comment(0)
M
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.
Moke answered 13/10, 2022 at 11:23 Comment(0)
P
-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.

Psychiatry answered 2/11, 2016 at 20:58 Comment(1)
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, which states that the "file" is actually a submodule. Flaggers / reviewers: For incorrect answers such as this one, downvote, don't delete!Envy

© 2022 - 2024 — McMap. All rights reserved.