5

Let's see what it is

  • Create two git repo sub & 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 the sub repo into test
 mv sub test cd test git add sub git commit -m "add sub directory" 

I want to treat them as one git repo and push them remotely, but now the files under sub directory can not be included ?

How can I achieve this in simple way like treat sub as normal directory ?

Use case for this

I try to add my jenkins data folder (JENKINS_HOME) into docker images using Dockerfile for demo. (ADD JEKINS_HOME /opt/jenkins)

JENKINS_HOME Dockerfile 

My jenkin has scriptler plugin which contains the git repo for its purpose. Then it exists in my docker image git repo like 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 

2 Answers 2

2
+100

Seems you cannot do that. The name .git is hard-coded in the source code: https://github.com/git/git/blob/fe9122a35213827348c521a16ffd0cf2652c4ac5/dir.c#L1260

Probably one way is to make a script which renames .git to something else and back before and after adding it into repo like

In working directory under scripts

mv .git hidden-git 

In Dockerfile

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

Alternatively, probably it's possible to pass GIT_DIR environment variable into the plugin, so it could use another name.

Sign up to request clarification or add additional context in comments.

4 Comments

Would be good to change the behavier of that plugin. script probably is not so bad.
@LarryCai If I read this code correctly, it doesn't support GIT_DIR customisation... so no way you could do it unless modify the code. github.com/jenkinsci/git-server-plugin/blob/… It should have setGitDir with some customisable value, but nothing here.
@LarryCai It's open source, you are welcome to introduce a patch.
could be. But I choose the script this time and update your answer directly.
0
git add sub git commit -m "add sub directory" 

I want to treat them as one git repo and push them remotely, but now the files under sub directory can not be included ?

They are not included because test sees repo sub as nested git repo, and records only its gitlink, or SHA1, and not its url as it would have if sub had been added as a submodule.

You would need to push sub to a remote url first, and then add it as submodule for test to see sub files.

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

Or you would need to use a subtree

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

1 Comment

thank, and the use case doesn't fit for this solution well

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.