📋一、整体学习路线
Git 学习的核心不是背命令,而是掌握一条主线:修改 → status → diff → add → commit → branch → merge → pull → push。这条工作流跑顺后,再学 stash、rebase、tag 等进阶。
10 天完整学习周期
8核心学习模块
15必背核心命令
从零开始面向 Git 初学者
Git 基础→创建仓库→
基本操作→撤销→
分支→冲突→
远程仓库→进阶工具
| 模块 | 学习内容 | 建议时间 | 最终能力 |
|---|---|---|---|
| 第1部分 | Git 基础 | 1 天 | 理解 Git 与 GitHub,能安装并配置 |
| 第2部分 | 创建仓库 | 1 天 | git init、git status、看懂 .git |
| 第3部分 | 基本操作 | 1 天 | add/commit/diff/log 核心三连 |
| 第4部分 | 撤销与恢复 | 1 天 | restore、amend、reset 三种模式 |
| 第5部分 | 分支 | 2 天 | 创建、切换、合并分支 |
| 第6部分 | 冲突处理 | 1 天 | 识别和解决合并冲突 |
| 第7部分 | 远程仓库 | 2 天 | clone、fetch、pull、push |
| 第8部分 | 常用工具 | 1 天 | show、stash、tag、.gitignore |
📖 关键术语速览
Repository(仓库)版本控制的目录,含
.git 子目录保存所有历史。Commit(提交)一次完整的版本快照,含作者、时间、说明和文件变更。
Branch(分支)从主线分出的独立开发线,分支间互不影响。
Working Directory(工作区)正在编辑的本地文件。
Staging Area(暂存区)准备好、但还未提交到仓库的"中间层"。
Remote(远程仓库)托管在网络上的仓库副本(如 GitHub)。
📖① Git 基础
学习目标:理解 Git 是什么、为什么用 Git、怎么安装和配置。
📌 什么是 Git
Git 是一种分布式版本控制系统(DVCS),由 Linus Torvalds 在 2005 年为管理 Linux 内核开发。
📌 Git 解决什么问题
文件版本管理
代码修改记录
多人协作
分支开发
版本回退
代码备份
📌 Git 与 GitHub 的区别
| Git | GitHub | |
|---|---|---|
| 本质 | 本地版本控制工具 | 托管 Git 仓库的网站 |
| 运行位置 | 你的电脑 | 云端服务器 |
| 是否需要联网 | 不需要(本地操作) | 需要(推拉远程) |
| 类比 | Office Word 软件 | 在线文档分享平台 |
📌 安装 Git
| 平台 | 安装方式 |
|---|---|
| Windows | 下载 Git for Windows 安装包(含 Git Bash、Git CMD、Git GUI) |
| macOS | brew install git 或安装 Xcode Command Line Tools |
| Linux | sudo apt install git(Ubuntu/Debian) |
📌 查看 Git 版本
$ git --version
git version 2.42.0.windows.2
📌 配置用户信息(必做)
$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"
$ git config --list
user.name=Your Name
[email protected]
core.autocrlf=true
...
为什么必做:每次提交都会记录作者名和邮箱。没配置的话,
git commit 会失败。邮箱注意事项:如果要往 GitHub/GitLab 推送,建议用对应平台的注册邮箱,这样贡献记录能正确显示。
🔑 必须掌握
git --versiongit config --global user.namegit config --global user.emailgit config --list📦② 创建 Git 仓库
Git 的仓库就是一个普通目录,里面多了个 .git 子目录保存历史。
📌 git init:初始化
$ cd myproject
$ git init
Initialized empty Git repository in C:/myproject/.git/
项目结构变化:
myproject ├── .git ← Git 自动生成的"版本数据库" ├── index.html ← 你的项目文件 └── README.md
📌 git status:查看状态(最常用命令)
$ git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
黄金习惯:每次操作前都先
git status 看看状态。这是最安全、最高效的命令之一。📌 4 个核心概念:工作区、暂存区、本地仓库、远程仓库
修改文件 ↓ Working Directory(工作区)
↓ git add ↓
Staging Area(暂存区)
↓ git commit ↓
Local Repository(本地仓库)
↓ git push ↓
Remote Repository(远程仓库)
📌 git clone:克隆已有仓库
$ git clone https://github.com/user/project.git
Cloning into 'project'...
remote: Counting objects: 100, done.
remote: Total 100 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (100/100), done.
🔑 必须掌握
git init 创建仓库git status 查看状态git clone <url> 克隆远程理解工作区/暂存区/仓库三层
💾③ 基本操作:add / commit / log / diff
这是初学者必须练到肌肉记忆的核心三连:
git status → git add → git commit
📌 git add:把修改放入暂存区
$ git add README.md # 添加单个文件
$ git add . # 添加当前目录所有修改(最实用)
$ git add index.html style.css # 添加多个文件
📌 git commit:提交到本地仓库
$ git commit -m "Initial commit"
[main (root-commit) a82f31a] Initial commit
2 files changed, 10 insertions(+)
create mode 100644 README.md
create mode 100644 index.html
提交信息(-m)必须写,不能空。建议用动词开头:"Add login page"、"Update README"、"Fix bug in dashboard"。
📌 git log:查看提交历史
$ git log
commit a82f31a4b3d20f5e8c7b9d1f0a3b2c1d4e5f6a7b (HEAD -> main)
Author: Your Name <[email protected]>
Date: Mon Sep 18 14:23:01 2026 +0800
Initial commit
commit 91b3d20e4a1f5b8c9d2e3f4a5b6c7d8e9f0a1b2c
Author: Your Name <[email protected]>
Date: Mon Sep 18 13:45:22 2026 +0800
Add README
📌 git log 简洁模式
$ git log --oneline
a82f31a Initial commit
91b3d20 Add README
$ git log --oneline --graph --all
* a82f31a (main) Initial commit
* 91b3d20 Add README
📌 git diff:查看修改
$ git diff # 工作区 vs 暂存区
diff --git a/index.html b/index.html
index 1234567..abcdef0 100644
--- a/index.html
+++ b/index.html
@@ -1,3 +1,3 @@
<h1>Old Title</h1>
-<h1>Old Title</h1>
+<h1>New Title</h1>
$ git diff --staged # 暂存区 vs 最近一次提交
$ git diff HEAD # 工作区 vs 最近一次提交
📌 三者关系
工作区 → git diff → 暂存区 → git diff --staged → 最近一次提交
🔑 必须掌握
git add .git commit -m "..."git log --onelinegit diff 与 git diff --staged↩️④ 撤销与恢复
改错了、提交错了、想反悔——这是 Git 必须掌握的"后悔药"。
📌 git restore:撤销工作区修改
$ git restore index.html # 放弃工作区的修改
不可恢复!
restore 会直接覆盖工作区文件,放弃你对该文件的所有未保存修改。📌 git restore --staged:取消暂存
$ git restore --staged index.html # 把文件从暂存区拿出来(修改保留在工作区)
📌 git commit --amend:修改最后一次提交
$ git commit -m "Add login page"
# 啊,提交信息写错了,或者漏了文件!
$ git add missed-file.html
$ git commit --amend -m "Add login page and missed file"
适用场景:刚刚提交、但发现提交信息写错或漏了文件。已推送(push)到远程的提交不要 amend。
📌 git reset 三种模式
| 模式 | 影响 | 用法 |
|---|---|---|
--soft | 只改 HEAD 指针,修改保留在暂存区 | 想"撤销提交但保留文件改动" |
--mixed | HEAD 指针+暂存区都还原,修改保留在工作区(默认) | 想"撤销提交+撤销暂存,但保留工作区" |
--hard | HEAD 指针+暂存区+工作区全部还原 | 想"完全回到某次提交" |
$ git reset --soft HEAD~1 # 撤销最近 1 次 commit,文件改动保留在暂存区
$ git reset HEAD~1 # 等价于 --mixed
$ git reset --hard HEAD~1 # ⚠️ 完全回到上一次提交,未提交的改动全没了
危险操作:
git reset --hard 会永久丢弃未保存的工作区修改。除非你很确定,否则不要用。已经推送的提交也不要用 reset。🔑 必须掌握
git restore 撤销工作区git restore --staged 取消暂存git commit --amend 改最后提交知道
reset --soft/--mixed/--hard 区别🌿⑤ 分支 Branch
分支是 Git 的灵魂功能。学会分支,就学会了并行开发和团队协作。
📌 什么是分支
main
│
C4 ─ C5 ─ C6
│
C3
│
C1 ─ C2
develop (从 main 分出)
│
D3
│
D1 ─ D2
📌 git branch:查看/创建分支
$ git branch # 列出所有本地分支(* 表示当前)
* main
$ git branch feature # 创建分支(不切换)
feature
* main
📌 git switch:切换分支(推荐)
$ git switch feature # 切换到 feature
Switched to branch 'feature'
$ git switch -c feature-login # 创建并切换(c = create)
Switched to a new branch 'feature-login'
推荐用
switch 而不是 checkout。checkout 功能太多(既能切分支又能恢复文件),容易出错;switch 只管切换分支,更清晰。📌 分支综合实例
# 当前在 main
$ git switch -c feature-login
Switched to a new branch 'feature-login'
# 修改代码并提交
$ git add .
$ git commit -m "Add login feature"
[feature-login 9c8d7b2] Add login feature
1 file changed, 25 insertions(+)
📌 git merge:合并分支
$ git switch main # 先切回 main
Switched to branch 'main'
$ git merge feature-login # 把 feature-login 合并到 main
Updating a82f31a..9c8d7b2
Fast-forward
index.html | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
📌 删除分支
$ git branch -d feature-login # 安全删除(已合并)
$ git branch -D feature-login # 强制删除(未合并,会丢改动)
📌 看分支图
$ git log --oneline --graph --all
* 9c8d7b2 (feature-login) Add login feature
* a82f31a (HEAD -> main) Initial commit
🔑 必须掌握
git branchgit switchgit switch -c 创建并切换git mergegit branch -d 删除分支能用
--graph 看分支图💥⑥ 合并冲突 Conflict
冲突不可怕——它只是告诉你"两个人改了同一处,你来决定保留哪个"。
📌 冲突如何产生
main 分支:把 index.html 标题改成 "Welcome"
↓
同一个文件同一行被两个人修改
↓
feature 分支:把 index.html 标题改成 "Hello"
↓
git merge 时发生 CONFLICT
📌 冲突标记
Git 会在文件中插入冲突标记,需要你手动解决:
<h1>
<<<<<<< HEAD
Welcome
=======
Hello
>>>>>>> feature
</h1>
冲突标记含义:
*
*
*
*
<<<<<<< HEAD —— 当前分支的内容*
======= —— 分隔线*
>>>>>>> feature —— 要合并过来的分支
📌 解决冲突的步骤
看到
CONFLICT 提示后,打开冲突文件。手动决定保留哪部分内容,把冲突标记(
<<<<<<< / ======= / >>>>>>>)全部删除。必要时把双方内容合并到一起。
git add <file> 标记冲突已解决。git commit 完成合并(Git 会生成默认的 merge 提交信息)。📌 实操示例
$ git merge feature
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
$ git status
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
# 编辑 index.html,保留期望内容,删除冲突标记...
$ git add index.html
$ git commit
[main 3f4e2a1] Merge branch 'feature'
小贴士:VS Code、IntelliJ IDEA 等编辑器都有图形化冲突解决界面,点击 Accept Current/Incoming/Both 即可,比手动改快得多。
常见错误:冲突文件中漏删了
<<<<<<< 或 >>>>>>> 标记就直接 commit——会导致标记作为文本保存到代码里。🔑 必须掌握
识别冲突标记
手动编辑文件解决冲突
git add 标记已解决git commit 完成合并☁️⑦ 远程仓库
本地的提交要推到远程(如 GitHub)才算"备份"和"分享"。
📌 远程仓库平台
| 平台 | 特点 |
|---|---|
| GitHub | 全球最大,英文为主,开源生态 |
| GitLab | 可自部署,企业常用 |
| Gitee | 国内访问快,中文界面 |
| Bitbucket | 与 Atlassian 工具链集成 |
📌 git remote:管理远程地址
$ git remote -v # 查看所有远程仓库
origin https://github.com/user/project.git (fetch)
origin https://github.com/user/project.git (push)
$ git remote add origin <url> # 添加远程(origin 是约定名)
$ git remote remove origin # 删除远程
📌 git push:推送到远程
$ git push -u origin main # 第一次推送,-u 建立追踪关系
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 320 bytes | 320.00 KiB/s
* To https://github.com/user/project.git
a82f31a..3f4e2a1 main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
$ git push # 之后直接 push(已建立追踪)
📌 git clone:克隆远程仓库
$ git clone https://github.com/user/project.git
Cloning into 'project'...
# 也可以用 SSH
$ git clone [email protected]:user/project.git
📌 git fetch vs git pull
| 命令 | 作用 | 等价于 |
|---|---|---|
git fetch | 下载远程更新,但不合并 | 只 fetch 不 merge |
git pull | 下载并自动合并到当前分支 | fetch + merge |
$ git fetch origin # 只下载,可先看一眼
$ git log origin/main --oneline # 查看远程 main 的更新
$ git merge origin/main # 再合并
$ git pull # 一步搞定:fetch + merge
推荐:协作时先用
fetch 看一眼远程更新再决定怎么合并;个人项目直接 pull 即可。📌 团队开发工作流
开始工作 → git pull → 创建/切换分支 → 修改代码 → git add → git commit → git push
🔑 必须掌握
git remote add origingit push -u origin maingit clonegit pull区分
fetch 和 pull理解本地/远程仓库的双向关系
🔧⑧ 常用工具
📌 git show:查看某次提交
$ git show HEAD # 查看最近一次提交
$ git show a82f31a # 查看指定 commit
📌 git stash:暂存工作
正在改 A 任务,突然要切到 B 任务修 bug,但 A 还没改完不想提交——用 stash 暂存起来:
$ git stash # 暂存当前所有未提交的修改
Saved working directory and index state WIP on main: a82f31a
# 这时切到其他分支处理 bug...
$ git stash pop # 回到原分支,恢复暂存
$ git stash list # 查看暂存列表
$ git stash drop # 删除某个暂存
📌 git tag:版本标签
$ git tag v1.0 # 给当前 commit 打轻量标签
$ git tag -a v1.0 -m "正式版" # 打带注释的标签
$ git tag # 查看所有标签
$ git push origin v1.0 # 推送标签到远程
📌 .gitignore:忽略文件
仓库中不应该被跟踪的文件(如临时文件、密码、构建产物)放在 .gitignore:
常见 .gitignore 示例:
# 操作系统 .DS_Store Thumbs.db # 编辑器 .vscode/ .idea/ *.swp # Node.js node_modules/ npm-debug.log # Python __pycache__/ *.pyc # 密钥 .env *.pem
📌 图形化 Git 工具
| 工具 | 特点 |
|---|---|
| Git GUI | Git 自带的图形界面 |
| Sourcetree | 免费、跨平台、功能强 |
| VS Code 源代码管理 | 编辑器内置,无需切换 |
| GitKraken | 漂亮好用,免费版够用 |
📌 ⭐ 初学者必背 15 个命令
git --versiongit configgit initgit statusgit addgit commitgit loggit diffgit restoregit branchgit switchgit mergegit clonegit pullgit push以上 15 个全掌握 = Git 入门毕业
🎯综合实训:个人 HTML 学习网站 Git 项目
用一个 9 步的真实项目,把所有命令跑一遍。
📌 项目结构
MyWeb
├── index.html ├── style.css ├── script.js └── README.md
📌 完整操作流程(9 步)
# ① 进入项目目录
$ cd MyWeb
# ② 初始化 Git
$ git init
Initialized empty Git repository in C:/MyWeb/.git/
# ③ 查看状态(应该是干净的)
$ git status
On branch main
nothing to commit, working tree clean
# ④ 创建文件(README.md 和 index.html),然后第一次提交
$ git add .
$ git commit -m "Initial website"
[main (root-commit) a82f31a] Initial website
2 files changed, 30 insertions(+)
# ⑤ 创建并切换到开发分支
$ git switch -c feature-home
Switched to a new branch 'feature-home'
# ⑥ 修改 index.html 和 style.css,然后提交
$ git add .
$ git commit -m "Improve homepage"
# ⑦ 切回 main,合并
$ git switch main
$ git merge feature-home
Updating a82f31a..9c8d7b2
Fast-forward
index.html | 25 +++++++++++
style.css | 18 +++++++
2 files changed, 43 insertions(+)
# ⑧ 看一眼完整历史(含分支图)
$ git log --oneline --graph --all
* 9c8d7b2 (feature-home) Improve homepage
* a82f31a (HEAD -> main) Initial website
# ⑨ 连接远程(先去 GitHub 建一个空仓库),推送
$ git remote add origin https://github.com/yourname/MyWeb.git
$ git push -u origin main
* To https://github.com/yourname/MyWeb.git
a82f31a..9c8d7b2 main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
📌 12 项最终能力测试
能解释 Git 是什么
能区分 Git 与 GitHub
能创建 Git 仓库
能用 git status 查看状态
能提交文件
能查看提交历史
能查看文件修改(diff)
能撤销简单修改
能创建并切换分支
能合并分支
能克隆远程项目
能从远程拉取/推送代码
📈 Git 知识体系
| 层次 | 核心内容 | 学习重点 |
|---|---|---|
| 第一层:基础 | Git 是什么、安装、配置 | 会用 git --version 和 config |
| 第二层:仓库 | init、status、.git、Repository | 理解工作区/暂存区/仓库 |
| 第三层:基本操作 | add、commit、log、diff | 核心三连形成肌肉记忆 |
| 第四层:撤销 | restore、amend、reset | 理解 reset 三种模式区别 |
| 第五层:分支 | branch、switch、merge | 独立开发不干扰主线 |
| 第六层:冲突 | 识别和解决冲突 | 冲突不可怕,手动编辑即可 |
| 第七层:远程 | remote、clone、fetch、pull、push | 团队协作核心 |
| 第八层:工具 | show、stash、tag、.gitignore | 提效 + 团队规范 |
最后一句话:Git 学习的核心不是背命令,而是掌握这条主线 —— 修改 → status → diff → add → commit → branch → merge → pull → push。这条线跑顺,再学
stash、rebase、tag 等进阶就容易了。