git submodule 管理子工程
摘要:當多人共同維護一個專案時,必然需要進行模組化開發,所以使用submodule來管理子工程很有必要。本文以圖文並貌的形勢進行一步步搭建主工程及繫結子工程。
SZ-denglibing:~ fangdd$ cd /Harry/Projects/HDMaster-SubProject/HDMasterProject SZ-denglibing:HDMasterProject fangdd$ git init Initialized empty Git repository in /Harry/Projects/HDMaster-SubProject/HDMasterProject/.git/ SZ-denglibing:HDMasterProject fangdd$ git add . SZ-denglibing:HDMasterProject fangdd$ git commit -m '初始化工程' [master (root-commit) 10f33c6] 初始化工程 12 files changed, 639 insertions(+) create mode 100644 HDMasterProject.xcodeproj/project.pbxproj create mode 100644 HDMasterProject.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 HDMasterProject.xcodeproj/xcuserdata/fangdd.xcuserdatad/xcschemes/HDMasterProject.xcscheme create mode 100644 HDMasterProject.xcodeproj/xcuserdata/fangdd.xcuserdatad/xcschemes/xcschememanagement.plist create mode 100644 HDMasterProject/AppDelegate.h create mode 100644 HDMasterProject/AppDelegate.m create mode 100644 HDMasterProject/Base.lproj/LaunchScreen.storyboard create mode 100644 HDMasterProject/Base.lproj/Main.storyboard create mode 100644 HDMasterProject/Info.plist create mode 100644 HDMasterProject/ViewController.h create mode 100644 HDMasterProject/ViewController.m create mode 100644 HDMasterProject/main.m SZ-denglibing:HDMasterProject fangdd$ git remote add origin https://github.com/erduoniba/HDMasterProject.git SZ-denglibing:HDMasterProject fangdd$ git push origin master Counting objects: 21, done. Delta compression using up to 4 threads. Compressing objects: 100% (19/19), done. Writing objects: 100% (21/21), 7.45 KiB | 0 bytes/s, done. Total 21 (delta 2), reused 0 (delta 0) To https://github.com/erduoniba/HDMasterProject.git * [new branch] master -> master SZ-denglibing:HDMasterProject fangdd$ cd ./HD HDMasterProject/ HDMasterProject.xcodeproj/ SZ-denglibing:HDMasterProject fangdd$ cd ../ HDMasterProject/ HDSubProjectOne/ HDSubProjectTwo/ SZ-denglibing:HDMasterProject fangdd$ cd ../HDSubProjectOne/ SZ-denglibing:HDSubProjectOne fangdd$ git init Initialized empty Git repository in /Harry/Projects/HDMaster-SubProject/HDSubProjectOne/.git/ SZ-denglibing:HDSubProjectOne fangdd$ git add . SZ-denglibing:HDSubProjectOne fangdd$ git commit -m '初始化子工程1' [master (root-commit) 9c5c421] 初始化子工程1 6 files changed, 438 insertions(+) create mode 100644 HDSubProjectOne.xcodeproj/project.pbxproj create mode 100644 HDSubProjectOne.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 HDSubProjectOne.xcodeproj/xcuserdata/fangdd.xcuserdatad/xcschemes/HDSubProjectOne.xcscheme create mode 100644 HDSubProjectOne.xcodeproj/xcuserdata/fangdd.xcuserdatad/xcschemes/xcschememanagement.plist create mode 100644 HDSubProjectOne/HDSubProjectOne.h create mode 100644 HDSubProjectOne/Info.plist SZ-denglibing:HDSubProjectOne fangdd$ git remote add origin https://github.com/erduoniba/HDSubProjectOne.git SZ-denglibing:HDSubProjectOne fangdd$ git push origin master Counting objects: 14, done. Delta compression using up to 4 threads. Compressing objects: 100% (12/12), done. Writing objects: 100% (14/14), 4.56 KiB | 0 bytes/s, done. Total 14 (delta 0), reused 0 (delta 0) To https://github.com/erduoniba/HDSubProjectOne.git * [new branch] master -> master SZ-denglibing:HDSubProjectOne fangdd$ cd ../HDSubProjectTwo/ SZ-denglibing:HDSubProjectTwo fangdd$ git init Initialized empty Git repository in /Harry/Projects/HDMaster-SubProject/HDSubProjectTwo/.git/ SZ-denglibing:HDSubProjectTwo fangdd$ git add . SZ-denglibing:HDSubProjectTwo fangdd$ git commit -m '初始化子工程2' [master (root-commit) 9be396f] 初始化子工程2 6 files changed, 438 insertions(+) create mode 100644 HDSubProjectTwo.xcodeproj/project.pbxproj create mode 100644 HDSubProjectTwo.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 HDSubProjectTwo.xcodeproj/xcuserdata/fangdd.xcuserdatad/xcschemes/HDSubProjectTwo.xcscheme create mode 100644 HDSubProjectTwo.xcodeproj/xcuserdata/fangdd.xcuserdatad/xcschemes/xcschememanagement.plist create mode 100644 HDSubProjectTwo/HDSubProjectTwo.h create mode 100644 HDSubProjectTwo/Info.plist SZ-denglibing:HDSubProjectTwo fangdd$ git remote add origin https://github.com/erduoniba/HDSubProjectTwo.git SZ-denglibing:HDSubProjectTwo fangdd$ git push origin master Counting objects: 14, done. Delta compression using up to 4 threads. Compressing objects: 100% (12/12), done. Writing objects: 100% (14/14), 4.55 KiB | 0 bytes/s, done. Total 14 (delta 0), reused 0 (delta 0) To https://github.com/erduoniba/HDSubProjectTwo.git * [new branch] master -> master SZ-denglibing:HDSubProjectTwo fangdd$
2、分別在兩個子工程新增程式碼進行測試:
@implementation HDSubProjectMethodOne
+ (void)hdSubProjectMethodOne{
NSLog(@"hdSubProjectMethodOne");
}
@end
@implementation HDSubProjectMethodTwo
+ (void)hdSubProjectMethodTwo{
NSLog(@"hdSubProjectMethodTwo");
}
@end
然後在各自的HDSubProjectOne.h
和HDSubProjectTwo.h
新增對測試程式碼標頭檔案
3、新增子工程到主工程中(單純的新增,而不是子模組繫結)
開啟主工程,建立2個工程資料夾,分別將 ,如下:HDSubProjectOne.xcodeproj
和 HDSubProjectTwo.xcodeproj
拖到資料夾中
在主工程的AppDelegate中呼叫2個子工程的程式碼:
嗯哼,找不到檔案,看看HDSubProject
下面,發現什麼都沒有,這個是因為直接拖動.xcodeproj
只是引用,而沒有將程式碼拷貝:
欄位說明:
$(SRCROOT)
表示工程.xcodeproj
所在的相對路徑,比如我的電腦可能是harry/project/HDMasterProject/
也可能是hhh/xcodeProject/HDMasterProject/
"$(SRCROOT)/當前工程名字/需要包含標頭檔案所在資料夾"
“$(SRCROOT)”
,會自動變成當前工程所以的目錄。這樣就可以了,發給別人,別人也不用在去修改路徑了。
執行還是有問題,這個時候需要對子工程進行配置:
然後將子工程的動態庫加入到主工程中:
執行:
搞定!
4、新增子專案到主專案中(子模組繫結)
使用
git submodule add https://github.com/erduoniba/HDSubProjectOne.git
和
git submodule add https://github.com/erduoniba/HDSubProjectTwo.git
將子專案繫結到主專案中。
這個時候你會發現在HDMasterProject
會多出2個資料夾,這裡的程式碼其實就是對HDSubProjectOne
,HDSubProjectTwo2
個子專案的clone,以後的程式碼修改就是在這裡修改。
開啟.gitmodules
可以看到.gitmodules
記錄了每個submodule
的引用資訊,知道在當前專案的位置以及倉庫的所在。現在提交主及子專案專案的程式碼,通知你的小夥伴clone程式碼吧。
5、他人拉取程式碼:
現在因為只有一臺電腦,所以我將主專案clone到另外一個目錄(Desktop)下來 模擬他人操作:
發現我們的子專案並沒有clone下來,莫急:
在HDMasterProject中clone 2個子專案
git clone https://github.com/erduoniba/HDSubProjectOne.git
git clone https://github.com/erduoniba/HDSubProjectTwo.git
開啟clone下來的主專案,會出現這樣的問題:
這個是因為在這裡:
是因為第3步的 “開啟主工程,建立2個工程資料夾,分別將” 有問題,原因是 在建立工程時,我們是將下圖 1 中的子模組的.xcodeproj拖入到主專案的,但是HDMasterProject並沒有包含他們,解決方式:重新將2中的.xcodeproj拖入主專案,提交即可,相應的Destop下的主專案更新程式碼就OK了。HDSubProjectOne.xcodeproj
和 HDSubProjectTwo.xcodeproj
拖到資料夾中
執行:
6、子專案提交程式碼,他人更新:
甲同學提交子專案程式碼:
SZ-denglibing:HDMasterProject fangdd$ cd HDSubProjectOne/
SZ-denglibing:HDSubProjectOne fangdd$ git add .
SZ-denglibing:HDSubProjectOne fangdd$ git commit -m '新增測試程式碼'
[master 6bdc6eb] 新增測試程式碼
2 files changed, 6 insertions(+)
SZ-denglibing:HDSubProjectOne fangdd$ git push origin master
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 542 bytes | 0 bytes/s, done.
Total 5 (delta 3), reused 0 (delta 0)
To https://github.com/erduoniba/HDSubProjectOne.git
364abed..6bdc6eb master -> master
i. 乙同學更新甲同學的子專案程式碼 方式1 (到子專案直接更新)
SZ-denglibing:HDMasterProject fangdd$ cd HDSubProjectOne/
SZ-denglibing:HDSubProjectOne fangdd$ git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 3), reused 5 (delta 3), pack-reused 0
Unpacking objects: 100% (5/5), done.
From https://github.com/erduoniba/HDSubProjectOne
364abed..6bdc6eb master -> origin/master
Updating 364abed..6bdc6eb
Fast-forward
HDSubProjectOne/HDSubProjectMethodOne.h | 2 ++
HDSubProjectOne/HDSubProjectMethodOne.m | 4 ++++
2 files changed, 6 insertions(+)
ii.乙同學更新甲同學的子專案程式碼方式2 (到主專案使用git submodule update更新)
cd 主專案路徑
git submodule update --remote --merge
SZ-denglibing:HDMasterProject fangdd$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/erduoniba/HDMasterProject.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
可以看到git的config中並沒有任何關於子專案的資訊,解決辦法:
SZ-denglibing:HDMasterProject fangdd$ git submodule init
Submodule 'HDSubProjectOne' (https://github.com/erduoniba/HDSubProjectOne.git) registered for path 'HDSubProjectOne'
Submodule 'HDSubProjectTwo' (https://github.com/erduoniba/HDSubProjectTwo.git) registered for path 'HDSubProjectTwo'
SZ-denglibing:HDMasterProject fangdd$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/erduoniba/HDMasterProject.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[submodule "HDSubProjectOne"]
url = https://github.com/erduoniba/HDSubProjectOne.git
[submodule "HDSubProjectTwo"]
url = https://github.com/erduoniba/HDSubProjectTwo.git
現在趕緊試試吧 (大功告成!!)
7、git submodule 管理子專案相關命令:
在主專案更新子專案程式碼:
1、cd 主專案目錄
2、 git submodule update --remote --merge
在主專案提交子專案的程式碼:
1、cd 子專案目錄
2、git checkout master (將子目錄checkout到master分支上)
3、
git pull
git add .
git commit
git push