【Git】尝试验证git子模块的行为【杂记】

为了确认子模块的行为,逐个执行命令并确认其行为。

注册子模块

> git submodule add git@github.com:kiyo27/git-submodule.git

确认更改。.gitmodules已创建。

> git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   .gitmodules
        new file:   git-submodule
> git diff HEAD git-submodule
diff --git a/git-submodule b/git-submodule
new file mode 160000
index 0000000..94a3033
--- /dev/null
+++ b/git-submodule
@@ -0,0 +1 @@
+Subproject commit 94a3033e4124495a4f6c44d373b02f14c24cf213

提交更改

> git add .gitmodules
> git add git-submodule
> git commit -m "update submodule to new version"
> git push origin main

提交指针已注册。

2021-07-18_005217.png

更新子模块

子项目更新

// git-submodule(submoduleのリポジトリ)
> touch version.txt
> echo "version 1" > version.txt
> git add version.txt
> git commit -m "add version file"
> git push origin main

在包含子模块的项目中,将子模块的更新合并进来。

> cd git-submodule
> git fetch
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 287 bytes | 16.00 KiB/s, done.
From github.com:kiyo27/git-submodule
   94a3033..388a533  main       -> origin/main
> git merge origin/main
Updating 94a3033..388a533
Fast-forward
 version.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 version.txt
> ls
README.md  version.txt

更新git子模块

我手动进行了合并,但使用git submodule update –remote可以自动完成。

> git submodule update --remote
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 279 bytes | 16.00 KiB/s, done.
From github.com:kiyo27/git-submodule
   49324e5..d1d4ad0  main       -> origin/main
Submodule path 'git-submodule': checked out 'd1d4ad0f0223cb676f6b71b6c4fafe0be83520c6'

检出分支(使HEAD进入分离状态)。

分离的 HEAD 的状态

> git status
HEAD detached at d1d4ad0
nothing to commit, working tree clean
> git branch
* (HEAD detached at d1d4ad0)
  main

在 `detached HEAD` 状态下,即使对子模块进行更改,执行 `git submodule update` 命令后,更改的内容也会消失。

修改子模块

ver 7
file change in sub project.

提交更改

> git commit -am "update"

在主项目中运行git submodule update

> git submodule update --remote
Submodule path 'git-submodule': checked out 'd1d4ad0f0223cb676f6b71b6c4fafe0be83520c6'

当检查在子模块中进行更改的文件时,发现更改的内容已经消失。

> cd git-submodule
> cat version.txt
ver 7

更新并合并远程的 git submodule。

通过执行命令 “git submodule update –remote –merge” 来合并分支。

> git submodule update --remote --merge
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 1), reused 4 (delta 1), pack-reused 0
Unpacking objects: 100% (4/4), 496 bytes | 29.00 KiB/s, done.
From github.com:kiyo27/git-submodule
   351be29..a2913d2  main       -> origin/main
Updating 351be29..a2913d2
Fast-forward
Submodule path 'git-submodule': merged in 'a2913d257fe9442460eeae93d61ca2078212ef90'

分支状态不是分离的HEAD。

> git branch
* main

在主要项目中更新子模块。

修改子模块的内容

> cd git-submodule
> echo "version @" > version.txt
> git commit -am "update"

我会尝试在主要项目中将报告推送出去,然后进行差异确认。

> git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   git-submodule (new commits)

no changes added to commit (use "git add" and/or "git commit -a")

提交更改并将其推送到远程。

> git add git-submodule
> git commit -m "update submodule"
> git push origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 268 bytes | 268.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:kiyo27/git-practice.git
   0c89257..a150914  main -> main

如果不推送子模块的更改,而是提交和推送主项目,其他人将无法获取到子模块的更改。主项目的子模块指向了已更改的指针。

通过添加 “–recurse-submodules=check” 参数,可以检查子模块的变更是否已经推送,如果子模块未推送,则不允许推送主项目。

> git push --recurse-submodules=check
The following submodule paths contain changes that can
not be found on any remote:
  git-submodule

