Translated from Mandarin here
- Create Working Environment (
git init
) - Directly Manipulate Objects (
git commit
) - Directly Manipulate References (
git branch
) - Directly Manipulate the Index (
git add / restore
) - Directly Manipulate HEAD (
git switch
) - Directly Manipulate Remotes (
git pull / push
) - Directly Manipulate Merges (
git diff / merge
) - Directly Manipulate Commits (
git rebase
) - Search and View History (
git log / blame / grep
) - The Evil Submodule (
git submodule
) - Batch Processing and Automation
- Configuration and Aliases (
git config
) - Single Repo Multi-Branch Workflow
- GPG Signatures
- Data Import and Export (
git archive
) - Data Recovery and Emergency
This tutorial also provides a cheatsheet for review and self-testing.
If you are not familiar with any of the commands in the cheatsheet, you may want to start with some basic tutorials: Beginner, Intermediate, Advanced. The advanced tutorial can be studied alongside this one.
If you have already mastered all the commands in the cheatsheet, this tutorial may be too basic for you. Consider moving on to the Git Reference or Git source code.
After completing this tutorial, you should have mastered 1% of all Git usage.
Note: Detailed explanations of git reset
/git checkout
are in Chapter 4. It is highly recommended to use the more powerful and intuitive git restore
and git switch
.
To better understand Git at its core, this tutorial introduces multiple ways to perform the same operation. The table below describes which method is best suited for different scenarios.
Level | Meaning | Usage Scenario |
---|---|---|
Lv0 | Pure manual implementation, no Git CLI | Learning Git internals |
Lv1 | Use low-level Git CLI with manual steps | For extremely special Git operations |
Lv2 | Use low-level Git CLI | For unconventional Git operations |
Lv3 | Use standard Git CLI | Daily use |
Lv4 | Git Alias | Extending Git for daily use |
Lv5 | Scripting with Git CLI | For unconventional extensions, occasional use |
- cwd defaults to
.
, meaning tocd
there before running subsequent commands - work-tree defaults to
GIT_WORK_TREE
or.
, but not all commands involve the worktree - git-dir defaults to
GIT_DIR
or./.git
:- If
./.git
is a directory, it is used as the repo - If
./.git
is a file, its content (usually an absolute path) is used as the repo
- If
git [-C <cwd>] [--git-dir=<repo>] [--work-tree=<worktree>] <command> [args]
Most commands follow this format for arguments:
- object: an object expression
- Usually composed of references, object SHA1, ^, ~, :, etc.
- See
git rev-parse
(Lv2) for the full list
- path: a file path
--
can be omitted if there is no ambiguity
Note: The presence or absence of the <path>
parameter can fundamentally change the command's meaning
git <command> [options] [<object>]
git <command> [options] [<object>] -- [<path>]
This tutorial covers all common Git commands and more than half of the uncommon ones. See the Roadmap.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.