133

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
torek
  • 448,244
  • 59
  • 642
  • 775
mark29230
  • 1,333
  • 2
  • 6
  • 6
  • Welcome to Stack Overflow! You might get more help if you update the title of your question to make it more clear what you are asking. Try putting the error message "in quotes". – Gar Jul 03 '19 at 15:45
  • They might as well change the error message to `PC LOAD LETTER` for all the help it gives. – Michael Burr Mar 04 '23 at 01:31

18 Answers18

223

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.

Mario Zigliotto
  • 8,315
  • 7
  • 52
  • 71
  • 11
    In my case, I accidentally created a directory name `.,` inside the repository. Removing it solved the issue. – Soon Santos Mar 27 '20 at 18:25
  • 2
    I have similar issue but the way out of it, is to look for existing git repo contain as a subfolder and delete. After, `run the git add .` – AKinsoji Hammed Adisa Feb 02 '22 at 17:19
  • A .gitignore (embedded in a .idea directory) can also trigger. – cloudsurfin Apr 19 '22 at 04:15
  • seems that I can have another .git directories inside project repo and it still works. https://stackoverflow.com/questions/72972314/adding-nested-git-folders-inside-gitignore – Wakan Tanka Jul 14 '22 at 11:01
  • 6
    Yes, this is correct. I have .git initialized in my subfolder. I had to navigate into the subfolder and then I ran this command rm -rf .git – Nagaraj Alagusundaram Oct 10 '22 at 11:25
  • Same thing happened to me. Was following youtube training video & the instructor was having me create lots of new projects using "cargo new {prjname} --bin". Those ended up being scattered around "below" the original "hello world" folder. Found and deleted all the ".git" subfolders and ".gitignore" files, then "git add ." worked. Now I just have to figure out how to configure my local git to talk to my gitlab repo. ugh. – mnemotronic Feb 24 '23 at 16:39
27

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

torek
  • 448,244
  • 59
  • 642
  • 775
  • "Submodules have a bunch of usability issues". That's quite an understatement. As unfriendly as it is, Subversion's `svn:externals` property at least just works without having to think about it much after you set it up. – Michael Burr Mar 04 '23 at 01:37
12

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

I hope this helps =)

Marc
  • 199
  • 8
10

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.

10

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: https://github.community/t/error-git-add/2937

Haron
  • 2,371
  • 20
  • 27
7

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"
Titus Dishon
  • 96
  • 2
  • 5
5

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

Omkar Gharat
  • 99
  • 1
  • 5
4

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 git@gitlab.com:username/project.git
Timothy Mach
  • 131
  • 1
  • 9
3

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

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

Albert
  • 65,406
  • 61
  • 242
  • 386
  • This didn't work for me because submodule update said `Skipping unmerged submodule` and checking out and everything else manually didn't work because `error: you need to resolve your current index first`. – DustWolf Jan 15 '21 at 08:04
  • @DustWolf In my case, the submodules were not checked out. And their directories just empty. I guess you have some different case, where some submodule is in a bad state. Maybe just `git reset` on the submodule. If you don't know how to resolve this, I would post a separate question. – Albert Jan 15 '21 at 10:16
  • I had submodule directories empty too. submodule init/update before merging fixed it, thanks. – onetuser Jul 19 '21 at 09:55
3

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.

Sharryy
  • 224
  • 4
  • 11
2

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

0

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.

elpat7
  • 43
  • 5
0

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 .  
reddi.tech
  • 2,183
  • 1
  • 16
  • 18
0

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

Rafael Inácio
  • 161
  • 1
  • 4
0

Today I was stuck in this problem as well, managed to fix it with this solution:

git rm -r --cached .

after that you can add the different Github Repo

git remote set-url origin git@gitlab.com:.../thisIsYourProject.git

hope this helps, happy coding!

Rokas Rudzianskas
  • 582
  • 1
  • 7
  • 10
0

Just navigate to that suddir and do git add . and alos run git commit plus do the pushing aswell. After that navigate back to your root dir and do the git add . again .That solved my issue

Bukenya KizzaRoland
  • 721
  • 1
  • 7
  • 13
-1

Use git add folderName/ instead of git add folderName

nakuzm
  • 49
  • 6
-2
rm -rf 172069/08_lab_routes_controllers_views_172069_172188-Copy/adventure_game/.git

git add 172069/08_lab_routes_controllers_views_172069_172188-Copy/adventure_game/
Tim Siwula
  • 966
  • 11
  • 15
  • 1
    Code only answers are not very useful. Please edit your answer and add some details about what this code does and why it fixes OP's problem. – JeffC Mar 27 '23 at 01:47