Please try

        git push --recurse-submodules=on-demand

or cd to the path and use

        git push

to push them to a remote.

fatal: Aborting.

通过添加–recurse-submodules=on-demand来在主项目push时也将子模块一并push。

> git push --recurse-submodules=on-demand
Pushing submodule 'git-submodule'
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 291 bytes | 291.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:kiyo27/git-submodule.git
   3822fdd..351be29  main -> main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 261 bytes | 261.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:kiyo27/git-practice.git
   72055b4..0c89257  main -> main

或者,您可以在子模块上进行作业并进行推送。

> cd git-submodule
> git push origin main

克隆含有子模块的项目

克隆包含子模块的项目。但仅仅克隆项目并不能获取子模块的内容,它们是空的。

> git clone git@github.com:kiyo27/git-practice.git main-project
> ls -la
total 0
drwxrwxrwx 1 ubuntu ubuntu 4096 Jul 18 12:20 .
drwxrwxrwx 1 ubuntu ubuntu 4096 Jul 18 12:20 ..

执行 git submodule init 和 git submodule update

> git submodule init
Submodule 'git-submodule' (git@github.com:kiyo27/git-submodule.git) registered for path 'git-submodule'
> git submodule update
Cloning into 'C:/Users/narik/workspace/git/main-project/git-submodule'...
Submodule path 'git-submodule': checked out 'daf413635a93e7622c31d11d7313e042eee91ea5'

子模块的内容

> ls -la git-submodule
total 0
drwxrwxrwx 1 ubuntu ubuntu 4096 Jul 18 12:24 .
drwxrwxrwx 1 ubuntu ubuntu 4096 Jul 18 12:20 ..
-rwxrwxrwx 1 ubuntu ubuntu   38 Jul 18 12:24 .git
-rwxrwxrwx 1 ubuntu ubuntu   17 Jul 18 12:24 README.md
-rwxrwxrwx 1 ubuntu ubuntu    9 Jul 18 12:24 version.txt

该分支处于detached HEAD的状态。

> git branch
* (HEAD detached at daf4136)
  main

当您克隆包含子模块的项目时,使用–recursive选项将帮助您同时进行子模块的初始化和数据获取。

> git clone --recursive git@github.com:kiyo27/git-practice.git main-project
Cloning into 'main-project'...
remote: Enumerating objects: 72, done.
remote: Counting objects: 100% (72/72), done.
remote: Compressing objects: 100% (49/49), done.
remote: Total 72 (delta 12), reused 66 (delta 6), pack-reused 0
Receiving objects: 100% (72/72), 8.20 KiB | 839.00 KiB/s, done.
Resolving deltas: 100% (12/12), done.
Submodule 'git-submodule' (git@github.com:kiyo27/git-submodule.git) registered for path 'git-submodule'
Cloning into 'C:/Users/narik/workspace/git/main-project/git-submodule'...
remote: Enumerating objects: 42, done.
remote: Counting objects: 100% (42/42), done.
remote: Compressing objects: 100% (27/27), done.
remote: Total 42 (delta 2), reused 40 (delta 0), pack-reused 0
Receiving objects: 100% (42/42), done.
Resolving deltas: 100% (2/2), done.
Submodule path 'git-submodule': checked out 'daf413635a93e7622c31d11d7313e042eee91ea5'

子模块的内容。数据已经获取到了。

> ls -la git-submodule/
total 0
drwxrwxrwx 1 ubuntu ubuntu 4096 Jul 18 12:29 .
drwxrwxrwx 1 ubuntu ubuntu 4096 Jul 18 12:29 ..
-rwxrwxrwx 1 ubuntu ubuntu   38 Jul 18 12:29 .git
-rwxrwxrwx 1 ubuntu ubuntu   17 Jul 18 12:29 README.md
-rwxrwxrwx 1 ubuntu ubuntu    9 Jul 18 12:29 version.txt

请参考

广告
将在 10 秒后关闭
bannerAds