克隆超级项目后如何保持git子模块分支状态? [英] how to keep git submodule on-branch status after cloning superproject?

查看:407
本文介绍了克隆超级项目后如何保持git子模块分支状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试新的 git submodule add -b 特性(在git 1.8.2之后),它创建了涉嫌跟踪分支而不是提交的子模块。我正在使用 git 1.8.4.msysgit.0 。子模块的分支跟踪功能似乎在原来的超级项目中运行良好,但只要超级项目被克隆就会失败。更具体地说:

我做的是典型的,大致如下:

  1。创建一个git repo(称为common):... 
2.创建一个主要项目(称为main),它使用通用的库/子模块。
mkdir main&& cd main
git init
git子模块add -b master url_to_common.git
git commit -m初始提交
cd常用
git状态

如上所述,添加的子模块跟踪子模块回购的主分支。我得到了:
$ b $ pre $ #在分支master
上没有提交,工作目录干净

另外,如果我做了 git pull git push ,我得到

 已经是最新的。 
一切都是最新的



然而,如果我以任何方式克隆 main 项目,那么 common 子模块克隆项目失去On Branch状态。我不能 git pull 或 git push common main 项目中的文件夹。当然,我可以添加 origin master 来为克隆的项目中的 common 进行推拉工作,但这似乎是打破追踪子模块(子模块add -b)的目的。

我用来克隆和检查子模块的命令为:

  cd main 
git clone。 ../main2 --recursive
cd ../main2/common
git status

我得到了:
$ b $

 #HEAD分离为0259d75 
没有提交,工作目录干净

我也试过 git clone。 ../main3 --recurse-submodules ,以及,

  git clone。 ../main4.git --bare 
git clone url_to_main4.git - 递归

同样的事情发生在 main3 main4 中。总而言之,我创建了一个超级项目,其子模块跟踪其主分支。一旦我试图克隆它并在其他地方工作,分支跟踪功能就会丢失。在我看来,保持分支跟踪子模块的唯一方法是压缩原来的 main 项目并复制它。我在这种情况下是否遗漏了某些东西,或者正在将最初的项目复制到最佳状态?



我的问题是,如何在克隆后保持分支跟踪功能超级项目。我特别感兴趣的是使 main4.git 工作,因为它涉及远程服务器上的裸克隆。



注意:在分支丢失后指定分支(例如, Git子模块:指定分支/标记)不是我正在寻找的,因为 git子模块中添加的信息add -b 仍然丢失,我们又回到原点了。

解决方案

对于存在这个问题的其他人:



如答案所述,您的子模块跟踪提交。这实际上不是问题,子模块在单个时间点(即提交)表示外部依赖关系,而不是活跃的开发流(分支)。你可以手动更新这个依赖关系(理论上,只有在master分支上)



在提交时你只关心你的分支。承诺如果不在一个分支上,就会造成一个分离的头脑,而且开始很容易让你的团队失去工作。



我在我们的工作室解决了这个问题,如果不在分支上,提交钩拒绝提交:

 #!/ bin / sh 

