# 基础知识

[GitBook](https://app.gitbook.com/@b1f6c1c4/s/learn-git-the-super-hard-way/)

## 目录

1. [创建工作环境](/learn-git-the-super-hard-way/chapter0.md)（`git init`）
2. [直接操纵对象](/learn-git-the-super-hard-way/chapter1.md)（`git commit`）
3. [直接操纵引用](/learn-git-the-super-hard-way/chapter2.md)（`git branch`）
4. [直接操纵索引](/learn-git-the-super-hard-way/chapter3.md)（`git add / restore`）
5. [直接操纵HEAD](/learn-git-the-super-hard-way/chapter4.md)（`git switch`）
6. [直接操纵远程](/learn-git-the-super-hard-way/chapter5.md)（`git pull / push`）
7. [直接操纵merge](/learn-git-the-super-hard-way/chapter6.md)（`git diff / merge`）
8. [直接操纵commit](/learn-git-the-super-hard-way/chapter7.md)（`git rebase`）
9. [检索与查看历史](/learn-git-the-super-hard-way/chapter8.md)（`git log / blame / grep`）
10. [邪恶的submodule](/learn-git-the-super-hard-way/chapter9.md)（`git submodule`）
11. [批处理与自动化](/learn-git-the-super-hard-way/chapter10.md)
12. [配置和alias](/learn-git-the-super-hard-way/chapter11.md)（`git config`）
13. [单repo多分支工作流](/learn-git-the-super-hard-way/chapter12.md)
14. [GPG签名](/learn-git-the-super-hard-way/chapter13.md)
15. [数据的导入和导出](/learn-git-the-super-hard-way/chapter14.md)（`git archive`）
16. [数据抢修与急救](/learn-git-the-super-hard-way/chapter15.md)

**本教程还提供了**[**cheatsheet**](/learn-git-the-super-hard-way/cheatsheet.md)，可以用来复习并检查学习效果。

如果你完全没有听说过cheatsheet里面的任何一条命令，那么你可能需要先学习一些基础教程：[入门](https://try.github.io)、[初级](https://learngitbranching.js.org)、[高级](https://git-scm.com/book/en/v2)。其中高级可以跟本教程同时学习。

如果你已经完全掌握cheatsheet里面的所有命令，那么此教程可能对你来说太过浅显，建议移步[Git Reference](https://git-scm.com/docs)、[Git源码](https://github.com/git/git)。

学习完本教程以后，你应该掌握了git的全部用法的1%。

备注：`git reset`/`git checkout`的详解在第4章。强烈推荐改用功能更强大更直观的`git restore`和`git switch`。

## 基本约定

为了更为本质地了解Git，本文会对同一种操作介绍多种不同的实现方法。 下表描述了不同使用场景下应该如何选择最适合的实现方法。

| 等级  | 含义                 | 使用场景             |
| --- | ------------------ | ---------------- |
| Lv0 | 纯粹手工实现，完全不使用Git命令行 | 学习Git内部结构时       |
| Lv1 | 使用底层Git命令行配合手工实现   | 实现极为特殊的Git操作时    |
| Lv2 | 使用底层Git命令行实现       | 实现非常规Git操作时      |
| Lv3 | 使用常规Git命令行实现       | 日常使用             |
| Lv4 | Git Alias          | 对Git进行扩展，日常使用    |
| Lv5 | 编写脚本调用Git命令行       | 对Git进行非常规扩展，偶尔使用 |

## Git命令行基础

### 全局命令行参数

* cwd默认为.，表示先cd到那里再运行后续命令
* work-tree默认为`GIT_WORK_TREE`或者`.`，但并非所有命令都涉及worktree
* git-dir默认为`GIT_DIR`或者`./.git`：
  * 若./.git是目录，则就以该目录为repo
  * 若./.git是文件，则以该文件内容（一般会是绝对路径）为repo

```bash
git [-C <cwd>] [--git-dir=<repo>] [--work-tree=<worktree>] <command> [args]
```

### 具体Git命令的参数

大部分命令的参数列表遵循以下格式：

* object是对象的表达式
  * 一般由引用、对象SHA1、^、\~、:等构成
  * 完整列表参见`git rev-parse`（Lv2）
* path是路径
* `--`在不引起歧义的情况下可以省略

注意：是否存在`<path>`参数可能对语义有本质的影响

```bash
git <command> [options] [<object>]
git <command> [options] [<object>] -- [<path>]
```

## Git命令列表

本教程涵盖所有的Git常用命令和一半以上的Git非常用命令， 参见[Roadmap](https://github.com/b1f6c1c4/learn-git-the-super-hard-way/tree/b5a0839cc97df61e641ff01819a38080d7db06ca/ROADMAP.md)

## License

\
This work is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-nc-sa/4.0/).


---

# 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://b1f6c1c4.gitbook.io/learn-git-the-super-hard-way/master.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.
