How can I add a git submodule to git repo as a regular directory?

Let's see what it is

  • Create two git repo sub and test
  mkdir test sub
 cd test && git init && touch README && git add README && git commit -m "initialize the git repo" && cd ..
 cd sub && git init && touch README && git add README && git commit -m "initialize the sub git repo" && cd ..
  • Move sub repository to test
  mv sub test
 cd test 
 git add sub
 git commit -m "add sub directory"

I want to treat them as one git repository and delete them remotely, but now the files in the sub directory cannot be included?

How can I achieve this in a simple way, how to handle sub as a regular directory?

Use for this

I am trying to add jenkins data folder ( JENKINS_HOME ) to JENKINS_HOME images using Dockerfile for demonstration. ( ADD JEKINS_HOME /opt/jenkins )

 JENKINS_HOME Dockerfile 

My jenkin has a scriptler plugin that contains a git repository for its purpose. Then it exists in my docker git repo image as below

  $ find jenkins-docker
 ./.git
 ./.git/ .. (skipped
 ./Dockerfile
 ./JENKINS_HOME
 ./JENKINS_HOME/scriptler
 ./JENKINS_HOME/scriptler/scripts
 ./JENKINS_HOME/scriptler/scripts/.git
 ./JENKINS_HOME/scriptler/scripts/.git / ... (skipped)
 ./JENKINS_HOME/scriptler/scripts/Sample.groovy
 ./JENKINS_HOME / ... (skipped)
 ./README
+5
source share
2 answers

You can't seem to do that. The name .git hardcoded in the source code: https://github.com/git/git/blob/fe9122a35213827348c521a16ffd0cf2652c4ac5/dir.c#L1260

Perhaps one way is to create a script that renames .git to another and back before and after adding it to the repo, for example

In the working directory under scripts

 mv .git hidden-git 

In dockerfile

 RUN mv $JENKINS_HOME/scriptler/scripts/hidden-git $JENKINS_HOME/scriptler/scripts/.git 

Alternatively, it may be possible to pass the GIT_DIR environment GIT_DIR to the plugin so that it can use a different name.

+1
source
 git add sub git commit -m "add sub directory" 

I want to treat them as one git repository and delete them remotely, but now the files in the subdirectory cannot be included?

They are not included, because test sees repo sub as a nested git repo, and writes only its gitlink or SHA1 , and not its url, as it would be if sub were added as a submodule.

First you need to click sub on the remote url, and then add it as a submodule for test to see the sub files.

  cd test git submodule add -- /url/to/sub 

Or you will need to use a subtree

 cd test git subtree --prefix sub /url/to/sub master --squash 
0
source

All Articles