0

I'm very new to git, and have only used it for simple project commits etc. so I may have made mistakes!

I recently began using git on originally unversioned code. I am using git in Visual Studio 2017. If I remember right, Visual Studio created the repositories, I didn't make any of them manually.

I have 4 projects in the same solution in Visual Studio. I noticed recently that 1 specific project didn't notify me of uncommited changes (Visual Studio puts icons next to files), and it wont let me commit changes, but I am able to see commit history. Using Git GUI or Bash I can see unstaged changes etc. and commit them.

Looking in file explorer I can see there is a git repo in the solution root directory, and no git repo in the 3 project directories that are working. But in the project that isn't working correctly it has its own git repo. I'm not sure how this happened.

I assumed this was causing a conflict preventing visual studio from recognising my changes. I want to basically remove the "child" git repo in the project directory, and have the "parent" repo manage the whole solution.

I attempted to merge the two repos following this method (from Git GUI on Windows: merging conflicts):

cd path/to/project-b
git remote add project-a /path/to/project-a
git fetch project-a --tags
git merge --allow-unrelated-histories project-a/master # or whichever branch you want to merge
git remote remove project-a

I can see in my commit history that the "child" repo's history appears to have been merged in, but changes are still not detected in the offending project.

Git history

I renamed the child git repo to see if that helped, but no change.

Is there a process I need to follow to allow me to merge the two repos, and have the solution level git repo manage all projects?

It would be preferred to retain the commit history of the child project if possible.

Kyohei Kaneko
  • 932
  • 2
  • 8
  • 25
  • Repositories are made up of commits (not files). A repository cannot contain another repository: instead, it will have a *reference to* the other repository. Git refers to this as a *submodule*. If you already have a submodule, and want to "de-submodule-ize" it, this tends to be difficult: as far as I know, there's no tool for this. – torek Jul 08 '21 at 21:37
  • Thanks for your help. I have found that I can deinitialise a sub module using: git submodule deinit. Is this procedure likely to help in my situation? – Kyohei Kaneko Jul 10 '21 at 12:38
  • If you deinit the submodule, and then remove the inner `.git` entirely, you can add the *files* from the submodule to the superproject. This does not add the *commits* from the submodule to the superproject, so you have, in effect, discarded all of the history from the submodule. If that's OK, that's the easy way out of this dilemma. If not, there is no easy way out. – torek Jul 10 '21 at 20:18
  • Thanks for your help, I followed the advice of this article to try and get some semblance of history from the submodule. – Kyohei Kaneko Jul 13 '21 at 11:54

1 Answers1

0

I found a reasonable solution here: How To: Merge a Git submodule into its main repository

I'll put some extracts here in case the article is removed in future. The article walks you through merging a submodule for scripts in src/models into the main repo.

The main idea is to make a new directory structure in the root of the submodule directory, as if it was the parent directory, move all of the tracked files into that directory, and push to remote.

In the models directory:

mkdir -p src/models
git ls-tree master --name-only | xargs -I{} git mv {} src/models
git commit -m 'Move all files into src/models directory'
git push

Then remove all references to the submodule, and delete the directory:

Git submodules metadata is stored in the .gitmodules file:

remove the submodule from that file.

git add .gitmodules

.git/config has a similar entry, edit that file as well.

git rm --cached models

rm -rf .git/modules/models

git commit -m 'Remove models submodule'

rm -rf models

Then you merge the submodule files from remote:

In the main repository run the commands:

git remote add models-origin git@github.com/exampleUser/models
git fetch models-origin
git merge --allow-unrelated-histories models-origin/master
git remote remove models-origin

For myself I had a strange situation where most of the submodule references didn't exist already, and I got the error:

No submodule mapping found in .gitmodules for path 'submodule/path'

This answer helped me: https://stackoverflow.com/a/13394710/1324919 I had to run a submodule update after removing the cached submodule:

git rm --cached submodule-name
git submodule update --init

Now I have a history from the submodule and the main repo is tracking the files. It doesn't look like it would be easy/possible to revert to though. At least I have the history there that I can read through and copy/paste out of if necessary.

Git commits, all branch history

Kyohei Kaneko
  • 932
  • 2
  • 8
  • 25