Git Submodule使用完整教程(小结)

作者:lsgxeva 时间:2022-06-29 21:01:06 

自从看了蒋鑫的《Git权威指南》之后就开始使用Git Submodule功能,团队也都熟悉了怎么使用,多个子系统(模块)都能及时更新到最新的公共资源,把使用的过程以及经验和容易遇到的问题分享给大家。

Git Submodule功能刚刚开始学习可能觉得有点怪异,所以本教程把每一步的操作的命令和结果都用代码的形式展现给大家,以便更好的理解。

1.对于公共资源各种程序员的处理方式

每个公司的系统都会有一套统一的系统风格,或者针对某一个大客户的多个系统风格保持统一,而且如果风格改动后要同步到多个系统中;这样的需求几乎每个开发人员都遇到,下面看看各个层次的程序员怎么处理:

假如对于系统的风格需要几个目录:css、images、js。

  • 普通程序员,把最新版本的代码逐个复制到每个项目中,如果有N个项目,那就是要复制N x 3次;如果漏掉了某个文件夹没有复制…@(&#@#。

  • 文艺程序员,使用Git Submodule功能,执行:git submodule update,然后冲一杯咖啡悠哉的享受着。

引用一段《Git权威指南》的话: 项目的版本库在某些情况虾需要引用其他版本库中的文件,例如公司积累了一套常用的函数库,被多个项目调用,显然这个函数库的代码不能直接放到某个项目的代码中,而是要独立为一个代码库,那么其他项目要调用公共函数库该如何处理呢?分别把公共函数库的文件拷贝到各自的项目中会造成冗余,丢弃了公共函数库的维护历史,这显然不是好的方法。

2.开始学习Git Submodule

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

既然文艺程序员那么轻松就搞定了,那我们就把过程一一道来。

说明:本例采用两个项目以及两个公共类库演示对submodule的操作。因为在一写资料或者书上的例子都是一个项目对应1~N个lib,但是实际应用往往并不是这么简单。

2.1 创建Git Submodule测试项目

2.1.1 准备环境


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

创建需要的本地仓库:


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

初始化工作区:


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

2.1.2 初始化项目

初始化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 为主项目添加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
#

# 查看一下公共类库的内容

➜ 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>

来源:https://www.cnblogs.com/lsgxeva/p/8540758.html

标签:Git,Submodule
0
投稿

猜你喜欢

  • 基于Python对象引用、可变性和垃圾回收详解

    2022-03-12 17:11:49
  • asp中access升级到sql server后要做的工作

    2007-08-11 13:35:00
  • python中实现指定时间调用函数示例代码

    2021-04-20 07:14:25
  • MySQL查询倒数第二条记录实现方法

    2024-01-26 07:15:50
  • PyCharm2020.1.2社区版安装,配置及使用教程详解(Windows)

    2023-08-09 20:32:30
  • Django 限制访问频率的思路详解

    2021-08-17 16:52:57
  • Python编辑器Pycharm安装配置超详细教程

    2023-03-25 14:55:15
  • 详解MySQL数据库优化的八种方式(经典必看)

    2024-01-13 11:32:20
  • MYSQL之插入极限分析

    2024-01-22 13:35:44
  • Python实现拓扑算法的示例

    2023-12-09 15:06:29
  • python的描述符(descriptor)、装饰器(property)造成的一个无限递归问题分享

    2023-04-29 08:57:32
  • Python实现批量修改图片格式和大小的方法【opencv库与PIL库】

    2021-10-08 09:04:29
  • 手把手教你安装Windows版本的Tensorflow

    2021-07-12 07:03:15
  • Go语言实现登录验证代码案例

    2024-05-08 10:24:14
  • Python使用googletrans报错的解决方法

    2021-09-21 01:38:00
  • SQLServer 连接 EXCEL

    2009-07-09 19:00:00
  • python缺失值的解决方法总结

    2023-07-28 23:47:38
  • javascript实现获取浏览器版本、操作系统类型

    2024-05-13 10:36:21
  • 使用虚拟机在VirtualBox+openEuler上安装部署openGauss数据库

    2024-01-21 22:45:53
  • JavaScript html5 canvas实现图片上画超链接

    2024-04-28 10:19:19
  • asp之家 网络编程 m.aspxhome.com