git add . -> still "nothing to commit" with new files

GitAddInit

Git Problem Overview


I am struggling with Git, I can't seem to add my files. I ran ls to show that the files are in the current directory, then ran git add . then git status which showed "nothing to commit".

JJ-Computer:first_app JJ$ git init

Reinitialized existing Git repository in /Users/JJ/rails_projects/first_app/.git/

JJ-Computer:first_app JJ$ ls

Diary.txt README.rdoc config.ru log   tmp
Gemfile   Rakefile  db    public    vendor
Gemfile.lock  app   doc   script
README    config    lib   test

JJ-Computer:first_app JJ$ git add .

JJ-Computer:first_app Jenn$ git status

# On branch master
nothing to commit (working directory clean)

JJ-Computer:first_app JJ$ 

Git Solutions


Solution 1 - Git

Your commands look correct (I've done them many times that way).

First try

git add --all

and then try git status. I don't think that will solve it, but worth trying next.

Next try looking at your .gitignore file, if you have one (in the top level where you did git init).

cat .gitignore

Remove any listings there that are causing your files to be ignored. For example is there an entry with just *?

Next try:

git add --force

and then try git status.

If none of those work, I notice that your output from git init says "reinitialized" rather than "initialized", so something may have gotten messed up. If you've just initialized it and don't mind losing history, start over by removing the .git dir:

rm -rf .git

And then reexecute your same commands above. If that doesn't work, some more information about your setup will be required. For example, you might have a global .gitignore file: ~/.gitignore_global that needs to edited (or removed if you don't want it).

Solution 2 - Git

Since I've experienced a similar issue multiple times I would just like to add that you should double check that you're trying to add from and are currently in the root folder of your project.

Solution 3 - Git

In case somebody else bumps into this, another reason that git does not add a directory is that maybe there is a .git directory within that directory.

In this case, git expects you to treat that as submodule. Otherwise, simply remove that .git (in the directory, not the root) and retry.

Handy command:

find . -name '.git' -type d

Solution 4 - Git

In my case I solved it by stashing and immediately afterwards applying the stash:

git stash
git stash apply

This sequence will leave the repo exactly like it was before. After this, doing git add [file] worked correctly.

(Using git version 1.7.12.4 under suse linux)

Solution 5 - Git

For some reason, Git worked well for me when I had repos in directories in Google Drive File Stream for quite a while. Yesterday, I ran into the same issue. git add . would report nothing to commit, but git add <some explicit filename> worked without a problem. I moved the directory to my local hard drive (off of Google Drive File Stream), and git add . picked up all my pending changes. Note that when the directory was still on Google Drive File Stream, setting the directory to be "Available offline" had no effect.

Solution 6 - Git

In my case, caused by nesting a git directory in the current git directory:

A1.java
B1.java
.git
someDirectory
  C1.java
  .git        
cd someDirectoy
rm .git -rf

Solution 7 - Git

I had this kind of situation just now. In my case, it was the file (lower/upper)case issue. The thing was - I had the NuGet.config file modified, and after that, Visual Studio re-created this file for some reason, but with name NuGet.Config (pay attention, not .config, but .Config).

The simplest fix I found was to remove (actually, move) this file to another location, and commit without this file at all. The git status message was

[deleted] NuGet.config
[deleted] NuGet.Config

Then I copied my file back to repo directory and committed, everything worked as it should be in the very beginning.

Long story short, in my case it was file naming strategy collision. In Windows, uppercase/lowercase chars in file names don't mater, but for UNIX-originated git command do.

Solution 8 - Git

I had a similar issue and realised that I was doing git add . from a subdirectory of main.

Be sure to be at the root of your project or you can use git add --all from any subdirectory and it will be ok.

Solution 9 - Git

Supplementing quux00's .gitignore answer, I found that my directory wasn't being tracked due to my build manager's (Maven's) temporary 'build' directory. Maven added 'build' to the .gitignore when building my program and thus my own directory (coincidentally named 'build') was then not tracked. This was solved by renaming my 'build' directory.

Solution 10 - Git

I found a completely different reason this is happening, and it's quite something. If you're not in Windows, this probably won't affect you. If you're seeing your file in the unstaged changes, .gitignore is never going to be your problem. Check something very carefully. Is your file both in the staged and unstaged changes?

git status
Staged changes
  modified: source/abc.Ui.Tests
  modified: source/def.Ui.Tests/def.UI.Tests.csproj
Unstaged changes
  modified: source/abc.UI.Tests
  modified: source/def.UI.Tests/def.UI.Tests.csproj

See the difference? It's hard to spot. Windows doesn't care about capitalization, but git bash does.

git rm -r source/abc.Ui.Tests
git reset source/abc.UI.Tests
git commit -m "Fixing capitalization"
git checkout source/abc.UI.Tests
git status
     working clean tree

Solution 11 - Git

Try git add <folder_name> one by one.

If first folder is staged. Try adding the next folder. In my case, I got one file contains some issue.

If directly run git add . while have issue in certain files, git will ignore all of it. Not an expert but this is what happened to me :)

Solution 12 - Git

This seem a little dumb, but may save someone time: check if the file is already tracked and has no modifications: git ls-files --error-unmatch <file>. This command should not show any errors if the file is tracked.

I just spend a few minutes to figure out this...

Solution 13 - Git

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.

Solution 14 - Git

I had a similar issue adding a new file to an existing initialized git folder. git add <filename> didn't work. What worked for me was git add . I use bash 5 and git 2.25 for mac. I hope this will help someone else, too...cheers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRandyMyView Question on Stackoverflow
Solution 1 - Gitquux00View Answer on Stackoverflow
Solution 2 - Gitjimjoebob123View Answer on Stackoverflow
Solution 3 - GitWtowerView Answer on Stackoverflow
Solution 4 - GitPabloView Answer on Stackoverflow
Solution 5 - GitGarlicFriesView Answer on Stackoverflow
Solution 6 - GitVincentView Answer on Stackoverflow
Solution 7 - GitAndrey KovalenkoView Answer on Stackoverflow
Solution 8 - GitRomain DereuxView Answer on Stackoverflow
Solution 9 - GitTracNavView Answer on Stackoverflow
Solution 10 - GitBluebaronView Answer on Stackoverflow
Solution 11 - GitkaitoAckermanView Answer on Stackoverflow
Solution 12 - Gitbruno.do.amaralView Answer on Stackoverflow
Solution 13 - GitRyanmanView Answer on Stackoverflow
Solution 14 - GitAltaView Answer on Stackoverflow