How to Fix: Error: '' Does Not Have a Commit Checked out Fatal: Adding Files Failed When Inputting "Git Add." in Command Prompt

I'm trying to add a ruby rails file to my repository in gitlab but it somehow wouldn't allow me to add the file saying that my file does not have commit checked out.

I've tried git pull, making the the file again and git adding but still wont work

error: '172069/08_lab_routes_controllers_views_172069_172188-Copy/adventure_game/' does not have a commit checked out
fatal: adding files failed
1

If you have a subdirectory with a .git directory and try to git add . you will see this message.

This can happen if you have a git repo and then create/clone another repo in a subdirectory under that repo.

3

I had the same error Message. I fixed it by deleting the file which causes the error from my Directory.

I hope this helps =)

0

To expand on both the accepted answer from Mario Zigliotto and Albert's answer, the reason this occurs is because of submodules. Here is a simple way to re-create the problem:

$ 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 clone on 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.:

rm -rf sub/.git

(see also Nissan's answer if using PowerShell on Windows).

Before you do that, you should:

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

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

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

I had the same Error Message. I fixed it by command below:

git add folder-name/* instead of git add .

My Folder directory is like below:

Main-folder
 -folder-one
 -folder-two

ref:

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.

1

The folder has a .git folder, due to the .git folder 📁 , it causes the error. So just delete that folder and then try again. It will be working.

Note: It is a hidden folder, so just select Hidden items in windows setting

I tried all those solutions but none worked for me. but the following solved my problem completely.

git rm -r --cached .

if you want to add a different repo you can use the above solutions

git remote set-url origin :username/project.git

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.

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' (:albertz/...) registered for path 'xyz'
...

And then git submodule update:

Cloning into '.../xyz'...
...
Submodule path 'xyz': checked out '...'
...

That fixed the error. Now git commit . runs fine.

3

My solution ended up using:

git mergetool

This brought up an interface where I could mark whether submodules should be used local or remote.

Submodule merge conflict for 'abc':
  {local}: submodule commit (hash)
  {remote}: submodule commit (hash)
Use (l)ocal or (r)emote, or (a)bort?

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.

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 .  

In my case I just wasn't allowed to push within the branch, so I had to create another one and make a Pull Request

I had thesame problem and here is what I found to be the cause.

Under one of the subfolder I had intialized git so I had a two .git folders, one in the parent directory and the other in the subfolder

To solve this I removed the git in the subfolder

cd project-directory/sub_folder_name

check if there is a .git folder

ls -la

rm -rf .git 
cd ..
git add .
git commit -m "commit message"

Use git add folderName/ instead of git add folderName

0

If you have both backend and frontend folder navigate to those folders and run this command on both directories

$ rm -rf .git

then navigate back to your main directory

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest