當(dāng)前位置:首頁 >  站長 >  編程技術(shù) >  正文

Git Submodule使用完整教程(小結(jié))

 2020-11-13 14:41  來源: 腳本之家   我來投稿 撤稿糾錯

  域名預(yù)訂/競價,好“米”不錯過

這篇文章主要介紹了Git Submodule使用完整教程(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

自從看了蔣鑫的《Git權(quán)威指南》之后就開始使用Git Submodule功能,團(tuán)隊(duì)也都熟悉了怎么使用,多個子系統(tǒng)(模塊)都能及時更新到最新的公共資源,把使用的過程以及經(jīng)驗(yàn)和容易遇到的問題分享給大家。

Git Submodule功能剛剛開始學(xué)習(xí)可能覺得有點(diǎn)怪異,所以本教程把每一步的操作的命令和結(jié)果都用代碼的形式展現(xiàn)給大家,以便更好的理解。

1.對于公共資源各種程序員的處理方式

每個公司的系統(tǒng)都會有一套統(tǒng)一的系統(tǒng)風(fēng)格,或者針對某一個大客戶的多個系統(tǒng)風(fēng)格保持統(tǒng)一,而且如果風(fēng)格改動后要同步到多個系統(tǒng)中;這樣的需求幾乎每個開發(fā)人員都遇到,下面看看各個層次的程序員怎么處理:

假如對于系統(tǒng)的風(fēng)格需要幾個目錄:css、images、js。

普通程序員,把最新版本的代碼逐個復(fù)制到每個項(xiàng)目中,如果有N個項(xiàng)目,那就是要復(fù)制N x 3次;如果漏掉了某個文件夾沒有復(fù)制…@(&#@#。

文藝程序員,使用Git Submodule功能,執(zhí)行:git submodule update,然后沖一杯咖啡悠哉的享受著。

引用一段《Git權(quán)威指南》的話: 項(xiàng)目的版本庫在某些情況蝦需要引用其他版本庫中的文件,例如公司積累了一套常用的函數(shù)庫,被多個項(xiàng)目調(diào)用,顯然這個函數(shù)庫的代碼不能直接放到某個項(xiàng)目的代碼中,而是要獨(dú)立為一個代碼庫,那么其他項(xiàng)目要調(diào)用公共函數(shù)庫該如何處理呢?分別把公共函數(shù)庫的文件拷貝到各自的項(xiàng)目中會造成冗余,丟棄了公共函數(shù)庫的維護(hù)歷史,這顯然不是好的方法。

2.開始學(xué)習(xí)Git Submodule

“工欲善其事,必先利其器”!

既然文藝程序員那么輕松就搞定了,那我們就把過程一一道來。

說明:本例采用兩個項(xiàng)目以及兩個公共類庫演示對submodule的操作。因?yàn)樵谝粚戀Y料或者書上的例子都是一個項(xiàng)目對應(yīng)1~N個lib,但是實(shí)際應(yīng)用往往并不是這么簡單。

2.1 創(chuàng)建Git Submodule測試項(xiàng)目

2.1.1 準(zhǔn)備環(huán)境

? henryyan@hy-hp ~ pwd
/home/henryyan
mkdir -p submd/repos

創(chuàng)建需要的本地倉庫:

cd ~/submd/repos
git --git-dir=lib1.git init --bare
git --git-dir=lib2.git init --bare
git --git-dir=project1.git init --bare
git --git-dir=project2.git init --bare

初始化工作區(qū):

mkdir ~/submd/ws
cd ~/submd/ws

2.1.2 初始化項(xiàng)目

初始化project1:

? henryyan@hy-hp ~/submd/ws git clone ../repos/project1.git
Cloning into project1...
done.
warning: You appear to have cloned an empty repository.

? henryyan@hy-hp ~/submd/ws cd project1
? henryyan@hy-hp ~/submd/ws/project1 git:(master) echo "project1" > project-infos.txt
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? ls
project-infos.txt

? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git add project-infos.txt
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: project-infos.txt
#
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git commit -m "init project1"
[master (root-commit) 473a2e2] init project1
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 project-infos.txt
? henryyan@hy-hp ~/submd/ws/project1 git:(master) git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 232 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/henryyan/submd/ws/../repos/project1.git
* [new branch] master -> master
</file>

初始化project2:

? henryyan@hy-hp ~/submd/ws/project1 cd ..
? henryyan@hy-hp ~/submd/ws git clone ../repos/project2.git
Cloning into project2...
done.
warning: You appear to have cloned an empty repository.

? henryyan@hy-hp ~/submd/ws cd project2
? henryyan@hy-hp ~/submd/ws/project2 git:(master) echo "project2" > project-infos.txt
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? ls
project-infos.txt

? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git add project-infos.txt
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: project-infos.txt
#
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git commit -m "init project2"
[master (root-commit) 473a2e2] init project2
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 project-infos.txt
? henryyan@hy-hp ~/submd/ws/project2 git:(master) git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 232 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/henryyan/submd/ws/../repos/project2.git
* [new branch] master -> master
</file>

2.1.3 初始化公共類庫

初始化公共類庫lib1:

? henryyan@hy-hp ~/submd/ws git clone ../repos/lib1.git
Cloning into lib1...
done.
warning: You appear to have cloned an empty repository.
? henryyan@hy-hp ~/submd/ws cd lib1
? henryyan@hy-hp ~/submd/ws/lib1 git:(master) echo "I'm lib1." > lib1-features
? henryyan@hy-hp ~/submd/ws/lib1 git:(master) ? git add lib1-features
? henryyan@hy-hp ~/submd/ws/lib1 git:(master) ? git commit -m "init lib1"
[master (root-commit) c22aff8] init lib1
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 lib1-features
? henryyan@hy-hp ~/submd/ws/lib1 git:(master) git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 227 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/henryyan/submd/ws/../repos/lib1.git
* [new branch] master -> master

初始化公共類庫lib2:

? henryyan@hy-hp ~/submd/ws/lib1 git:(master) cd ..
? henryyan@hy-hp ~/submd/ws git clone ../repos/lib2.git
Cloning into lib2...
done.
warning: You appear to have cloned an empty repository.
? henryyan@hy-hp ~/submd/ws cd lib2
? henryyan@hy-hp ~/submd/ws/lib2 git:(master) echo "I'm lib2." > lib2-features
? henryyan@hy-hp ~/submd/ws/lib2 git:(master) ? git add lib2-features
? henryyan@hy-hp ~/submd/ws/lib2 git:(master) ? git commit -m "init lib2"
[master (root-commit) c22aff8] init lib2
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 lib2-features
? henryyan@hy-hp ~/submd/ws/lib2 git:(master) git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 227 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/henryyan/submd/ws/../repos/lib2.git
* [new branch] master -> master

2.2 為主項(xiàng)目添加Submodules

2.2.1 為project1添加lib1和lib2

? henryyan@hy-hp ~/submd/ws/lib2 git:(master) cd ../project1
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ls
project-infos.txt
? henryyan@hy-hp ~/submd/ws/project1 git:(master) git submodule add ~/submd/repos/lib1.git libs/lib1
Cloning into libs/lib1...
done.
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git submodule add ~/submd/repos/lib2.git libs/lib2
Cloning into libs/lib2...
done.
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? ls
libs project-infos.txt
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? ls libs
lib1 lib2

? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: .gitmodules
# new file: libs/lib1
# new file: libs/lib2
#

# 查看一下公共類庫的內(nèi)容

? henryyan@hy-hp ~/submd/ws/project1 git:(master) cat libs/lib1/lib1-features
I'm lib1.
? henryyan@hy-hp ~/submd/ws/project1 git:(master) cat libs/lib2/lib2-features
I'm lib2.
</file>

好了,到目前為止我們已經(jīng)使用git submodule add命令為project1成功添加了兩個公共類庫(lib1、lib2),查看了當(dāng)前的狀態(tài)發(fā)現(xiàn)添加了一個新文件(.gitmodules)和兩個文件夾(libs/lib1、libs/lib2);那么新增的.gitmodules文件是做什么用的呢?我們查看一下文件內(nèi)容便知曉了:

n@hy-hp ~/submd/ws/project1 git:(master) ? cat .gitmodules
[submodule "libs/lib1"]
path = libs/lib1
url = /home/henryyan/submd/repos/lib1.git
[submodule "libs/lib2"]
path = libs/lib2
url = /home/henryyan/submd/repos/lib2.git

原來如此,.gitmodules記錄了每個submodule的引用信息,知道在當(dāng)前項(xiàng)目的位置以及倉庫的所在。

好的,我們現(xiàn)在把更改提交到倉庫。

? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git commit -a -m "add submodules[lib1,lib2] to project1"
[master 7157977] add submodules[lib1,lib2] to project1
3 files changed, 8 insertions(+), 0 deletions(-)
create mode 100644 .gitmodules
create mode 160000 libs/lib1
create mode 160000 libs/lib2

? henryyan@hy-hp ~/submd/ws/project1 git:(master) git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 491 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
To /home/henryyan/submd/ws/../repos/project1.git
45cbbcb..7157977 master -> master

假如你是第一次引入公共類庫的開發(fā)人員,那么項(xiàng)目組的其他成員怎么Clone帶有Submodule的項(xiàng)目呢,下面我們再clone一個項(xiàng)目講解如何操作。

2.3 Clone帶有Submodule的倉庫

模擬開發(fā)人員B……

? henryyan@hy-hp ~/submd/ws/project1 git:(master) cd ~/submd/ws
? henryyan@hy-hp ~/submd/ws git clone ../repos/project1.git project1-b
Cloning into project1-b...
done.
? henryyan@hy-hp ~/submd/ws cd project1-b
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) git submodule
-c22aff85be91eca442734dcb07115ffe526b13a1 libs/lib1
-7290dce0062bd77df1d83b27dd3fa3f25a836b54 libs/lib2

看到submodules的狀態(tài)是hash碼和文件目錄,但是注意前面有一個減號:-,含義是該子模塊還沒有檢出。

OK,檢出project1-b的submodules……

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) git submodule init
Submodule 'libs/lib1' (/home/henryyan/submd/repos/lib1.git) registered for path 'libs/lib1'
Submodule 'libs/lib2' (/home/henryyan/submd/repos/lib2.git) registered for path 'libs/lib2'
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) git submodule update
Cloning into libs/lib1...
done.
Submodule path 'libs/lib1': checked out 'c22aff85be91eca442734dcb07115ffe526b13a1'
Cloning into libs/lib2...
done.
Submodule path 'libs/lib2': checked out '7290dce0062bd77df1d83b27dd3fa3f25a836b54'

讀者可以查看:.git/config文件的內(nèi)容,最下面有submodule的注冊信息!

驗(yàn)證一下類庫的文件是否存在:

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) cat libs/lib1/lib1-features libs/lib2/lib2-features
I'm lib1.
I'm lib2.

上面的兩個命令(git submodule init & update)其實(shí)可以簡化,后面會講到!

2.3 修改Submodule

我們在開發(fā)人員B的項(xiàng)目上修改Submodule的內(nèi)容。

先看一下當(dāng)前Submodule的狀態(tài):

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) cd libs/lib1
? henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git status
# Not currently on any branch.
nothing to commit (working directory clean)

為什么是Not currently on any branch呢?不是應(yīng)該默認(rèn)在master分支嗎?別急,一一解答!

Git對于Submodule有特殊的處理方式,在一個主項(xiàng)目中引入了Submodule其實(shí)Git做了3件事情:

記錄引用的倉庫

記錄主項(xiàng)目中Submodules的目錄位置

記錄引用Submodule的commit id

在project1中push之后其實(shí)就是更新了引用的commit id,然后project1-b在clone的時候獲取到了submodule的commit id,然后當(dāng)執(zhí)行g(shù)it submodule update的時候git就根據(jù)gitlink獲取submodule的commit id,最后獲取submodule的文件,所以clone之后不在任何分支上;但是master分支的commit id和HEAD保持一致。

查看~/submd/ws/project1-b/libs/lib1的引用信息:

? henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 cat .git/HEAD
c22aff85be91eca442734dcb07115ffe526b13a1
? henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 cat .git/refs/heads/master
c22aff85be91eca442734dcb07115ffe526b13a1

現(xiàn)在我們要修改lib1的文件需要先切換到master分支:

? henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git checkout master
Switched to branch 'master'
? henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git:(master) echo "add by developer B" >> lib1-features
? henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git:(master) ? git commit -a -m "update lib1-features by developer B"
[master 36ad12d] update lib1-features by developer B
1 files changed, 1 insertions(+), 0 deletions(-)

在主項(xiàng)目中修改Submodule提交到倉庫稍微繁瑣一點(diǎn),在git push之前我們先看看project1-b狀態(tài):

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: libs/lib1 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
</file></file>

libs/lib1 (new commits)狀態(tài)表示libs/lib1有新的提交,這個比較特殊,看看project1-b的狀態(tài):

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? git diff
diff --git a/libs/lib1 b/libs/lib1
index c22aff8..36ad12d 160000
--- a/libs/lib1
+++ b/libs/lib1
@@ -1 +1 @@
-Subproject commit c22aff85be91eca442734dcb07115ffe526b13a1
+Subproject commit 36ad12d40d8a41a4a88a64add27bd57cf56c9de2

從狀態(tài)中可以看出libs/lib1的commit id由原來的c22aff85be91eca442734dcb07115ffe526b13a1更改為36ad12d40d8a41a4a88a64add27bd57cf56c9de2

注意:如果現(xiàn)在執(zhí)行了git submodule update操作那么libs/lib1的commit id又會還原到c22aff85be91eca442734dcb07115ffe526b13a1,

這樣的話剛剛的修改是不是就丟死了呢?不會,因?yàn)樾薷囊呀?jīng)提交到了master分支,只要再git checkout master就可以了。

現(xiàn)在可以把libs/lib1的修改提交到倉庫了:

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? cd libs/lib1
? henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git:(master) git push
Counting objects: 5, done.
Writing objects: 100% (3/3), 300 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/henryyan/submd/repos/lib1.git
c22aff8..36ad12d master -> master

