> For the complete documentation index, see [llms.txt](https://1425816423.gitbook.io/my-knowledge-base/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://1425816423.gitbook.io/my-knowledge-base/za-xiang/qi-ta/git.md).

# Git

## 前言

Git 是一个开源的分布式版本控制系统，它在我们日常工作中扮演非常重要的角色，不管是在工作时的团队合作开发项目，还是在平时自己开发项目，都离不开它。Git可以很方便地进行项目版本管理，以便我们可以知道代码哪里变动了，为什么变动了，甚至在一些时候可以回退到以前的代码，相当于“后悔药”。正因为Git对我们的项目开发如此重要，因此我们需要好好地学习一下Git的常用指令。

## 重要概念

### 工作区/工作树(Working Tree)

当前工作目录下的区域，此区域分为被追踪的文件和未被追踪的文件。

### 暂存区(Index/Stage)

暂存区域，此时还没完全提交成功，只是暂存起来，仍然可以将文件恢复。

### 仓库/存储库(respository)

commit最终存放的地方，此时commit已经保存成功。

## 常用指令

### 提交代码

```bash
// 追踪代码
git add .
// 提交commit
git commit
// 提交到远程仓库
git push
```

### 远程仓库

```bash
// 查看所有远程仓库
git remote
// 添加新的仓库
git remote add <name> <url>
// 获取仓库地址
git remote get-url <name>
// 修改仓库地址
git  remote set-url <name> <newUrl>
// 拉取
git pull
// 推送
git push
```

### 查看状态

```bash
git status
```

### 分支

#### 分支管理

```bash
// 查看所有分支 / 查看当前分支
git branch
// 切换分支
git checkout <branch>
// 新增分支
git branch <branch>
// 新增分支并切换到该分支
git checkout -b <branch>
// 删除分支
git branch -d <branch>
```

#### 合并分支

```bash
git merge <branch>
```

### commit管理

#### 查看commit日志

```bash
git log
```

#### 将当前代码提交到上次commit/修改上次commit信息

有时候我们提交完了才发现漏掉了几个文件没有添加，或者提交信息写错了。 此时，可以运行带有 `--amend`选项的提交命令尝试重新提交。

```bash
git add .
git commit --amend
```

#### 变基

```bash
git rebase -i <commitId>
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
```

### 比较差异

```bash
// 工作环境与你的暂存区的差异
git diff
// 暂存区域与你最后提交之间的差异
git diff --staged
// 两个分支之间的差异
git diff branchA branchB
// 启动第三方工具
git difftool
```

### 回滚代码

```bash
// 历史commit、暂存区都会被删除
git reset --hard <commit>
// 历史commit被删除，但是变动的文件会被存放进暂存区中
git reset --soft <commit>
// 历史commit被删除，但是变动的文件会被存放进工作区中
git reset --mixed <commit>
```

### 恢复工作树

```bash
git restore <file>
```

前提是代码还没被提交到暂存区，否则需要先将暂存区的文件移到工作树中：

```bash
git restore --staged <file>
// or 
git reset HEAD <file>
```

### 追踪

#### 追踪文件

add这个命令有些特殊，既可以作为追踪文件的命令，也可以作为暂存代码的命令。

```bash
git add <file>
```

#### 取消追踪

```bash
git rm --cache <file> 　　//不删除本地文件，只取消追踪
git rm -f <file> 　　//删除本地文件
git rm -r <目录>  // 删除目录
```

### 暂存

#### 将变动添加到暂存区

```bash
git add .
```

#### 取消暂存

```bash
git restore --staged .
```

也可以是

```bash
git reset HEAD .
```

### 贮藏

有时候我们当前分支还未开发完，但是需要切换到其他分支去紧急维修bug，这个时候可以调用stash将当前变动贮藏，等后面再回过头来继续开发。

```bash
// 将当前工作区追踪文件和暂存区文件贮藏
git stash / git stash push
// 工作区未被追踪的文件也将被贮藏
git stash -u 
// 查看贮藏的内容
git stash show <name>
// 查看所有贮藏
git stash list
// 移除某项贮藏
git stash drop <name>
// 去除之前的贮藏
git stash apply / git stash pop
```

### 删除工作区未被追踪的文件

```bash
git clean -f <file>  // 删除文件，不会包含目录
git clean -f -d <path> // 删除包含目录
git clean -n -d <path> // 查看删除时会做的事，如果要预测删除所来的影响可以先执行这个命令
```

### 查看commit改动

```bash
// 查看commit改动了什么
git show <commit>
```

## 配置

### 将vscode作为默认编辑器

```bash
git config --global core.editor "code --wait"
```

### 打开.gitconfig配置文件

```bash
git config --global -e
```

### git别名

```bash
$ git config --global alias.co checkout
$ git config --global alias.br branch
// 之后
git co 等效于 git checkout
git br 等效于 git branch
```
