78

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$ 
RandyMy
  • 1,013
  • 1
  • 8
  • 9

16 Answers16

153

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).

quux00
  • 13,679
  • 10
  • 57
  • 69
109

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.

jimjoebob123
  • 1,089
  • 1
  • 7
  • 4
  • 16
    +1 this is a good idea. people who ask are frequently not in the root. It's interesting that in this case the OP does not show `cd`ing into the project directory. – Michael Durrant Sep 23 '15 at 20:22
  • Huge source of confusion for people starting out in git - most commands are executed within the context of the current working directory. – gravidThoughts Sep 29 '17 at 16:58
  • 4
    This should be the first line of defense THEN the answer by @quux00 – Jacksonkr Jul 10 '20 at 14:50
  • this is usually the case when git add --all and git add . produce different results – Yolomep Jul 25 '22 at 01:59
20

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
Wtower
  • 18,848
  • 11
  • 103
  • 80
5

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.

Romain Dereux
  • 119
  • 2
  • 5
4

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)

Pablo
  • 77
  • 1
  • 7
4

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
Vincent
  • 433
  • 3
  • 10
3

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.

GarlicFries
  • 8,095
  • 5
  • 36
  • 53
  • 1
    I encountered the same issue with a git repo residing in Google Drive File Stream on Mac OS X. Unmounting ("ejecting") and re-mounting File Stream resolved the issue, but I think the better solution is indeed to move the repo out of File Stream entirely. – Peter Leimbigler Sep 06 '19 at 04:11
2

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.

Andrey Kovalenko
  • 2,534
  • 1
  • 11
  • 4
2

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
Bluebaron
  • 2,289
  • 2
  • 27
  • 37
1

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.

TracNav
  • 45
  • 6
0

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 :)

0

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...

  • This does not seem to do what you described? If you want to see, if a file is tracked or not just run `git ls-files ` - no need to error-unmatch it. – Lars Dec 04 '20 at 14:49
0

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
  • 880
  • 1
  • 8
  • 20
0

My problem is solved by :

The git pull --rebase command is helpful in situations where your local branch is a commit behind the remote branch.

git pull --rebase origin main

git push -u origin main 

In my another try, I solved it

git rebase --continue
PyMatFlow
  • 459
  • 4
  • 8
0

Just ran into this issue. For me, my file was already added to Git, and there were no changes to commit at all.

So try editing the file to see if git status shows you a different result; if so your file has already been added and you can restore the file with git checkout -p

Nahin Khan
  • 17
  • 1
  • 3
-2

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.

Alta
  • 7
  • 2