現(xiàn)在僅僅只完成了一步,下一步要提交project1-b引用submodule的commit id:

? henryyan@hy-hp ~/submd/ws/project1-b/libs/lib1 git:(master) cd ~/submd/ws/project1-b
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? git add -u
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? git commit -m "update libs/lib1 to lastest commit id"
[master c96838a] update libs/lib1 to lastest commit id
1 files changed, 1 insertions(+), 1 deletions(-)
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 395 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/henryyan/submd/ws/../repos/project1.git
7157977..c96838a master -> master

OK,大功高成,我們完成了Submodule的修改并把libs/lib1的最新commit id提交到了倉庫。

接下來要看看project1怎么獲取submodule了。

2.4 更新主項(xiàng)目的Submodules

好的,讓我們先進(jìn)入project1目錄同步倉庫:

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) cd ../project1
? henryyan@hy-hp ~/submd/ws/project1 git:(master) git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/henryyan/submd/ws/../repos/project1
7157977..c96838a master -> origin/master
Updating 7157977..c96838a
Fast-forward
libs/lib1 | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: libs/lib1 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
</file></file>

我們運(yùn)行了git pull命令和git status獲取了最新的倉庫源碼,然后看到了狀態(tài)時modified,這是為什么呢?

我們用git diff比較一下不同:

? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git diff
diff --git a/libs/lib1 b/libs/lib1
index 36ad12d..c22aff8 160000
--- a/libs/lib1
+++ b/libs/lib1
@@ -1 +1 @@
-Subproject commit 36ad12d40d8a41a4a88a64add27bd57cf56c9de2
+Subproject commit c22aff85be91eca442734dcb07115ffe526b13a1
從diff的結(jié)果分析出來時因?yàn)閟ubmodule的commit id更改了,我們前面剛剛講了要在主項(xiàng)目更新submodule的內(nèi)容首先要提交submdoule的內(nèi)容,然后再更新主項(xiàng)目中引用的submodulecommit id;現(xiàn)在我們看到的不同就是因?yàn)閯倓偢牧藀roject1-b的submodule commit id;好的,我來學(xué)習(xí)一下怎么更新project1的公共類庫。

follow me……

? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git submodule update
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: libs/lib1 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
</file></file>

為什么沒有更新?git submodule update命令不是更新子模塊倉庫的嗎?

別急,先聽我解釋;因?yàn)樽幽K是在project1中引入的,git submodule add ~/submd/repos/lib1.git libs/lib1命令的結(jié)果,操作之后git只是把lib1的內(nèi)容clone到了project1中,但是沒有在倉庫注冊,證據(jù)如下:

? henryyan@hy-hp ~/submd2/ws/project1 git:(master) ? cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = /home/henryyan/submd/ws/../repos/project1.git
[branch "master"]
remote = origin
merge = refs/heads/master

我們說過git submodule init就是在.git/config中注冊子模塊的信息,下面我們試試注冊之后再更新子模塊:

? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git submodule init
Submodule 'libs/lib1' (/home/henryyan/submd/repos/lib1.git) registered for path 'libs/lib1'
Submodule 'libs/lib2' (/home/henryyan/submd/repos/lib2.git) registered for path 'libs/lib2'
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git submodule update
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/henryyan/submd/repos/lib1
c22aff8..36ad12d master -> origin/master
Submodule path 'libs/lib1': checked out '36ad12d40d8a41a4a88a64add27bd57cf56c9de2'

? henryyan@hy-hp ~/submd/ws/project1 git:(master) cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = /home/henryyan/submd/ws/../repos/project1.git
[branch "master"]
remote = origin
merge = refs/heads/master
[submodule "libs/lib1"]
url = /home/henryyan/submd/repos/lib1.git
[submodule "libs/lib2"]
url = /home/henryyan/submd/repos/lib2.git

? henryyan@hy-hp ~/submd/ws/project1 git:(master) cat libs/lib1/lib1-features
I'm lib1.
add by developer B

上面的結(jié)果足以證明剛剛的推斷,所以記得當(dāng)需要更新子模塊的內(nèi)容時請先確保已經(jīng)運(yùn)行過git submodule init。

2.5 為project2添加lib1和lib2

這個操作對于讀到這里的你來說應(yīng)該是輕車熟路了,action:

? henryyan@hy-hp ~/submd/ws/project1 git:(master) cd ~/submd/ws/project2
? henryyan@hy-hp ~/submd/ws/project2 git:(master) git submodule add ~/submd/repos/lib1.git libs/lib1
Cloning into libs/lib1...
done.
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git submodule add ~/submd/repos/lib2.git libs/lib2
zsh: correct 'libs/lib2' to 'libs/lib1' [nyae]? n
Cloning into libs/lib2...
done.
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? ls
libs project-infos.txt
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git submodule init
Submodule 'libs/lib1' (/home/henryyan/submd/repos/lib1.git) registered for path 'libs/lib1'
Submodule 'libs/lib2' (/home/henryyan/submd/repos/lib2.git) registered for path 'libs/lib2'

? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: .gitmodules
# new file: libs/lib1
# new file: libs/lib2
#
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git commit -a -m "add lib1 and lib2"
[master 8dc697f] add lib1 and lib2
3 files changed, 8 insertions(+), 0 deletions(-)
create mode 100644 .gitmodules
create mode 160000 libs/lib1
create mode 160000 libs/lib2
? henryyan@hy-hp ~/submd/ws/project2 git:(master) git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 471 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
To /home/henryyan/submd/ws/../repos/project2.git
6e15c68..8dc697f master -> master

</file>

我們依次執(zhí)行了添加submodule并commit和push到倉庫,此階段任務(wù)完成。

2.6 修改lib1和lib2并同步到project1和project2

假如開發(fā)人員C同時負(fù)責(zé)project1和project2,有可能在修改project1的某個功能的時候發(fā)現(xiàn)lib1或者lib2的某個組件有bug需要修復(fù),這個需求多模塊和大型系統(tǒng)中經(jīng)常遇到,我們應(yīng)該怎么解決呢?

假如我的需求如下:

在lib1中添加一個文件:README,用來描述lib1的功能

在lib2中的lib2-features文件中添加一寫文字:學(xué)習(xí)Git submodule的修改并同步功能

2.6.1 在lib1中添加一個文件:README

? henryyan@hy-hp ~/submd/ws/project2 git:(master) cd libs/lib1
? henryyan@hy-hp ~/submd/ws/project2/libs/lib1 git:(master) echo "lib1 readme contents" > README
? henryyan@hy-hp ~/submd/ws/project2/libs/lib1 git:(master) ? git add README
? henryyan@hy-hp ~/submd/ws/project2/libs/lib1 git:(master) ? git commit -m "add file README"
[master 8c666d8] add file README
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README
? henryyan@hy-hp ~/submd/ws/project2/libs/lib1 git:(master) git push
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 310 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/henryyan/submd/repos/lib1.git
36ad12d..8c666d8 master -> master

前面提到過現(xiàn)在僅僅只完成了一部分,我們需要在project2中再更新lib1的commit id:

? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: libs/lib1 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git add libs/lib1
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git commit -m "update lib1 to lastest commit id"
[master ce1f3ba] update lib1 to lastest commit id
1 files changed, 1 insertions(+), 1 deletions(-)
</file></file>

我們暫時不push到倉庫,等待和lib2的修改一起push。

2.6.2 在lib2中的lib2-features文件添加文字

? henryyan@hy-hp ~/submd/ws/project2 git:(master) cd libs/lib2
? henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) echo "學(xué)習(xí)Git submodule的修改并同步功能" >> lib2-features
? henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) ? git add lib2-features
? henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) ? git commit -m "添加文字:學(xué)習(xí)Git submodule的修改并同步功能"
[master e372b21] 添加文字:學(xué)習(xí)Git submodule的修改并同步功能
1 files changed, 1 insertions(+), 0 deletions(-)
? henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 376 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/henryyan/submd/repos/lib2.git
7290dce..e372b21 master -> master

? henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) echo "學(xué)習(xí)Git submodule的修改并同步功能" >> lib2-features
? henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) ? git add lib2-features
? henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) ? git commit -m "添加文字:學(xué)習(xí)Git submodule的修改并同步功能"
[master e372b21] 添加文字:學(xué)習(xí)Git submodule的修改并同步功能
1 files changed, 1 insertions(+), 0 deletions(-)
? henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 376 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/henryyan/submd/repos/lib2.git
7290dce..e372b21 master -> master
? henryyan@hy-hp ~/submd/ws/project2/libs/lib2 git:(master) cd -
~/submd/ws/project2
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: libs/lib2 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git add libs/lib2
? henryyan@hy-hp ~/submd/ws/project2 git:(master) ? git commit -m "update lib2 to lastest commit id"
[master df344c5] update lib2 to lastest commit id
1 files changed, 1 insertions(+), 1 deletions(-)
? henryyan@hy-hp ~/submd/ws/project2 git:(master) git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
nothing to commit (working directory clean)
? henryyan@hy-hp ~/submd/ws/project2 git:(master) git push
Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 776 bytes, done.
Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
To /home/henryyan/submd/ws/../repos/project2.git
8dc697f..df344c5 master -> master
</file></file>

2.7 同步project2的lib1和lib2的修改到project1

現(xiàn)在project2已經(jīng)享受到了最新的代碼帶來的快樂,那么既然project1和project2屬于同一個風(fēng)格,或者調(diào)用同一個功能,要讓這兩個(可能幾十個)項(xiàng)目保持一致。

