安装git
请参阅官方文档
初次使用git
全局配置
关闭SSL证书认证
1 git config --global http.sslVerify false
启用保存账号密码,只需要输入一次密码。
1 git config --global credential.helper store
设置默认分支为main
1 git config --global init.defaultBranch main
克隆远程项目
克隆仓库(提交本地仓库,跳过此步)
1 git clone "https://... or git@..."
克隆时提示登录
Username for ‘https://github.com ‘: Password for ‘https://asucanyh-cn@github.com ‘:
1 U2FsdGVkX1+J8h4yqaXNbak9+HeY/8BE4tI6pdBpuZgOW7u2kstotAR4q9kq8PVv4vPcSLr5xl/1qqMj9GyipA
本地初始化仓库
在本地的项目中,创建一个README文档,然后对将项目初始化(创建本地仓库),再创建一个提交并提供描述信息,选择要提交到的分支(如果没有需要新建),选择需要提交到的远程仓库地址,
1 2 echo "# 这是README文档的标题" >> README.mdgit init
1 2 3 git branch -m master main
将本地仓库上传至Github 1 2 3 4 git add . git commit -m "这是针对本次提交的描述信息" git remote add origin "repo-url" git branch -M main
使用情景 查看暂存文件
查看add了哪些文件
删除暂存区中的文件
修改描述信息
如果不小心写错了描述信息,如何修改?如果已经推送到了远程仓库该怎么办?
情景一
本地已经提交描述信息。
情景二
本地已经推送给远程仓库
1 2 3 4 5 6 7 git rebase -i HEAD~1 git commit --amend git rebase --continue git push --force
强制覆盖本地仓库 注意
强制覆盖的操作很危险,请确保自己的修改是否不被需要!
1 2 3 # 设置上传到远程仓库的分支 git branch --set-upstream-to=origin/main git fetch --all && git reset --hard origin/main && git pull
注意事项 在本地仓库硬回退git reset --hard HEAD HASH
后,通过git push origin HEAD --force
可以同步远程仓库回退,但不能让其它的本地仓库
和远程仓库同步,使用git pull
命令会一直提示最新而文件内容未发生改变。
报错方案
为什么GitHub默认分支为main呢?
error: failed to push some refs to ‘git@github.com :Asucanyh-cn/blog-source.git’
SSL证书报错
git SSL certificate problem: unable to get local issuer certificate
1 git config --global http.sslVerify false
git提交或克隆报错
fatal: unable to access ‘https://github.com/Asucanyh-cn/blog-source.git/ ‘: Failed connect to github.com:443; Connection timed out
1 2 3 4 5 6 7 8 9 10 11 12 # 用加速器 # 修改hosts文件 140.82.114.3 github.com 199.232.69.194 github.global.ssl.fastly.net 185.199.108.153 assets-cdn.github.com 185.199.109.153 assets-cdn.github.com 185.199.110.153 assets-cdn.github.com 185.199.111.153 assets-cdn.github.com # 尝试取消全局代理,有时候还是无效 git config --global --unset http.proxy git config --global --unset https.proxy
解决冲突 : AutoMatic merge failed;fix conflicts and then commit the result
有些目录存在但是看不见内容:fatal: Path ‘XXX‘ is in submodule ‘XXX‘错误(path is in submodule)
原因是该目录为其他git项目,可以使用git rm rf --cached‘/path’
1 2 3 4 5 //解决方案 git rm rf --cached‘/path’ git add /path/* git commit -m '' git push
Git冲突:Please commit your changes or stash them before you merge
1 2 3 4 5 git stash git pull git stash pop git stash list git stash clear
Pull is not possible because you have unmerged files.Please, fix them up in the work tree, and then use ‘git add/rm ‘ as appropriate to mark resolution, or use ‘git commit -a’.
1 2 3 4 5 6 7 8 9 10 git reset --hard FETCH_HEAD git pull git add . git commit -m " " git pull git add . git commit -m " " git push
detected dubious ownership in repository at ‘/…/‘
1 git config --global --add safe.directory "*";
warning: push.default is unset; its implicit value is changing in Git 2.0 from ‘matching’ to ‘simple’.
1 git config --global push.default matching
error: The last gc run reported the following. Please correct the root cause and remove gc.log.Automatic cleanup will not be performed until the file is removed.warning: There are too many unreachable loose objects; run ‘git prune’ to remove them.
1 2 git fsck --lost-found #查看悬空对象 git gc --prune=now #清空
your branch is ahead of by x commits.
1 2 git push origin main #直接提交 git reset --hard HEAD~x #放弃commit
fatal: refusing to merge unrelated histories
1 git pull --allow-unrelated-histories
fatal: unable to access ‘’: Error in the HTTP2 framing layer
1 2 git config --global --unset http.proxy git config --global --unset https.proxy
git pull –tags origin main fatal: unable to access ‘’: GnuTLS recv error (-110): The TLS connection was non-properly terminated.
1 2 3 4 sudo apt-get update sudo apt-get install gnutls-bin git config --global http.sslVerify false git config --global http.postBuffer 1048576000