subtree 主要命令github
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>]
項目地址fetch
https://github.com/test/test_parent.git master https://github.com/test/test_lib.git master
test_parent 文件結構code
. |--- .git |--- testfile1 |--- testfile2 |--- sub |--- subfile1 |--- subfile2 |--- lib |--- libfile1 |--- libfile2
其中sub文件夾爲subtree repo的目錄rem
test_lib 文件結構it
. |--- .git |--- subfile1 |--- subfile2 |--- lib |--- libfile1 |--- libfile2
lib倉庫操做只要正常pull push 就能夠 不受影響ast
parent倉庫下subtree操做以下class
parent添加test_lib到parent的sub目錄test
git subtree add --prefix=sub https://github.com/test/test_lib.git master --squash
(--squash參數表示不拉取歷史信息,而只生成一條commit信息。)sed
parent的sub目錄進行代碼更新
git subtree pull --prefix=sub https://github.com/test/test_lib.git master --squash
從lib倉庫拉取更新後,會在本地倉庫自動生成commit信息,此時須要git push origin master , 這樣普通parent倉庫成員,才能正常拉取已經從lib倉庫拉取得更新。
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.