? henryyan@hy-hp ~/submd/ws/project2 git:(master) cd ../project1
? henryyan@hy-hp ~/submd/ws/project1 git:(master) git pull
Already up-to-date.

看看上面的結(jié)果對嗎?為什么lib1和lib2更新了但是沒有顯示new commits呢?說到這里我記得剛剛開始學(xué)習(xí)的時候真得要暈死了,Git跟我玩捉迷藏游戲,為什么我明明提交了但是從project1更新不到任何改動呢?

幫大家分析一下問題,不過在分析之前先看看當(dāng)前(project1和project2)的submodule狀態(tài):

# project2 的狀態(tài),也就是我們剛剛修改后的狀態(tài)
? henryyan@hy-hp ~/submd/ws/project2 git:(master) git submodule
8c666d86531513dd1aebdf235f142adbac72c035 libs/lib1 (heads/master)
e372b21dffa611802c282278ec916b5418acebc2 libs/lib2 (heads/master)

# project1 的狀態(tài),等待更新submodules
? henryyan@hy-hp ~/submd/ws/project1 git:(master) git submodule
36ad12d40d8a41a4a88a64add27bd57cf56c9de2 libs/lib1 (remotes/origin/HEAD)
7290dce0062bd77df1d83b27dd3fa3f25a836b54 libs/lib2 (heads/master)

兩個項(xiàng)目有兩個區(qū)別:

commit id各不相同

libs/lib1所處的分支不同

2.7.1 更新project1的lib1和lib2改動

我們還記得剛剛在project2中修改的時候把lib1和lib2都切換到了master分支,目前project1中的lib1不在任何分支,我們先切換到master分支:

? henryyan@hy-hp ~/submd/ws/project1 git:(master) cd libs/lib1
? henryyan@hy-hp ~/submd/ws/project1/libs/lib1 git checkout master
Previous HEAD position was 36ad12d... update lib1-features by developer B
Switched to branch 'master'
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
? henryyan@hy-hp ~/submd/ws/project1/libs/lib1 git:(master) git pull
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/henryyan/submd/repos/lib1
36ad12d..8c666d8 master -> origin/master
Updating c22aff8..8c666d8
Fast-forward
README | 1 +
lib1-features | 1 +
2 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 README
? henryyan@hy-hp ~/submd/ws/project1/libs/lib1 git:(master)

果不其然,我們看到了剛剛在project2中修改的內(nèi)容,同步到了project1中,當(dāng)然現(xiàn)在更新了project1的lib1,commit id也會隨之變動:

? henryyan@hy-hp ~/submd/ws/project1/libs/lib1 git:(master) cd ../../
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: libs/lib1 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git diff
diff --git a/libs/lib1 b/libs/lib1
index 36ad12d..8c666d8 160000
--- a/libs/lib1
+++ b/libs/lib1
@@ -1 +1 @@
-Subproject commit 36ad12d40d8a41a4a88a64add27bd57cf56c9de2
+Subproject commit 8c666d86531513dd1aebdf235f142adbac72c035
</file></file>

現(xiàn)在最新的commit id和project2目前的狀態(tài)一致,說明真的同步了;好的,現(xiàn)在可以使用相同的辦法更新lib2了:

? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? cd libs/lib2
? henryyan@hy-hp ~/submd/ws/project1/libs/lib2 git:(master) git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/henryyan/submd/repos/lib2
7290dce..e372b21 master -> origin/master
Updating 7290dce..e372b21
Fast-forward
lib2-features | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)

2.7.2 更新project1的submodule引用

在2.7.1中我們更新了project1的lib1和lib2的最新版本,現(xiàn)在要把最新的commit id保存到project1中以保持最新的引用。

? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: libs/lib1 (new commits)
# modified: libs/lib2 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
? henryyan@hy-hp ~/submd/ws/project1 git:(master) ? git commit -a -m "update lib1 and lib2 commit id to new version"
[master 8fcca50] update lib1 and lib2 commit id to new version
2 files changed, 2 insertions(+), 2 deletions(-)
? henryyan@hy-hp ~/submd/ws/project1 git:(master) git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 397 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /home/henryyan/submd/ws/../repos/project1.git
c96838a..8fcca50 master -> master
</file></file>

2.8 更新project1-b項(xiàng)目的子模塊(使用腳本)

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/henryyan/submd/ws/../repos/project1
c96838a..8fcca50 master -> origin/master
Updating c96838a..8fcca50
Fast-forward
libs/lib1 | 2 +-
libs/lib2 | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: libs/lib1 (new commits)
# modified: libs/lib2 (new commits)
#
no changes added to commit (use "git add" and/or "git commit -a")
</file></file>

Git提示lib1和lib2有更新內(nèi)容,這個判斷的依據(jù)來源于submodule commit id的引用。