函数parse_git_branch_check {
if [[$ {branch_name} ==*(从*分离);然后
echo****************** **************************
echo你需要在提交
echo之前在分支上* *******************************************
出口1
else
echo - 你在分支$ branch_name -
exit 0
fi
}
function parse_git_branch {
git分支 - 无色2> / dev / null | sed -e'/ ^ [^ *] / d'
}

branch_name = $(parse_git_branch)
parse_git_branch_check ;

这个钩子与这个BAT一起致力于超级回购(是的,我知道,对不起)scr ipt安装钩子

  REM  - 为所有子模块创建预连接钩子的链接,以防止没有分支的提交

if EXIST .git\hooks\pre-commit(
del .git\hooks\pre-commit

mklink / h .git\ hooks\pre-commit pre-commit

FOR / Ftokens = *%% i IN('DIR .git \modules / A:D / b')do(
if EXIST .git\modules\ %% i\hooks\pre-commit(
del .git\modules\ %% i\hooks\pre-commit

mklink / h .git \modules \ %% i \ hooks \ pre-commit pre-commit

新的克隆需要运行该脚本,但这并不完美,但最接近我想出的自动解决方案。


I am testing the new git submodule add -b feature (after git 1.8.2), which creates sub-module that allegedly tracks a branch rather than a commit. I am using git version 1.8.4.msysgit.0. The branch-tracking feature for submodules seems to work fine in the original super-project but fails as soon as the super-project is cloned. To be more specific:

What I did is typical, and is roughly as follows,

1. create a git repo (called common): ...
2. create a main project (called main), which uses common as a library/submodule.
 mkdir main && cd main
 git init
 git submodule add -b master url_to_common.git
 git commit -m "initial commit"
 cd common
 git status

As advertised, the added submodule tracks the master branch of the submodule repo. And I got:

# On branch master
nothing to commit, working directory clean

In addition, if I do git pull or git push, I get

Already up-to-date.
Everything up-to-date

, respectively.

However, if I clone the main project in any way, the common submodule in the cloned project loses the "On branch" status. And I couldn't git pull or git push inside the common folder as in the prototype main project. Of course, I can add origin master to make pull and push work for common in the cloned project, but this seems to defeat the purpose of having a tracking submodule (submodule add -b).

The commands I used to clone and check submodules were:

cd main
git clone . ../main2 --recursive
cd ../main2/common
git status

I got:

# HEAD detached at 0259d75
nothing to commit, working directory clean

I also tried git clone . ../main3 --recurse-submodules, as well as,

git clone . ../main4.git --bare
git clone url_to_main4.git --recursive

the same thing happens to main3 and main4.

In sum, I created a super-project with a submodule tracking its master branch. The branch-tracking capability is lost as soon as I tried to clone it and work on it elsewhere. It seems to me that the only way to keep branch-tracking submodules is to compress the original main project and copy it around. Did I missing something in this case, or is copying the original project around the best I can have?

My question is, how to keep the branch-tracking capability after cloning the super-project. I am particularly interested in making main4.git work because it involves a bare clone on a remote server.

Note: specifying a branch after the branch is lost (e.g. Git submodules: Specify a branch/tag) is not what I am looking for, because the info given in git submodule add -b is still lost, and we are back to square one. We might as well delete the submodule and add it again.

解决方案

For anyone else with this problem:

As the answers have stated, your submodule tracks a commit. This isn't actually an problem, a submodule represents an external dependency at a single point in time (ie, a commit) rather than an active stream of development (a branch). You manually update this dependency at your choosing (and theoretically, are only ever on Master branch)

You only really care about your branch when making commits. Committing without being on a branch makes a detached head, and it starts being easy for your team to lose its work.

I solved this at our studio by adding a pre-commit hook to reject commits if not on a branch:

#!/bin/sh

function parse_git_branch_check {
    if [[ ${branch_name} == "* (detached from "* ]]; then
        echo "********************************************"
        echo "You need to be on a branch before committing"
        echo "********************************************"
        exit 1
    else
        echo "-- You are on branch $branch_name --"
        exit 0
    fi
}
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d'
}

branch_name=$(parse_git_branch)
parse_git_branch_check;

This hook is committed to the super repo, along with this BAT (yes, I know, sorry) script to install the hooks

REM - Create links for all submodules to our pre-commit hooks for preventing submissions without a branch

if EXIST .git\hooks\pre-commit (
    del .git\hooks\pre-commit
)
mklink /h .git\hooks\pre-commit pre-commit

FOR /F "tokens=*" %%i IN ('DIR .git\modules /A:D /b') do (
    if EXIST .git\modules\%%i\hooks\pre-commit (
        del .git\modules\%%i\hooks\pre-commit
    )
    mklink /h .git\modules\%%i\hooks\pre-commit pre-commit
)

New clones need to run the script, which is not perfect, but the closest to an automatic solution I could come up with.

这篇关于克隆超级项目后如何保持git子模块分支状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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