安装git

请参阅官方文档

初次使用git

全局配置

  1. 关闭SSL证书认证
1
git config --global http.sslVerify false
  1. 启用保存账号密码,只需要输入一次密码。
1
git config --global credential.helper store
  1. 设置默认分支为main
1
git config --global init.defaultBranch main

克隆远程项目

  1. 克隆仓库(提交本地仓库,跳过此步)
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.md
git init #初始化本地仓库,会创建一个.git文件
1
2
3
#执行过修改默认分支,可不用再使用该语句
git branch -m master main
#在git中把默认的master分支改为main,用于发布到GitHub,也可以直接在GitHub创建一个master分支,将项目提交到master分支中。

将本地仓库上传至Github

1
2
3
4
git add . #将项目代码添加到待提交区
git commit -m "这是针对本次提交的描述信息"
git remote add origin "repo-url" #设置上传的远程仓库地址
git branch -M main
1
git push -u origin main #提交上传,可以直接用git push上传到默认的分支

使用情景

查看暂存文件

查看add了哪些文件

1
git ls-files

删除暂存区中的文件

1
git rm

修改描述信息

如果不小心写错了描述信息,如何修改?如果已经推送到了远程仓库该怎么办?

情景一

本地已经提交描述信息。

1
2
git commit --amend
#输入完毕后,就可以对描述信息进行修改了

情景二

本地已经推送给远程仓库

1
2
3
4
5
6
7
git rebase -i HEAD~1 
#~1表示倒数第一条推送记录
#输入完毕后,会让你修改提交记录
#将需要修改的对应行的pick改成edit,保存退出
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: src refspec main does not match any.
    error: failed to push some refs to ‘git@github.com:Asucanyh-cn/blog-source.git’

  • ! [rejected] main -> main (fetch first)

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