没有分离头的Git子模块?

hol*_*lms 3 git git-submodules

也许我没有故意使用git-submodules,但如果有任何其他git功能满足我的情况,那么很高兴找到它.

在克隆存储库之后,我希望子模块在master分支中,我永远不会在chef-starter子模块中存储任何其他文件.

git clone git@github.com:holms/vagrant-starter.git
git submodule update --init
cd chef-starter
git branch
   * (detached from 0b6a803)
   master
Run Code Online (Sandbox Code Playgroud)

我确实理解这是一个很好的功能,分别跟踪该子模块目录树,但它不是我的情况.在主存储库克隆之后,我只想将该子模块克隆到最新的主阶段而不需要任何额外的麻烦.

我怎样才能做到这一点?

Ste*_*ngs 8

子模块有一个分离的头,因为子模块意味着"从子模块的存储库中检出特定的提交".该master分支可能已经向前移动(它可能指向一个承诺,从承诺下降0b6a803),所以混帐签出特定修订,而不是检查出的一个分支.

git-submodule可以选择记录分支名称.执行此操作时,您可以使用git submodule update --remote该分支更新子模块:

# Add the submodule with the "master" branch referenced
git submodule add -b master https://github.com/holms/chef-starter.git chef-starter

# When you want to update the submodule:
git submodule update --remote

# You'll still need to commit the submodule if you want future checkouts to use the new revision
git add chef-starter
git commit -m "Update chef-starter submodule"
Run Code Online (Sandbox Code Playgroud)

当你开始检查超级项目(vagarant-starter)的不同版本时,你仍然会在子模块中得到一个分离的头,但至少现在更容易从远程更新子模块到最新版本.

您可能会发现使用git-subtree而不是子模块更容易:

# First, get rid of the submodule
git submodule deinit chef-starter # If you have Git 1.8.3 or later, otherwise edit .gitmodules
git rm chef-starter               # Remove the directory
git commit -m "Removed chef-starter submodule in preparation to add as a subtree"

# Now add the subtree
git subtree add -P chef-starter https://github.com/holms/chef-starter master --squash
Run Code Online (Sandbox Code Playgroud)

这会将chef-starterrepo 移植到repo中的chef-starter/目录中vagrant-starter.从那时起,如果您选择编辑子树中的文件,则不必执行任何特殊操作.例如,您不必git submodule update在克隆后运行.


cma*_*han 6

为了确保我的子模块保持同步且未分离,我在批处理文件中包含以下内容。它依赖于采用同步到子模块的主分支的策略 - 对我来说这不是问题,我只是分叉子模块并确保它有一个主分支。我发现用这种方法来跟踪事情要容易得多。

git pull
git submodule sync    # Ensure the submodule points to the right place
git submodule update  # Update the submodule  
git submodule foreach git checkout master  # Ensure subs are on master branch
git submodule foreach git pull origin master # Pull the latest master
Run Code Online (Sandbox Code Playgroud)