git sub module使用注意

如果clone的时候忘记改名了

要改三个地方
本来的文件夹名字
.gitmodules中的名字
.git/modules/.....名字
.git/config
# 看样子还是不够,还是改回来吧!用默认的!

https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%AD%90%E6%A8%A1%E5%9D%97

clone

cd ./extern/fast-rtps/
git submodule add git@github.com:eProsima/Fast-RTPS.git src master
git submodule update --init --recursive

修改


cd submodule_directory
git checkout v1.0
cd ..
git add submodule_directory
git commit -m "moved submodule to v1.0"
git push
git pull
git submodule update
git submodule update --remote

git pull 之后,立即执行 git status, 如果发现 submodule 有修改,立即执行 git submodule update
尽量不要使用 git commit -a, git add 命令存在的意义就是让你对加入暂存区的文件做二次确认,而 git commit -a 相当于跳过了这个确认过程。

git submodule foreach git submodule update
有些时候你需要对 submodule 做一些修改,很常见的做法就是切到 submodule 的目录,然后做修改,然后 commit 和 push。
这里的坑在于,默认 git submodule update 并不会将 submodule 切到任何 branch,所以,默认下 submodule 的 HEAD 是处于游离状态的 (‘detached HEAD’ state)。所以在修改前,记得一定要用 git checkout master 将当前的 submodule 分支切换到 master,然后才能做修改和提交。
如果你不慎忘记切换到 master 分支,又做了提交,可以用 cherry-pick 命令挽救。具体做法如下:
用 git checkout master 将 HEAD 从游离状态切换到 master 分支 , 这时候,git 会报 Warning 说有一个提交没有在 branch 上,记住这个提交的 change-id(假如 change-id 为 aaaa)
用 git cherry-pick aaaa 来将刚刚的提交作用在 master 分支上
用 git push 将更新提交到远程版本库中

原文地址:https://www.cnblogs.com/lizhensheng/p/11117239.html