I had the same error message when trying to add multiple files using "git add ." I was able to add all those files one by one instead of all at the same time.
You don't need to delete the entire file from the directory as the first answer suggests, I just had to delete the .git directory, and then your git add . will work
This happens for the reason @Mario Zigliotto suggested, there is another repo in a subdirectory under that repo.
To add to Mario's (correct) answer and resolve this type of issue which can be quite common (e.g.in scaffolding an application using an app generator inside a repository top folder, and it generates its own .git file), you can run the following commands to cleanup and get the files checked in properly to git.
cd \{\{appname}}
rm -rf .git
# rm -r -fo .git # if on Windows powershell
cd ..
git add .
Note: I assume you have a different problem, but I had the same error message and this was the first Google result, so it might be helpful to others to post my situation, problem and solution.
I have a repo with Git submodules.
The error occurs by git commit .:
error: 'xyz' does not have a commit checked out
fatal: updating files failed
Then I did git submodule init, which printed:
Submodule 'xyz' (git@github.com:albertz/...) registered for path 'xyz'
...
And then git submodule update:
Cloning into '.../xyz'...
...
Submodule path 'xyz': checked out '...'
...
$ mkdir empty-submodule && cd empty-submodule
$ git init
Initialized empty Git repository in [path ending in empty-submodule/.git]
$ mkdir sub && (cd sub && git init)
Initialized empty Git repository in ... [path ending in empty-submodule/sub/.git]
$ ls
sub
$ ls -a sub
. .. .git
$ git add .
error: 'sub/' does not have a commit checked out
fatal: adding files failed
Note that the subdirectory sub is itself a Git repository, but no commit is checked out in this subdirectory. This is a simple statement of fact, true in this case because I created sub, went into it, and created a new, empty Git repository there. So that Git repository has no commits at all.
To the above fact, we add one more: No Git repository can hold another Git repository inside it. The reason for this has to do with security, but for our purpose here, the important side effect of this fact is that an attempt to add a sub-repository to a repository (like the superproject in empty-submodule) does not add the repository itself. Instead, it adds a reference to a commit within the repository. This is how submodules are implemented.1 But to refer to some commit within the submodule, the submodule itself has to have some commit checked out.
The way to fix this really depends on the result you want. See the next section for more information about this.
1Technically, submodules are implemented with two parts:
Each commit in the superproject (the "outer" repository) has a reference to a commit within the submodule.
In order to be able to run git cloneon the submodule, the outer repository should also contain a .gitmodules file. This file will hold the instructions that tell the superproject Git how to git clone the submodule. Once the submodule repository exists, the superproject Git never needs to run git clone on it again. So it's possible to accidentally, or on purpose, omit the .gitmodules file entirely.
Doing this produces a superproject that is difficult to use. I like to call this a half-assed submodule. If it were fully-assed, the superproject would be able to clone the submodule, but since the .gitmodules file is missing, it can't.
Fixing the problem
There are multiple ways to fix the problem. Before you pick one, you should know whether you want to have a submodule. There's no one correct answer here, as the question of should I use a submodule is a bit like which flavor of ice cream should I pick: different people have different preferences.
If you don't want a submodule at all you will need to move or remove the .git subdirectory within the subdirectory in question, e.g.:
Determine whether you want to keep the other repository. If so, do not remove it! Just move it somewhere else, e.g., mkdir ../keep && mv sub/.git ../keep.
Determine whether you need to git checkout some commit or branch before moving or removing the repository (the .git directory). If so, enter the submodule and check out the desired commit.
If you do want a submodule, you may need to make some commit(s) within the submodule, or check out some existing commit, just as in step 2 above. Once the submodule repository is on the correct commit, you can git add it.
Here is an example of creating a commit in my submodule named sub:
$ cd sub
$ echo this is the submodule > README.md
$ git add .
$ git commit -m initial
[master (root-commit) c834131] initial
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ cd ..
Now that the submodule has a commit, I can add it to the superproject. There's a bit of a hitch here though, as I should also create the .gitmodule file mentioned in footnote 1 above. The git submodule add command does everything for you, but you need to know where you'll git push this submodule repository. For instance:
$ git submodule add ssh://git@github.com/place/where/submodule/lives.git sub
Adding existing repo at 'sub' to the index
Everything is now ready to commit in the superproject:
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitmodules
new file: sub
$ cat .gitmodules
[submodule "sub"]
path = sub
url = ssh://git@github.com/place/where/submodule/lives.git
The submodule has not actually been sent to GitHub yet. Before anyone else will be able to use the superproject, I'd have to create this GitHub repository, giving it sufficient (e.g., public) access, and git push the submodule commit to that GitHub repository. Then I can git push my commit in my superproject to whatever location that repository should live at. Then you—the generic "you"—can git clone the superproject, which now has a .gitmodules file with instructions by which your Git will be able to run git clone ssh://git@github.com/place/where/submodule/lives.git sub.
Submodules have a bunch of usability issues, with all of the above complications being one of them. Be sure you know what you're getting into.
This problem occurs if there is already a .git directory in some nested folder which in most cases is the <filename> which git throws as error.
If there is already .git directory and you again use git init in some parent directory then there is some weird situation in which .git tries to track another
.git directory.
To solve this, move to that directory and then use rm -rf .git. This will remove the .git directory in nested folder and then you can use git add --all or any other command.
NOTE : Deleting .git will erase all local tracking information of that folder e.g local commits or branches.