如何将git repo添加为自身的子模块? (或者:如何以编程方式生成GitHub页面?) [英] How to add a git repo as a submodule of itself? (Or: How to generate GitHub Pages programmatically?)

查看:821
本文介绍了如何将git repo添加为自身的子模块? (或者:如何以编程方式生成GitHub页面?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的项目网站开始使用 GitHub Pages 。这只需要在回购中有一个名为 gh-pages 的分支(子树),并提供其内容。问题在于网站的一部分(手册,更新日志,下载页面)是由构建系统自动生成的,因此我想找到将这些更改提交到 gh-页面分支,而主回购仍然在 master (或任何地方)。



提交给 gh-pages 分支,我可以编写一个脚本,将repo克隆到临时目录中,进行修改,提交它们,然后将它们推回到main回购。但是这听起来像一个容易出错的过程,所以我希望有一个更简单的方法。



有朋友建议我可以添加 gh-pages 分支作为主存储库的子模块。我运行了一个小实验,但它不起作用:

  $ git init main 
初始化的空的Git仓库在/tmp/main/.git/
$ cd main
$ touch main.txt
$ git add。
$ git commit -m'主分支中的初始提交'
[master(root-commit)1c52a4e]主分支中的初始提交。
0个文件已更改,0个插入(+),0个删除( - )
创建模式100644 main.txt
$ git symbolic-ref HEAD refs / heads / gh-pages
$ rm .git / index
$ git clean -fdx
删除main.txt
$ touch index.html
$ git add。
$ git commit -m'网站分支的初始提交'
[gh-pages(root-commit)94b10f2]网站分支的初始提交。
0文件改变,0插入(+),0删除( - )
创建模式100644 index.html
$ git checkout master
转换到分支'master'
$ git submodule add -b gh-pages。 gh-pages
repo URL:'。'必须是绝对的或以./|../
$ git submodule add -b gh-pages ./ gh-pages
remote(origin )没有在.git / config中定义的URL

我是子模块的新手;当然做了一些阅读,但我不明白这种行为。为什么它需要一个来源远程?理想情况下,我希望子模块始终引用它驻留的回购站,因此它不应该引用 origin 或任何其他远程设备。如果有人克隆回购并运行 git submodule init; git子模块更新,它应该从新克隆的回购中拉出。



是否可以将回购作为自身的子模块添加?这是可取的吗?我需要注意哪些缺陷?有没有更好的方法来实现我想要的?

解决方案

在这种情况下,行为似乎是git试图将原始存储库的来源设置为子模块的原点。这是通过 git子模块手册页确认的,它说[我强调]:


<库>是新子模块的源存储库的URL。这可能是绝对URL或(如果以./或../开头),则是相对于超级项目的源存储库的位置。


对我来说一个很好的解决方法是执行以下操作:

  #define起源是此存储库的绝对路径 - 我们稍后将删除
#:
$ cd / tmp / main /
$ git remote add origin / tmp / main /

#现在添加子模块:
$ git submodule add -b gh-pages ./ gh-pages
在/tmp/main/gh-pages/.git/中初始化的空Git存储库
分支gh页面设置为从原点跟踪远程分支gh页面。

#现在.gitmodules看上去很明智:
$ cat .gitmodules
[submodulegh-pages]
path = gh-pages
url = ./

#但是,子模块的来源不是我们想要的:
$ cd gh-pages
$ git remote -v
origin / tmp / main /(fetch)
origin / tmp / main /(push)

#删除它并添加正确的原点(只是..):
$ git远程rm源
$ git远程添加源

#回到主存储库并提交:
$ cd ..
$ git commit -m添加gh-pages分支作为此存储库的子模块
[master 6849d53]添加了gh-pages分支作为此存储库的子模块
2个文件已更改,4个插入(+),0个删除( - )
创建模式100644 .gitmodules
创建模式160000 gh-pages

这似乎工作正常 - 如果我改变到另一个目录,并做:

  $ cd / var / tmp 
$ git clone --recursive / tmp / main /

...子模块已更新并正确初始化。 (更新:尽管您在下面的评论中指出,子模块中的 origin 将被设置为您从中克隆的URL,而不是 ..



至于这是否是个好主意:我曾参与过一个项目,过去类似的设置,然后放弃它。然而,这样做的原因是(a)主存储库中的替代分支很庞大,甚至对不需要子模块的人来说也膨胀了存储库;(b)它引起了混淆对于那些不确定发生了什么的人来说。



然而,对于您的用例,我认为这是一个相当整洁的解决方案:)

I want to start using GitHub Pages for my project's website. This simply requires a branch (subtree) named gh-pages in the repo, and serves up its content. The problem is that part of the website (manual, changelog, download page...) is auto-generated by the build system, so I want to find the best way to commit these changes to the gh-pages branch while the main repo remains on master (or wherever).

To commit to the gh-pages branch, I could write a script that clones the repo into a temporary directory, makes the modifications, commits them, and then pushes them back to the main repo. But this sounds like an error-prone process, so I'm hoping there is an easier way.

A friend suggested that I might add the gh-pages branch as a submodule to the main repository. I ran a little experiment, but it doesn't quite work:

$ git init main
Initialized empty Git repository in /tmp/main/.git/
$ cd main
$ touch main.txt
$ git add .
$ git commit -m'Initial commit in main branch.'
[master (root-commit) 1c52a4e] Initial commit in main branch.
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 main.txt
$ git symbolic-ref HEAD refs/heads/gh-pages
$ rm .git/index
$ git clean -fdx
Removing main.txt
$ touch index.html
$ git add .
$ git commit -m'Initial commit in website branch.'
[gh-pages (root-commit) 94b10f2] Initial commit in website branch.
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 index.html
$ git checkout master
Switched to branch 'master'
$ git submodule add -b gh-pages . gh-pages
repo URL: '.' must be absolute or begin with ./|../
$ git submodule add -b gh-pages ./ gh-pages
remote (origin) does not have a url defined in .git/config

I'm new to submodules; have done some reading, of course, but I don't understand this behaviour. Why does it need an origin remote? Ideally, I want the submodule to always reference the repo that it resides in, so it should not reference origin or any other remotes. If somebody clones the repo and runs git submodule init ; git submodule update, it should ideally pull from the newly cloned repo.

Is it possible to add a repo as a submodule of itself? Is it desirable? Are there any pitfalls that I need to be aware of? Is there a better way to achieve what I want?

解决方案

In this case, the behaviour seems to be that git is trying to set the origin of the original repository to be the origin of the submodule. This is confirmed by the git submodule man page, which says [my emphasis]:

<repository> is the URL of the new submodule’s origin repository. This may be either an absolute URL, or (if it begins with ./ or ../), the location relative to the superproject’s origin repository.

A workaround that seems fine for me is to do the following:

# Define origin to be the absolute path to this repository - we'll remove
# this later:
$ cd /tmp/main/
$ git remote add origin /tmp/main/

# Now add the submodule:
$ git submodule add -b gh-pages ./ gh-pages
Initialized empty Git repository in /tmp/main/gh-pages/.git/
Branch gh-pages set up to track remote branch gh-pages from origin.

# Now .gitmodules looks sensible:
$ cat .gitmodules 
[submodule "gh-pages"]
    path = gh-pages
    url = ./

# However, the origin for the submodule isn't what we want:
$ cd gh-pages
$ git remote -v
origin  /tmp/main/ (fetch)
origin  /tmp/main/ (push)

# So remove it and add the right origin (just ".."):
$ git remote rm origin
$ git remote add origin ..

# Change back to the main repository and commit:
$ cd ..
$ git commit -m "Added the gh-pages branch as a submodule of this repository"
[master 6849d53] Added the gh-pages branch as a submodule of this repository
 2 files changed, 4 insertions(+), 0 deletions(-)
 create mode 100644 .gitmodules
 create mode 160000 gh-pages

This seems to work OK - if I change into another directory and do:

$ cd /var/tmp
$ git clone --recursive /tmp/main/

... the submodule is updated and initialized correctly. (Update: although as you point out in a comment below, origin in the submodule will be set to the URL you cloned from rather than ..)

As for whether this is a good idea or not: I've worked on a project which used a similar setup in the past and which subsequently abandoned it. The reasons for this, however, were (a) that the alternative branches in the main repository were huge and bloated the repository even for people who didn't need the submodule and (b) that it caused confusion for people who weren't sure what was going on.

For your use case, however, I think it's a rather neat solution :)

这篇关于如何将git repo添加为自身的子模块? (或者:如何以编程方式生成GitHub页面?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