現(xiàn)在怎么更新呢?難道還是像project1中那樣進(jìn)入子模塊的目錄然后git checkout master,接著git pull?

而且現(xiàn)在僅僅才兩個子模塊、兩個項(xiàng)目,如果在真實(shí)的項(xiàng)目中使用的話可能幾個到幾十個不等,再加上N個submodule,自己算一下要怎么更新多少個submodules?

例如筆者現(xiàn)在做的一個項(xiàng)目有5個web模塊,每個web模塊引用公共的css、js、images、jsp資源,這樣就有20個submodule需要更新!!!

工欲善其事,必先利其器,寫一個腳本代替手動任務(wù)。

2.8.1 牛刀小試

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? grep path .gitmodules | awk '{ print $3 }' > /tmp/study-git-submodule-dirs
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? cat /tmp/study-git-submodule-dirs
libs/lib1
libs/lib2

我們通過分析.gitmodules文件得出子模塊的路徑,然后就可以根據(jù)這些路徑進(jìn)行更新。

2.8.2 上路

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? mkdir bin
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? vi bin/update-submodules.sh

把下面的腳本復(fù)制到bin/update-submodules.sh中:

#!/bin/bash
grep path .gitmodules | awk '{ print $3 }' > /tmp/study-git-submodule-dirs

# read
while read LINE
do
echo $LINE
(cd ./$LINE && git checkout master && git pull)
done < /tmp/study-git-submodule-dirs

稍微解釋一下上面的腳本執(zhí)行過程:

首先把子模塊的路徑寫入到文件/tmp/study-git-submodule-dirs中;

然后讀取文件中的子模塊路徑,依次切換到master分支(修改都是在master分支上進(jìn)行的),最后更新最近改動。

2.8.3 2013-01-19更新

網(wǎng)友@紫煌給出了更好的辦法,一個命令就可以代替上面的bin/update-submodules.sh的功能:

git submodule foreach git pull

此命令也腳本一樣,循環(huán)進(jìn)入(enter)每個子模塊的目錄,然后執(zhí)行foreach后面的命令。

該后面的命令可以任意的,例如 git submodule foreach ls -l 可以列出每個子模塊的文件列表

2.8.3 體驗(yàn)工具帶來的便利

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? git submodule
+36ad12d40d8a41a4a88a64add27bd57cf56c9de2 libs/lib1 (heads/master)
+7290dce0062bd77df1d83b27dd3fa3f25a836b54 libs/lib2 (heads/master)

# 添加執(zhí)行權(quán)限
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? chmod +x ./bin/update-submodules.sh
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? ./bin/update-submodules.sh
libs/lib1
Already on 'master'
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/henryyan/submd/repos/lib1
36ad12d..8c666d8 master -> origin/master
Updating 36ad12d..8c666d8
Fast-forward
README | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README
libs/lib2
Switched to branch 'master'
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/henryyan/submd/repos/lib2
7290dce..e372b21 master -> origin/master
Updating 7290dce..e372b21
Fast-forward
lib2-features | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? git submodule
8c666d86531513dd1aebdf235f142adbac72c035 libs/lib1 (heads/master)
e372b21dffa611802c282278ec916b5418acebc2 libs/lib2 (heads/master)

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# bin/
nothing added to commit but untracked files present (use "git add" to track)
</file>

更新之后的兩個變化:

git submodule的結(jié)果和project2的submodule commit id保持一致;

project1-b不再提示new commits了。

現(xiàn)在可以把工具添加到倉庫了,當(dāng)然你可以很驕傲的分享給其他項(xiàng)目組的同事。

? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? git add bin/update-submodules.sh
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) ? git commit -m "添加自動更新submodule的快捷腳本^_^"
[master 756e788] 添加自動更新submodule的快捷腳本^_^
1 files changed, 9 insertions(+), 0 deletions(-)
create mode 100755 bin/update-submodules.sh
? henryyan@hy-hp ~/submd/ws/project1-b git:(master) git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 625 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
To /home/henryyan/submd/ws/../repos/project1.git
8fcca50..756e788 master -> master

2.9 新進(jìn)員工加入團(tuán)隊(duì),一次性Clone項(xiàng)目和Submodules

一般人使用的時候都是使用如下命令:

git clone /path/to/repos/foo.git
git submodule init
git submodule update

新員工不耐煩了,嘴上不說但是心里想:怎么那么麻煩?

上面的命令簡直弱暴了,直接一行命令搞定:

git clone --recursive /path/to/repos/foo.git

–recursive參數(shù)的含義:可以在clone項(xiàng)目時同時clone關(guān)聯(lián)的submodules。

git help 對其解釋:

--recursive, --recurse-submodules

After the clone is created, initialize all submodules within, using their default settings. This is equivalent to running git

submodule update --init --recursive immediately after the clone is finished. This option is ignored if the cloned repository

