# Git 相关

# 创建版本库

git init
1

# 添加远程库

$ git remote add origin https://github.com/JohnNashs/learngit.git
1

# 克隆指定分支

git clone -b <name> 仓库地址
1

# Git 状态

git stauts
1

# 三种状态

  • Working Directory 工作区
  • Staging Area 暂存区
  • Repository 版本库

# 精简命令

# 提交代码

# 将代码提交到暂存区
git add .

# 将代码提交到当前分支
git commit -m "这里是注释"

# 将代码提交到线上
git push origin <name>
1
2
3
4
5
6
7
8

# 拉取代码

git pull
1

# 分支操作

# 查看分支
git branch

# 新建分支
git branch NewBranchName

# 切换分支
git chekcout NewBranchName

# 本地检出一个新的分支并推送到远程仓库

## 1.创建并切换分支(创建本地分支)
git checkout -b <name>
## 2.创建并切换分支(创建本地分支)
git push --set-upstream origin <name>

# 将远程git仓库里的指定分支拉取到本地(本地不存在的分支)
git checkout -b 本地分支名 origin/远程分支名

# 合并某支到当前分支
git checkout master #先切换到master
git merge NewBranchName #将NewBranchName合并到当前的分支master

# 删除分支
git branch -d NewBranchName
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# 查看 log

# 查看 commit log
git log

# 查看操作记录
git relog # git log -g
1
2
3
4
5

# 版本回退

# 回退若干个版本(回退一个 HEAD^, 回退两个版本是 HEAD^^, 三个是 HEAD^^^, 以此类推 )
git reset --hard HEAD^

# 按照版本号回退
git reset --hard HEAD~版本号 # 版本号不需要写全
1
2
3
4
5

# 重置本地账户密码

git config --system --unset credential.helper
1

# 保存账号密码

git config --global credential.helper store
1

# 删除本地缓存(项目提交后再建 gitignore 不生效解决方法)

git rm -r --cached .
1

# 升级 Git 后,仓库无法 push

error: RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR
1

# 解决方式

git config --global http.version HTTP/1.1
git push
git config --global http.version HTTP/2
1
2
3

# 原则

协同开发应建立自己的分支,再合并到 master 上

# 教程

Git 官网 (opens new window)

廖雪峰 (opens new window)