4. Git 常见问题
4.1 常用命令
基本操作
# 添加改动
git add .
# 添加commit
git commit -m "xxxxx"
# 提交代码
git push
# 拉取代码
git pull
切换分支
//列出所有分支
git branch -a
//切换到xxx分支
git checkout xxx
删除分支
//删除本地分支branch_name
git branch -d branch_name
克隆指定分支
//xxx 你要克隆的分支 url 仓库地址
git clone --branch xxx url
恢复到上一次提交
//查看git的提交版本和id 执行结果会显示历史记录
git log
//恢复到指定版本 pre_id指定你要恢复的id
git reset --hard pre_id
//如 git reset --hard 4a28f1cc1e27bb1870d8966a09ca87eb7ef21095
强制提交到仓库
//强制提交到master
git push -f origin master
//强制提交到分支 branch_name 为远程分支
git push origin branch_name --force
创建分支并提交到新分支
//branch_name 为远程分支
git checkout -b branch_name
//查看分支
git branch
//提交到新分支
git push origin branch_name
//必要时为此分支创建跟踪信息
git branch --set-upstream-to=origin/branch_name
强制拉取仓库
git fetch --all
git reset --hard master
git pull
4.2 常见问题
被仓库拒绝提交
remote: You do not have permission push to this repository
remote: Permission to xxxxx.git denied to xxxxx.
git config --global user.name userA
git config --global user.email userA@Email.com
git config credential.username UserB
git push origin master
git:'credential-osxkeychain' 不是一个 git 命令。参见 'git --help'。
git:'credential-wincred' 不是一个 git 命令。参见 'git --help'。
git config --unset-all credential.helper
git config --global credential.helper store
remote: Invalid username or password.
fatal: Authentication failed for 'xxxxx'
提交时账户密码输出错误
Incorrect username or password (access token)
git config --global user.name userA
git config --global user.email userA@Email.com
git config credential.username UserB
git push origin master
push 被拒绝
error: failed to push some refs to 'https://gitee.com/taotaodiy/taotaodiy.git'
remote: error: GE007: Your push would publish a private email address.
邮箱管理,取消勾选,不公开我的邮箱地址
提交时候很多警告
The file will have its original line endings in your working directory.
git config --global core.autocrlf false
修改远程仓库
方法一:使用命令先删再增
第一步:先删除你的远程仓库
git remote rm origin
第二步:再添加远程仓库
git remote add origin [url]
注意:如果有其他分支修改后需要重新拉去一下远程分支
git fetch
方法二:使用命令修改
git remote origin set-url [url]
方法三:直接修改你本地的.git文件
进入.git文件夹,编辑.git文件中的config文件,修改config文件中的url路径为新的远程仓库地址路径。
gnutls_handshake() failed error in the pull function
\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\--
删除大文件记录
git verify-pack -v .git/objects/pack/pack-*.idx | sort -k 3 -g | tail -3
# 列出体积最大的 top10
git verify-pack -v .git/objects/pack/pack-*.idx | sort -k 3 -g | tail -10
3c9d13145345a487c945e831cef7236c627f747c blob 13868280 6086361 52372939
ef2a59a81ca04939c16fbf0d04e532bdc1da4f98 blob 14820004 14632166 101243560
ef2a59a81ca04939c16fbf0d04e532bdc1da4f98 blob 14820004 14632166 73991219
8b4993be48aae19632e426c9477f1f40fc0f39d6 blob 23613536 19113810 5406242
d64b12ed18341985f1d67d6686fb40f8463905ee blob 24659968 10169612 8859013
5473d05d20afeb10f1847f45cf71b74ee799d1cb blob 44291216 10731945 35323744
5473d05d20afeb10f1847f45cf71b74ee799d1cb blob 44291216 10731945 59301376
24aadc6dc91b66590ee80f0d7d8bcf16c60d3171 blob 59711754 17737912 46055689
24aadc6dc91b66590ee80f0d7d8bcf16c60d3171 blob 59711754 17737912 71573635
3508fc89d90b57ee9266c33279a60cbf420bda44 blob 173640278 41395761 10977144
# 查找每一个 commit 对应的文件
git rev-list --objects --all | grep <commit-id>
git rev-list --objects --all 3508fc89d90b57ee9266c33279a60cbf420bda44
3508fc89d90b57ee9266c33279a60cbf420bda44 ffmpeg/linuxlib/libavcodec.a
git rev-list --objects --all 24aadc6dc91b66590ee80f0d7d8bcf16c60d3171
24aadc6dc91b66590ee80f0d7d8bcf16c60d3171 ffmpeg/linuxlib/libavfilter.a
git rev-list --objects --all 5473d05d20afeb10f1847f45cf71b74ee799d1cb
5473d05d20afeb10f1847f45cf71b74ee799d1cb ffmpeg/linuxlib/libavformat.a
# 清除大文件
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch 'ffmpeg/linuxlib/libavcodec.a'" --prune-empty --tag-name-filter cat -- --all
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch 'ffmpeg/linuxlib/libavfilter.a'" --prune-empty --tag-name-filter cat -- --all
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch 'ffmpeg/linuxlib/libavformat.a'" --prune-empty --tag-name-filter cat -- --all
# 清空 git 历史
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
# 如果这一步有权限不足的问题,需要手动到github上将master分支的保护去掉
git push origin master --force
# 清理远程仓库
git remote prune origin
克隆仓库密码输入错误
再次克隆,不再提示输入账号密码,直接拒绝
window window凭据 然后删除该仓库的记录,再次克隆
4.3 Git 其他
Git 补丁
git 生成patch和打patch
管理多个 Git 工程
安卓 yocto
Git服务器搭建
自动构建
持续集成(Continuous integration,简称CI)指的是,频繁地(一天多次)将代码集成到主干。
什么是 GitLab CI ? GitLab CI 是 GitLab Continuous Integration (Gitlab 持续集成)的简称。
从 GitLab 的 8.0 版本开始,GitLab 就全面集成了Gitlab-CI,并且对所有项目默认开启。 要在项目仓库的根目录添加.gitlab-ci.yml 文件,并且配置了 Runner(运行器),那么每一次合并请求(MR)或者 push 都会触发 CI pipeline。