2

Hello I am new to using git and actually putting code there, so yesterday I had made a new repo https://github.com/Ntshembo-Hlongwane1/After-Dark-XI-Online-Store

That repo has client folder in it.

So today I made new file and started a new project all together so I also wanted to make some commits for today's progress on that project but when I try to git add . I get the following warnings

warning: adding embedded git repository: client
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint: 
hint:   git submodule add <url> client
hint: 
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint: 
hint:   git rm --cached client
hint: 
hint: See "git help submodule" for more information.

So I used this command git submodule add <url> client with the hopes of everything will be sorted but just find out that when the files are added to the repo I can not access the client files

if you look at it, it's not accessible how can I resolve this issue? https://github.com/Ntshembo-Hlongwane1/Real-Estate-Agency

Ntshembo
  • 73
  • 2
  • 7
  • 1
    paste the output of `git remote -v` – Chakreshwar Sharma Sep 23 '20 at 20:23
  • Note that Git does not push *files*, but rather pushes *commits*. It's true that commits *contain* files (each one has a full snapshot of every file that it contains at all) but it's an all-or-nothing deal: you either send the commit (and then they have it and hence all files), or you don't (and they don't). As [W.Prins answered](https://stackoverflow.com/a/64036071/1256452), that big long `hint` that your Git printed out is telling you that you've made a submodule: new commits won't have its files, they'll just say *and then use commit _____ (fill in the blank) from another Git*. – torek Sep 23 '20 at 23:06
  • I understand this right but I fail to understand how I mad that submodule, bare with me for noob question literally my first time using Git and pushing code there – Ntshembo Sep 23 '20 at 23:12

3 Answers3

2

You've somehow created a git repo inside another, git considers this to be a submodule, e.g. a pointer to another independent git repo. I kind of doubt you intended to do that, instead I suspect you merely wanted to add files to your original repo. If so then get rid of the submodule and just add normal files to the toplevel repo.

Submodules are useful when your project references specific versions of other (independent) projects and you want to include those specific version in your project. You can do this with submodules, and git can then track and checkout those other repo's (submodules) when your parent repo is checked out. You can interact with submodule repositories from the parent repo with the "git submodule" command.

Edit: Responding to the additional question in the comment below about how this happened and how to prevent it happening in future:

Obviously I'm guessing here, but I think what's happened is something like this:

1.) You created a repo, called "client", something like the following (I'm outlining the events using command line commands, I hope this is clear enough to communicate the gist, if not then ask for clarification):

mkdir client
cd client
git init
notepad somefile.txt 
git add .
git commit -m "added somefile.txt"
cd ..

2.) Then you created another repo alongside that, called "server", again, similarly maybe something like this:

mkdir server
cd server
git init
notepad server.txt 
git add .
git commit -m "added server.txt"
cd ..

At this point you would have had 2 independent repos alongside each other.

3.) Then I'm guessing you decided to put these into one containing folder "parent", and to make a repo out of this also (to maybe upload to github):

mkdir project
mv client project
mv server project
cd project
git init
git add .

Now notice what you've done: You've got 2 git repos already, now you've moved them inside another one...

But, you think, I need to add my 2 folders into the new parent repo.

So consequently at this point you'll get:

C:\temp\project> git add .
warning: adding embedded git repository: client
hint: You've added another git repository inside your current repository.
... etc

The key thing to understand is that when you have a git repo inside of another, it is considered to be a submodule, which means that the parent git repo will not track changes underneath a child git repo. The child repo will track such changes.

What you're really doing then, is trying to merge 2 independent repositories which remember, have independent histories...

How to deal with this depends on what you want. If you don't care about the commit histories in the original repos, you can convert them into normal folders (lose their repos, held in the hidden .git folder in each) before adding them into the parent repo by deleting the .git folder in sub folders.

Deleting the .git folder will delete the local repository copy of those folders, including all branches and all history. If you have no other (remote or backup) copies of your repository then this will mean all history/branches for these repos will be lost. Make sure this is what you want. You have been warned. (It is advisable to make a backup copy of the original folders before doing anything.)

If you do this before you "git add" the moved folders into the parent repo you won't get the message anymore.

If you do it afterwards, you can still remove the .git folder to "fix" things, but you'll need to also make git forget about the child git repo/submodule (using e.g. the command "git rm --cached ..." -- see below.)

If however you want to retain the histories of both branches, effectively merging them as though they've always been one, then it is a bit more tricky. Again what to do depends somewhat on what you want as the output, but basically you need to create a new repo, set up the one original repo as upstream, pull all the commits from it, move the files into a suitable subfolder, then repeat the process with the other. There are multiple pages on the internet about this.

Probably what you'd want to read in that case is this question: Merge git repository in subdirectory and in particular the answer using git-subtree, which is the git command specifically intended for this sort of thing. Also this: How to un-submodule a Git submodule? -- It contains explanation regarding using "git rm --cached" to make git forget about a child folder being a submodule/repo in order to enable you to add it to a parent repo.

W.Prins
  • 1,286
  • 1
  • 14
  • 22
  • I understand that which you saying and now it is resolved but I fail to understand how I caused that to happen can I get pointer on avoiding that mistake again – Ntshembo Sep 23 '20 at 23:11
2

Since W.Prins already explained it in detail. there is only one thing I want to point out.

hint: You've added another git repository inside your current repository.

Delete the folder which have .git file. [you probably duplicating files]

Hope this works for future noobs.

Lord-shiv
  • 1,103
  • 2
  • 9
  • 19
0

this worked for me delete the .git in the folder mentioned as embedded git repository

cd to the parent directory
git rm -f --cached {folder mentioned as embedded git repository}
git commit
git push
git add .
git commit
git push