10

I have a top level git repo organized like so:

  • .git/
  • repoA/
    • .git/
    • file
    • file2
    • folder/
    • ...
  • file
  • file2
  • folder/
  • ...

How can I get git to add and commit repoA (and repoA/.git) as if they were regular files/folders?

Specifically, I want to bypass the treatment of such a folder as a submodule but I want to retain the branches/history of the nested repo (so the .git folder is required).

I do not want to alter the state of the nested repo (by committing or merging histories) and do not want to push it to a separate repo on the remote.

The intended result is the same as if I were to tar repoA and un-tar it later. That is, all the files are intact with no modification or special treatment by git.

arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
  • Related: https://webmasters.stackexchange.com/questions/84378/how-can-i-create-a-git-repo-that-contains-several-other-git-repos and https://stackoverflow.com/questions/47008290/how-to-make-outer-repository-and-embedded-repository-work-as-common-standalone-r and https://stackoverflow.com/questions/9674576/how-can-i-get-git-to-add-files-already-tracked-under-another-git-repository – Gabriel Staples Jun 14 '20 at 04:30
  • Also related: https://stackoverflow.com/questions/40921904/force-adding-submodule-contents-in-git/40922436#40922436 – jthill Apr 29 '21 at 18:12

2 Answers2

5

This is not directly possible. The .git directory has a special meaning for git. When you run git operations from inside a git work-tree (not necessarily from the top level) without explicitly specifying the path to the repository (via the --git-dir option or the GIT_DIR environment variable) the latter is automatically discovered by looking for the first ancestor directory that contains a .git subdirectory. A .git directory that is intended to be considered as regular content would interfere with that procedure.

However, I can suggest the following workaround:

  1. In your sub-repository, rename the .git directory to something else (e.g. .git.subrepo, .pseudogit, etc):
mv repoA/.git repoA/.pseudogit
  1. Add and commit the sub-repository directory:
git add repoA
git commit -m "Added repoA as regular sub-directory"

After this git operations from inside the sub-repository tree will work using the master repository by default. When you need to study the history of the sub-repository you will have to use the --git-dir option:

cd repoA
git --git-dir .pseudogit log
Leon
  • 31,443
  • 4
  • 72
  • 97
4

You can look into git subtree.

For that, if you currently have submodules, you can follow "Convert a git repository from submodules to subtrees":

cat .gitmodules |while read i
do
  if [[ $i == \[submodule* ]]; then
    mpath=$(echo $i | cut -d\" -f2)
    read i; read i;
    murl=$(echo $i|cut -d\  -f3)
    mcommit=`eval "git submodule status ${mpath} |cut -d\  -f2"`
    mname=$(basename $mpath)
    echo -e "$name\t$mpath\t$murl\t$mcommit"
    git submodule deinit $mpath
    git rm -r --cached $mpath
    rm -rf $mpath
    git remote add $mname $murl
    git fetch $mname
    git branch _$mname $mcommit
    git read-tree --prefix=$mpath/ -u _$mname
fi
done
git rm .gitmodules

(test it on a copy of your repo first)

See also "When to use git subtree?", which differs from ingydotnet/git-subrepo.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Your first link is now this: https://www.atlassian.com/git/tutorials/git-subtree. It auto-forwards so no big deal. Looks like a pretty good article. I'm reading it now. – Gabriel Staples Jun 14 '20 at 04:35
  • Related: https://stackoverflow.com/questions/32407634/when-to-use-git-subtree – Gabriel Staples Jun 14 '20 at 04:49
  • And yet another option: https://github.com/ingydotnet/git-subrepo – Gabriel Staples Jun 14 '20 at 04:54
  • @GabrielStaples Thank you . I have updated the answer accordingly. – VonC Jun 14 '20 at 15:18
  • 1
    Glad to help. I just wrote this `git disable-repos` tool over the weekend too to archive a bunch of old projects (into a single parent repo) which need fixing up and reviving at a later date: https://stackoverflow.com/a/62368415/4561887. Perhaps your or someone else will find it useful as well. – Gabriel Staples Jun 15 '20 at 10:09