# Git

## 1、Git介绍

Git是一个免费的、开源的**分布式版本控制系统**

像 Git 这种分布式版本控制工具，客户端提取的不是最新版本的文件快照，而是把代码 仓库完整地镜像下来（本地库）。这样任何一处协同工作用的文件发生故障，事后都可以用 其他客户端的本地仓库进行恢复。因为每个客户端的每一次文件提取操作，实际上都是一次 对整个文件仓库的完整备份。

分布式的版本控制系统出现之后,解决了集中式版本控制系统的缺陷:

1. 服务器断网的情况下也可以进行开发（因为版本控制是在本地进行的）
2. 每个客户端保存的也都是整个完整的项目（包含历史记录，更加安全）

Git也是Linux的开发者于2005年用C语言研究开发的

## 2、Git常用命令

| 命令                                | 说明      |
| --------------------------------- | ------- |
| git config --global user.name 用户名 | 设置用户签名  |
| git config --global user.email 邮箱 | 设置用户签名  |
| git init                          | 初始化本地库  |
| git status                        | 查看本地库状态 |
| git add 文件名                       | 添加到暂存区  |
| git commit -m "日志信息" 文件名          | 提交到本地库  |
| git reflog                        | 查看历史记录  |
| git reset --hard 版本号              | 版本穿梭    |

### 2.1设置用户签名

设置名称和邮箱（这个只是保存在Windows客户端）

签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看 到，以此确认本次提交是谁做的。Git 首次安装必须设置一下用户签名，否则无法提交代码。

```
$git config --global user.email period@gmail.com
$git config --global user.name aimer
```

### 2.2 初始化本地库

会在文件下建立一个新的.git文件夹

```
$git init
```

#### 2.3 添加

```
$ git add +文件名.文件类型 ，将某个文件加到缓存区
$ git add +文件名.文件类型 ... 文件名.文件类型 ，将n个文件添加到缓存区
$ git add xx文件夹/*.html，将xx文件夹下的所有的html文件添加到缓存区。
$ git add *hhh ，将以hhh结尾的文件的所有修改添加到暂存区
$ git add Hello* ，将所有以Hello开头的文件的修改添加到暂存区
git add -u ，提交被修改(modified)和被删除(deleted)文件，不包括新文件(new)
git add .，提交新文件(new)和被修改(modified)文件，不包括被删除(deleted)文件
…
git add -A，提交所有变化。git add前几条都可以记不住，这个必须记住！！！
```

### 2.4 将暂存区的文件提交到本地库

```
git commit -m "日志信息" 文件名
```

还有一个`$ git push origin master -f` 强制推送，如果你某次推送失败，git bash报错，你懒得处理错误，你就可以用这个。但是有风险，因为报错90%是因为你本地仓库和远程仓库数据发生冲突，使用这个会直接用本地数据覆盖掉远程数据，可能损失数据哦


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notebook.aimersin.cc/readme.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
