-->

Unable to add folder to git repo

2019-08-19 07:13发布

问题:

I think this issue has already been discussed under this question :

Recursively add the entire folder to a repository

However I still can not add some folders to a git repository and I need help.

$ git status
...
# modified:   folder_to_add1 (modified content, untracked content)
# modified:   folder_to_add2 (modified content, untracked content)

These folders originally belonged to another git repository, so I entered the folders and deleted the .git folder inside.

I then ran:

$ git add folder_to_add1
$ git add folder_to_add2
$ git status 
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#   (commit or discard the untracked or modified content in submodules)
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       ../filestore/
#       ../../stdout
no changes added to commit (use "git add" and/or "git commit -a")

Neither folder_to_add1 and folder_to_add2 can be committed.

There is no .gitignore file in my repo.

What else can I try ? Thanks.


I will try to add further information regarding my original post which ended right before previous line. I will appreciate your comments regarding "this" being considered the right war to further clarify my original question, I am a complete newbie posting here.

Dear torek, thanks for your very detailed answer. I guess I will have to read it very carefully to understand the subtleties involved with the submodules concept. I guess submodules are very much like "svn externals" which I am more familiar with.

From a ten minutes reading of your answer, my answer is: "I do not want a submodule or subgit at all". I mean: I want these folders, originally being submodules, to be part of only one project instead of many projects (to use a figure).

I am sure your answer is somewhere telling me why this is happening, but the fact is that I need to copy my project from one PC to another one and by using git to do it, these folders are actually not included in the index with "git add " regardless of whether there is a .git subfolder in them or not.

A ".gitmodules" file does not seem to exist. After running :

$ find . -name .gitmodules -print 

on the main folder of the project I get no results.

What I did to be able to add these folders to a new (different) repository was:

$ cp -r myproject /home/myusername/newproject
$ cd /home/myusername/newproject/folder_to_add1
$ rm -r .git
$ cd ../folder_to_add2
$ rm -r .git
$ cd /home/myusername/newproject
$ git init
$ git add .
$ git commit -m "Adding all existing files to the new repository"

But I think I loose all file changes history by doing it this way.

回答1:

Whenever you see:

modified: some-name (modified content)

in git status output, this means that your Git repository thinks that the directory named some-name is part of some other Git repository. (Note that the sub-directory / folder name here, some-name in this example, does not end with a slash.)

One way that I find helpful to think about this issue is to pretend that there are multiple Gits involved (because there are): one is your Git, working in your repository, and the other in this case is the folder or subdirectory's Git, working in this second Git repository. And it is—or at least was—a second Git repository, at some point.

This bears repeating: this sub-directory / folder really was some other Git repository before. It may or may not still be a second repository. Moreover, your own Git repository has already recorded the presence of the other Git in some way. If your Git repository had not recorded that, you would have seen:

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        subgit/

Instead, though, you are seeing:

Changes not staged for commit:
...
        modified:   subgit (modified content)

So, the fact that your Git repository's git status output says modified content or untracked content means your repository is quite certain that this sub-directory is some other repository. Your repository is reporting the relative state of the other repository.

You can get another hint about this if you use git status -uall, or git status --untracked-files=all (these mean the same thing, -u is short for --untracked-files=): if your Git doesn't think the sub-directory contains another Git repository, it will look inside the sub-directory and tell you about each file in the directory. If your Git is convinced that the sub-directory is a Git repository of its own, it won't look inside it and report each file—but your Git also won't say "modified content" or "untracked content" like this.

What to do about this depends on what you want to happen in the end

First, we need to talk about submodules.

Submodules

Git has a concept called submodules. Some people call them sob-modules because they make programmers cry.

标签: git git-add