User Tools

Site Tools


library:git:generalcmd

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
library:git:generalcmd [2022/05/02 00:32]
127.0.0.1 external edit
library:git:generalcmd [2023/02/16 17:13] (current)
lhaosen [1.7 向下同步子模块仓库]
Line 253: Line 253:
   提交检出均不转换   提交检出均不转换
   git config --global core.autocrlf false   git config --global core.autocrlf false
 +
 +===== 临时保存和恢复工作区 =====
 +  git stash
 +  
 +  git stash list
 +  git stash pop = git stash apply + git stash drop
 +
 +===== - sub module =====
 +
 +==== - 相关文件 ====
 +  * .gitsubmodules
 +记录所有子模块的文件
 +  [submodule "app/pm2"]
 +          path = app/pm2
 +          url = git@codeup.aliyun.com:xs/webapp/app.pm2.git
 +
 +  * .git/config
 +  [submodule "apps/pm2"]
 +          url = git@codeup.aliyun.com:xs/webapp/app.pm2.git
 +          active = true
 +          
 +   * .git/modules/<color #ed1c24>app/pm2</color>
 +   子模块仓库的副本,存放子模块仓库本身的.git目录文件
 +
 +  * 子模块目录的 .git
 +指向主仓库记录子模块的路径
 +  gitdir: ../../.git/modules/<color #ed1c24>app/pm2</color>
 +
 +
 +==== - 查看所有子模块 ====
 +  git submodule
 +
 +
 +==== - 克隆一个带子模块的仓库 ====
 +
 +  - 下载主项目: git clone <remote-URL>
 +  - git submodule init
 +  - git submodule update
 +  - 更新子模块: (在子模块目录) git pull origin master
 +
 +注:
 +  * git submodule init  git submodule update 两条指令可以合并为: git submodule update --init --recursive
 +  * git submodule init 的作用就是将子模块的信息写到 .git/config中
 +  * git submodule update 的作用是将子模块的空目录 clone 成对应版本的 git 仓库。
 +  * 当目录为空时,git submodule 显示的子模块不带版本信息。
 +
 +
 +
 +==== - 主仓库添加子模块(已存在的git仓库) ====
 +  git sumodule add <remote-URL> [name]
 +  git add name
 +  git commit -m "add submodule"
 +  git push origin master
 +
 +
 +
 +
 +==== - 主仓库添加子模块(将已有的目录设为子模块)====
 +
 +  * 为sub-module创建一个单独的git仓库
 +
 +  git checkout v2.0
 +  cd sub-module
 +  git init
 +  git add sub.txt
 +  git commit -m "version 1.0 of sub-module"
 +  git remote add origin https://github.com/bitmingw/sub-module.git
 +  git push -u origin master
 +
 +  * 从 main-module.git中删除 sub-module文件夹
 +
 +  cd .. # main-module/
 +  git rm -r sub-module
 +  git commit -m "remove sub-module directory"
 +
 +  * 将 sub-module.git注册为main-module.git的子模块
 +
 +  main-module/
 +  git submodule add https://github.com/bitmingw/sub-module.git
 +  git commit -m "add submodule version 1.0"
 +
 +==== - 删除子模块 ====
 +  * 逆初始化模块,其中{MOD_NAME}为模块目录,执行后可发现模块目录被清空
 +
 +   git submodule deinit <Submodule>
 +
 +  * 删除.gitmodules中记录的模块信息(--cached选项清除.git/modules中的缓存)
 +
 +  git rm --cached <Submodule>
 +
 +  * 提交更改到代码库,可观察到'.gitmodules'内容发生变更
 +
 +  git commit -am "Remove a submodule." 
 +
 +==== - 向下同步子模块仓库 ====
 +
 +  git submodule init
 +  git submodule update
 +
 +
 +  git submodule update --init --recursive
 +  
 +  
 +
 +
 +
library/git/generalcmd.1651422725.txt.gz · Last modified: 2022/05/02 00:32 by 127.0.0.1