Git相关指令

创建版本库

1
git init

添加远程库

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

克隆指定分支

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

Git 状态

1
git stauts

三种状态

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

精简命令

提交代码

1
2
3
4
5
6
7
8
# 将代码提交到暂存区
git add .

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

# 将代码提交到线上
git push origin <name>

拉取代码

1
git pull

分支操作

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
# 查看分支
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

查看 log

1
2
3
4
5
# 查看 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
git config --system --unset credential.helper

保存账号密码

1
git config --global credential.helper store

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

1
git rm -r --cached .

升级 Git 后,仓库无法 push

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

解决方式

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

原则

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

博客发布指令

hexo clean && hexo g && hexo d

教程

Git 官网

廖雪峰