does not have a worktree/checkout (i.e. if any of --no-checkout/-n, --bare, or --mirror is given)

2.9.1 使用一鍵方式克隆project2

? henryyan@hy-hp ~/submd/ws git clone --recursive ../repos/project2.git project2-auto-clone-submodules
Cloning into project2-auto-clone-submodules...
done.
Submodule 'libs/lib1' (/home/henryyan/submd/repos/lib1.git) registered for path 'libs/lib1'
Submodule 'libs/lib2' (/home/henryyan/submd/repos/lib2.git) registered for path 'libs/lib2'
Cloning into libs/lib1...
done.
Submodule path 'libs/lib1': checked out '8c666d86531513dd1aebdf235f142adbac72c035'
Cloning into libs/lib2...
done.
Submodule path 'libs/lib2': checked out 'e372b21dffa611802c282278ec916b5418acebc2'
舒服……

3.移除Submodule

牢騷:搞不明白為什么git不設(shè)計(jì)一個類似:git submodule remove的命令呢?

我們從project1.git克隆一個項(xiàng)目用來練習(xí)移除submodule:

? henryyan@hy-hp ~/submd/ws git clone --recursive ../repos/project1.git project1-remove-submodules
Cloning into project1-remove-submodules...
done.
Submodule 'libs/lib1' (/home/henryyan/submd/repos/lib1.git) registered for path 'libs/lib1'
Submodule 'libs/lib2' (/home/henryyan/submd/repos/lib2.git) registered for path 'libs/lib2'
Cloning into libs/lib1...
done.
Submodule path 'libs/lib1': checked out '8c666d86531513dd1aebdf235f142adbac72c035'
Cloning into libs/lib2...
done.
Submodule path 'libs/lib2': checked out 'e372b21dffa611802c282278ec916b5418acebc2'
? henryyan@hy-hp ~/submd/ws cd !$
? henryyan@hy-hp ~/submd/ws cd project1-remove-submodules

3.1 Step by

1、刪除git cache和物理文件夾

? henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) git rm -r --cached libs/
rm 'libs/lib1'
rm 'libs/lib2'
? henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) ? rm -rf libs

2、刪除.gitmodules的內(nèi)容(或者整個文件) 因?yàn)楸纠挥袃蓚€子模塊,直接刪除文件:

? henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) ? rm .gitmodules

如果僅僅刪除某一個submodule那么打開.gitmodules文件編輯,刪除對應(yīng)submodule配置即可。

3、刪除.git/config的submodule配置 源文件:

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = /home/henryyan/submd/ws/../repos/project1.git
[branch "master"]
remote = origin
merge = refs/heads/master
[submodule "libs/lib1"]
url = /home/henryyan/submd/repos/lib1.git
[submodule "libs/lib2"]
url = /home/henryyan/submd/repos/lib2.git

刪除后:

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = /home/henryyan/submd/ws/../repos/project1.git
[branch "master"]
remote = origin
merge = refs/heads/master

4、提交更改

? henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) ? git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: libs/lib1
# deleted: libs/lib2
#
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: .gitmodules
#
? henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) ? git add .gitmodules
? henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) ? git commit -m "刪除子模塊lib1和lib2"
[master 5e2ee71] 刪除子模塊lib1和lib2
3 files changed, 0 insertions(+), 8 deletions(-)
delete mode 100644 .gitmodules
delete mode 160000 libs/lib1
delete mode 160000 libs/lib2
? henryyan@hy-hp ~/submd/ws/project1-remove-submodules git:(master) git push
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 302 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
To /home/henryyan/submd/ws/../repos/project1.git
756e788..5e2ee71 master -> master
</file></file></file>

4.結(jié)束語

對于Git Submodule來說在剛剛接觸的時候多少會有點(diǎn)凌亂的趕緊,尤其是沒有接觸過svn:externals。

本文從開始創(chuàng)建項(xiàng)目到使用git submodule的每個步驟以及細(xì)節(jié)、原理逐一講解,希望到此讀者能駕馭它。

學(xué)會了Git submdoule你的項(xiàng)目中再也不會出現(xiàn)大量重復(fù)的資源文件、公共類庫,更不會出現(xiàn)多個版本,甚至一個客戶的多個項(xiàng)目風(fēng)格存在各種差異。

來源:http://www.kafeitu.me/git/2012/03/27/git-submodule.html

拉取所有子模塊

git submodule foreach git pull
git submodule foreach --recursive git submodule init
git submodule foreach --recursive git submodule update

到此這篇關(guān)于Git Submodule使用完整教程(小結(jié))的文章就介紹到這了,更多相關(guān)Git Submodule使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

文章轉(zhuǎn)自腳本之家,原文鏈接:https://www.jb51.net/article/193194.htm

申請創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)標(biāo)簽
git使用的基本流程

相關(guān)文章

熱門排行

信息推薦