00.使用环境
ubuntu-1804
10.配置GIT
1. 配置用户信息等
# cmd : 查看全部信息
git config --list
# cmd : 配置(或修改)用户名、用户邮箱(非全局)
git config user.name "xxxxx"
git config user.email "xxxx@yyy.com"
# cmd : 配置(或修改)用户名、用户邮箱(全局)
git config --global user.name "xxxxx"
git config --global user.email "xxxx@yyy.com"
2. GIT 命令自动补全
下载:
curl -o ~/.git-completion.bash https://gitee.com/mirrors/git/raw/master/contrib/completion/git-completion.bash
添加:
echo "source ~/.git-completion.bash" >> ~/.bashrc
更新:
source ~/.bashrc
20.命令
1. 本地仓库管理
1.设置缓冲区大小
# cmd : 在git仓库下设置
git config http.postBuffer 524288000
2.不检索未跟踪的文件
# cmd : 不检索未跟踪的文件,缩短时间
git status . -uno // 不检索 未跟踪 的文件
git status . -u // 检索 未跟踪的文件
git status . // 默认是检索的,作用同上一条
3. 修改远程仓库地址
# 产看远程仓库地址、选中的名称
# git remote -v
# git remote
# 示例:修改origin的地址,
git remote set-url origin http:xxxxx.git
4.遍历当前文件夹下指定文件
git add **/*.c 之类的可能存在各种环境问题,考虑更换为如下几种命令:
find . -name "*.c" -o -name "*.h" | xargs git add
find . -type f -name "*.S" | xargs git add // 只包含文件,不包含文件夹
5.不显示^M字符
# 设置全局仓库
git config --global core.whitespace cr-at-eol
# 或者当前仓库
git config core.whitespace cr-at-eol
6.压缩提交 squash commits
功能:将多次提交合并为一次提交
命令:it rebase -i HASH值,修改pick为squash,保存后修改commitmessge即可
备注:需要强制提交都远程分支,多人合作多注意
测试用例:
4444 commit#4
3333 commit#3
2222 commit#2
1111 commit#1
测试用例:
4444 commit#4
3333 commit#3
2222 commit#2
1111 commit#1
命令:
git rebase -i 1111 // 从这之后的提交合并,不包括这一次
进入交互式界面:
// 将如下界面
pick commit#4
pick commit#3
pick commit#2
pick commit#1
// 修改为: (squash可以缩写为s)
pick commit#4
s commit#3
s commit#2
s commit#1
// 保存后,添加这一次合并后想要使用的message信息
根据提示:
# This is a combination of 4 commits.
A new message : rebase test
// 保存退出即可
7.遍历并添加当前文件夹及其所有子文件夹下指定后缀的文件
git add `find . -type f -name '*.c'`
2. 远程仓库管理
1.推送本地tag到远端服务器
git 需要显式推送tag
# cmd : 推送本地单个tag
git push [remote_name] [tagname]
git push origin v0.1.0
# cmd : 推送本地所有tag
git push [remote_name] --tags
git push origin --tags
2.删除远端tag
先删除本地tag(r0.0.0),再推送到远端(origin)
git tag -d r0.0.0
# git push <REMOTE_NAME> :/refs/tags/<TAG_NAME>
git push origin :refs/tags/r0.0.0
3.删除远端分支
直接推送命令即可
git push origin -d REMOTE_BRANCH_NAME
3. 子仓库管理
- 假设主仓库为main.git
- 子仓库为toolchain.git 、docs.git,使用的分支全为test
1. 添加子仓库submodule
格式参考: git submodule add -b 分支名 --name 子仓库名 子仓库URL 本地文件夹名
git clone main.git
git submodule add -b test --name toolchains https://xxxxx/toolchains.git toolchains
git submodule add -b test --name docs https://xxxx/docs.git docs
// 得到如下新文件
new file: .gitmodules
new file: toolchains
new file: docs
// .gitmodules存放了路径,.git/config,.git/submodules/xxx 均有记录,删除时需要都做修改、删除
// 提交修改
git commit -m "xxxxx"
git push xxxxxx // 可以看到main.git仓库只有子仓库@当前提交id
2. 下载主仓库后,管理子仓库 clone,submodule
git clone main.git // 也可以下载主仓库时直接下载子仓库
git submodule update --init // 初始化子仓库配置
git submodule update --remote // 更新全部子仓库到最新提交
# git submodule update --remote docs // 可指定更新某一个子仓库
3. 删除子仓库
xxx: 谨慎操作
git submodule deinit docs -f
vi .gitmodules // 删除对应选项
vi .git/config // 删除对应选项
rm -rf .git/modules/docs/ // 删除对应文件夹
30.问题
1. fatal: refusing to merge unrelated histories 合并不相关的库
xxx : 添加选项 --allow-unrelated-histories
如果文件有冲突的话,不是很建议做这样的操作,如果是完全不相干的两个库,可以尝试一下
git merge xxxx --allow-unrelated-histories
2. 子仓库管理问题
1. fatal: Needed a single revision
更新子仓库时报错:fatal: Needed a single revision
删除子仓库,然后靠git管理来恢复,然后再update
// 问题现象:init时中断下载,随后便出问题了
git submodule update --remote 1.toolchains/
# Username for 'https://xxx.com': xx
# Password for 'https://xxx@xxx.com':
# fatal: Needed a single revision
# Unable to find current origin/xxxxx revision in submodule path # '1.toolchains'
// 移除子仓库并利用git恢复
rm -rf 1.toolchains/
git checkout 1.toolchains
// 更新子仓库
git submodule update --remote 1.toolchains