git命令--subtree
subtree 主要命令
git subtree add --prefix=<prefix> <commit>
git subtree add --prefix=<prefix> <repository> <ref>
git subtree pull --prefix=<prefix> <repository> <ref>
git subtree push --prefix=<prefix> <repository> <ref>
git subtree merge --prefix=<prefix> <commit>
git subtree split --prefix=<prefix> [OPTIONS] [<commit>]
项目地址
https://github.com/test/test_parent.git master
https://github.com/test/test_lib.git master
test_parent 文件结构
.
|--- .git
|--- testfile1
|--- testfile2
|--- sub
|--- subfile1
|--- subfile2
|--- lib
|--- libfile1
|--- libfile2
-
其中sub文件夹为subtree repo的目录
test_lib 文件结构
.
|--- .git
|--- subfile1
|--- subfile2
|--- lib
|--- libfile1
|--- libfile2
lib repo
lib仓库操作只要正常pull push 就可以 不受影响
parent repo
parent仓库下subtree操作如下
subtree add
parent添加test_lib到parent的sub目录
git subtree add --prefix=sub https://github.com/test/test_lib.git master --squash
(--squash参数表示不拉取历史信息,而只生成一条commit信息。)
subtree pull
parent的sub目录进行代码更新
git subtree pull --prefix=sub https://github.com/test/test_lib.git master --squash
从lib仓库拉取更新后,会在本地仓库自动生成commit信息,此时需要git push origin master , 这样普通parent仓库成员,才能正常拉取已经从lib仓库拉取得更新。
subtree push
git subtree push--prefix=sub https://github.com/test/test_lib.git master --squash
简化命令
使用remote建立子仓库,
git remote add -f lib https://github.com/test/test_lib.git
然后之前得三条subtree命令可以写成这样
git subtree add --prefix=sub lib master --squash
git subtree pull --prefix=sub lib master --squash
git subtree push --prefix=sub lib master
注:建立好子仓库后,简便了操作需要注意自己平时正常得pull操作已经push操作,主要是注意自己得pull和push得remote路径(有些人会直接使用git push或者TortriseGit直接pull或push得习惯),如果在parent直接正常push到lib仓库,一般情况会提醒你存在内容尚未pull下来,然后pull下来之后解决冲突或自动merge,此时如果继续push过去得就会将lib仓库完全替换为parent仓库
提示如下如果出现,请重新审查自己操作是否正确
Pushing to https://github.com/test/test_lib.git
To https://github.com/test/test_lib.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/test/test_lib.git
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.