first commit
This commit is contained in:
commit
669f49b1d6
342 files changed
+320811
No files matched your search
+544
@@ -0,0 +1,544 @@
|
||||
# Git 命令学习
|
||||
|
||||
> 适用环境:Windows + PowerShell。
|
||||
> 学习目标:先掌握日常高频 Git 命令,再理解分支、远程协作、撤销回退和冲突解决。
|
||||
|
||||
## 1. Git 核心概念
|
||||
|
||||
Git 是一个分布式版本控制工具,用来记录代码或文档的每次变化。
|
||||
|
||||
最重要的是理解三个区域:
|
||||
|
||||
```text
|
||||
工作区 -> git add -> 暂存区 -> git commit -> 本地仓库
|
||||
```
|
||||
|
||||
| 区域 | 含义 | 常见命令 |
|
||||
|---|---|---|
|
||||
| 工作区 | 你正在编辑的文件 | `git status`、`git diff` |
|
||||
| 暂存区 | 准备提交的修改 | `git add`、`git restore --staged` |
|
||||
| 本地仓库 | 已经形成版本记录的提交 | `git commit`、`git log` |
|
||||
|
||||
## 2. 初始化与基础配置
|
||||
|
||||
查看 Git 是否安装:
|
||||
|
||||
```powershell
|
||||
git --version
|
||||
```
|
||||
|
||||
设置用户名和邮箱:
|
||||
|
||||
```powershell
|
||||
git config --global user.name "你的名字"
|
||||
git config --global user.email "你的邮箱"
|
||||
```
|
||||
|
||||
查看配置:
|
||||
|
||||
```powershell
|
||||
git config --list
|
||||
```
|
||||
|
||||
在当前目录初始化 Git 仓库:
|
||||
|
||||
```powershell
|
||||
git init
|
||||
```
|
||||
|
||||
## 3. 日常提交流程
|
||||
|
||||
查看当前状态:
|
||||
|
||||
```powershell
|
||||
git status
|
||||
```
|
||||
|
||||
把指定文件加入暂存区:
|
||||
|
||||
```powershell
|
||||
git add 文件名
|
||||
```
|
||||
|
||||
把当前目录所有修改加入暂存区:
|
||||
|
||||
```powershell
|
||||
git add .
|
||||
```
|
||||
|
||||
提交修改:
|
||||
|
||||
```powershell
|
||||
git commit -m "提交说明"
|
||||
```
|
||||
|
||||
查看提交历史:
|
||||
|
||||
```powershell
|
||||
git log
|
||||
git log --oneline
|
||||
```
|
||||
|
||||
推荐日常流程:
|
||||
|
||||
```powershell
|
||||
git status
|
||||
git add .
|
||||
git commit -m "说明本次修改"
|
||||
git log --oneline
|
||||
```
|
||||
|
||||
## 4. 查看修改差异
|
||||
|
||||
查看工作区中还没有暂存的修改:
|
||||
|
||||
```powershell
|
||||
git diff
|
||||
```
|
||||
|
||||
查看已经 `git add` 但还没有提交的修改:
|
||||
|
||||
```powershell
|
||||
git diff --staged
|
||||
```
|
||||
|
||||
查看某个文件的修改:
|
||||
|
||||
```powershell
|
||||
git diff -- 文件名
|
||||
```
|
||||
|
||||
查看提交历史图:
|
||||
|
||||
```powershell
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
|
||||
## 5. 分支命令
|
||||
|
||||
查看分支:
|
||||
|
||||
```powershell
|
||||
git branch
|
||||
```
|
||||
|
||||
创建分支:
|
||||
|
||||
```powershell
|
||||
git branch dev
|
||||
```
|
||||
|
||||
切换分支:
|
||||
|
||||
```powershell
|
||||
git switch dev
|
||||
```
|
||||
|
||||
创建并切换分支:
|
||||
|
||||
```powershell
|
||||
git switch -c feature/login
|
||||
```
|
||||
|
||||
合并分支:
|
||||
|
||||
```powershell
|
||||
git switch main
|
||||
git merge feature/login
|
||||
```
|
||||
|
||||
删除已经合并的分支:
|
||||
|
||||
```powershell
|
||||
git branch -d feature/login
|
||||
```
|
||||
|
||||
强制删除分支:
|
||||
|
||||
```powershell
|
||||
git branch -D feature/login
|
||||
```
|
||||
|
||||
> 注意:`-D` 会强制删除分支,使用前要确认该分支不再需要。
|
||||
|
||||
## 6. 远程仓库命令
|
||||
|
||||
克隆远程仓库:
|
||||
|
||||
```powershell
|
||||
git clone 仓库地址
|
||||
```
|
||||
|
||||
查看远程仓库地址:
|
||||
|
||||
```powershell
|
||||
git remote -v
|
||||
```
|
||||
|
||||
添加远程仓库:
|
||||
|
||||
```powershell
|
||||
git remote add origin 仓库地址
|
||||
```
|
||||
|
||||
首次推送本地 `main` 分支到远程:
|
||||
|
||||
```powershell
|
||||
git push -u origin main
|
||||
```
|
||||
|
||||
后续推送:
|
||||
|
||||
```powershell
|
||||
git push
|
||||
```
|
||||
|
||||
拉取远程更新并自动合并:
|
||||
|
||||
```powershell
|
||||
git pull
|
||||
```
|
||||
|
||||
只获取远程更新,不自动合并:
|
||||
|
||||
```powershell
|
||||
git fetch
|
||||
```
|
||||
|
||||
常见区别:
|
||||
|
||||
| 命令 | 作用 |
|
||||
|---|---|
|
||||
| `git fetch` | 获取远程变化,但不改当前工作分支 |
|
||||
| `git pull` | 获取远程变化,并尝试合并到当前分支 |
|
||||
| `git push` | 把本地提交推送到远程仓库 |
|
||||
|
||||
## 7. 撤销与回退
|
||||
|
||||
撤销工作区中某个文件的修改:
|
||||
|
||||
```powershell
|
||||
git restore 文件名
|
||||
```
|
||||
|
||||
把文件从暂存区移回工作区:
|
||||
|
||||
```powershell
|
||||
git restore --staged 文件名
|
||||
```
|
||||
|
||||
撤销上一次提交,但保留修改在暂存区:
|
||||
|
||||
```powershell
|
||||
git reset --soft HEAD~1
|
||||
```
|
||||
|
||||
撤销上一次提交,修改回到工作区:
|
||||
|
||||
```powershell
|
||||
git reset --mixed HEAD~1
|
||||
```
|
||||
|
||||
撤销上一次提交,并丢弃相关修改:
|
||||
|
||||
```powershell
|
||||
git reset --hard HEAD~1
|
||||
```
|
||||
|
||||
生成一个新的反向提交,用来撤销某次提交:
|
||||
|
||||
```powershell
|
||||
git revert 提交ID
|
||||
```
|
||||
|
||||
安全建议:
|
||||
|
||||
| 场景 | 推荐命令 |
|
||||
|---|---|
|
||||
| 只是撤销某个文件的未提交修改 | `git restore 文件名` |
|
||||
| 已经提交但还没推送,想重新整理 | `git reset --soft HEAD~1` |
|
||||
| 已经推送到远程,想撤销影响 | `git revert 提交ID` |
|
||||
| 明确要丢弃所有修改 | `git reset --hard` |
|
||||
|
||||
> 高风险命令:`git reset --hard` 会丢弃修改,执行前必须确认没有重要内容。
|
||||
|
||||
## 8. 临时保存修改
|
||||
|
||||
当前工作做到一半,但需要先切换分支时,可以用 `stash`。
|
||||
|
||||
保存当前修改:
|
||||
|
||||
```powershell
|
||||
git stash
|
||||
```
|
||||
|
||||
查看保存列表:
|
||||
|
||||
```powershell
|
||||
git stash list
|
||||
```
|
||||
|
||||
恢复最近一次保存:
|
||||
|
||||
```powershell
|
||||
git stash pop
|
||||
```
|
||||
|
||||
恢复但不删除保存记录:
|
||||
|
||||
```powershell
|
||||
git stash apply
|
||||
```
|
||||
|
||||
删除某条保存记录:
|
||||
|
||||
```powershell
|
||||
git stash drop stash@{0}
|
||||
```
|
||||
|
||||
## 9. 冲突解决
|
||||
|
||||
当两个人或两个分支修改了同一处内容,合并时可能出现冲突。
|
||||
|
||||
典型流程:
|
||||
|
||||
```powershell
|
||||
git pull
|
||||
```
|
||||
|
||||
如果出现冲突,打开冲突文件,会看到类似内容:
|
||||
|
||||
```text
|
||||
<<<<<<< HEAD
|
||||
当前分支的内容
|
||||
=======
|
||||
对方分支的内容
|
||||
>>>>>>> branch-name
|
||||
```
|
||||
|
||||
处理步骤:
|
||||
|
||||
1. 手动编辑文件,保留正确内容。
|
||||
2. 删除 `<<<<<<<`、`=======`、`>>>>>>>` 标记。
|
||||
3. 执行:
|
||||
|
||||
```powershell
|
||||
git add 冲突文件
|
||||
git commit
|
||||
```
|
||||
|
||||
## 10. 标签命令
|
||||
|
||||
标签常用于标记版本号。
|
||||
|
||||
查看标签:
|
||||
|
||||
```powershell
|
||||
git tag
|
||||
```
|
||||
|
||||
创建标签:
|
||||
|
||||
```powershell
|
||||
git tag v1.0.0
|
||||
```
|
||||
|
||||
创建带说明的标签:
|
||||
|
||||
```powershell
|
||||
git tag -a v1.0.0 -m "发布 v1.0.0"
|
||||
```
|
||||
|
||||
推送标签:
|
||||
|
||||
```powershell
|
||||
git push origin v1.0.0
|
||||
```
|
||||
|
||||
推送所有标签:
|
||||
|
||||
```powershell
|
||||
git push origin --tags
|
||||
```
|
||||
|
||||
## 11. .gitignore
|
||||
|
||||
`.gitignore` 用来声明哪些文件不需要提交到 Git。
|
||||
|
||||
常见示例:
|
||||
|
||||
```gitignore
|
||||
# 日志
|
||||
*.log
|
||||
|
||||
# 系统文件
|
||||
Thumbs.db
|
||||
.DS_Store
|
||||
|
||||
# 依赖目录
|
||||
node_modules/
|
||||
|
||||
# 构建产物
|
||||
dist/
|
||||
build/
|
||||
|
||||
# 本地配置
|
||||
.env
|
||||
local.properties
|
||||
```
|
||||
|
||||
如果文件已经被 Git 跟踪,后来再加入 `.gitignore` 不会自动生效,需要先取消跟踪:
|
||||
|
||||
```powershell
|
||||
git rm --cached 文件名
|
||||
```
|
||||
|
||||
## 12. 常用排查命令
|
||||
|
||||
查看某个文件每一行是谁改的:
|
||||
|
||||
```powershell
|
||||
git blame 文件名
|
||||
```
|
||||
|
||||
查看所有操作记录,包括 reset 前的记录:
|
||||
|
||||
```powershell
|
||||
git reflog
|
||||
```
|
||||
|
||||
查看某次提交详情:
|
||||
|
||||
```powershell
|
||||
git show 提交ID
|
||||
```
|
||||
|
||||
查看当前分支与远程分支差异:
|
||||
|
||||
```powershell
|
||||
git status
|
||||
git log --oneline --graph --decorate --all
|
||||
```
|
||||
|
||||
## 13. 推荐学习练习
|
||||
|
||||
建议新建一个专门练习目录,例如:
|
||||
|
||||
```powershell
|
||||
mkdir git-practice
|
||||
cd git-practice
|
||||
git init
|
||||
```
|
||||
|
||||
练习 1:第一次提交
|
||||
|
||||
```powershell
|
||||
New-Item readme.md
|
||||
Add-Content readme.md "# Git Practice"
|
||||
git status
|
||||
git add readme.md
|
||||
git commit -m "初始化练习文档"
|
||||
git log --oneline
|
||||
```
|
||||
|
||||
练习 2:查看差异
|
||||
|
||||
```powershell
|
||||
Add-Content readme.md "第一次修改"
|
||||
git diff
|
||||
git add readme.md
|
||||
git diff --staged
|
||||
git commit -m "更新练习文档"
|
||||
```
|
||||
|
||||
练习 3:创建分支
|
||||
|
||||
```powershell
|
||||
git switch -c feature/note
|
||||
Add-Content readme.md "feature 分支内容"
|
||||
git add readme.md
|
||||
git commit -m "添加 feature 分支内容"
|
||||
git switch main
|
||||
git merge feature/note
|
||||
```
|
||||
|
||||
练习 4:撤销修改
|
||||
|
||||
```powershell
|
||||
Add-Content readme.md "这是一行想撤销的内容"
|
||||
git status
|
||||
git restore readme.md
|
||||
git status
|
||||
```
|
||||
|
||||
练习 5:查看历史图
|
||||
|
||||
```powershell
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
|
||||
## 14. 日常速查表
|
||||
|
||||
| 目标 | 命令 |
|
||||
|---|---|
|
||||
| 查看状态 | `git status` |
|
||||
| 查看未暂存差异 | `git diff` |
|
||||
| 查看已暂存差异 | `git diff --staged` |
|
||||
| 添加文件 | `git add 文件名` |
|
||||
| 添加全部修改 | `git add .` |
|
||||
| 提交 | `git commit -m "说明"` |
|
||||
| 查看历史 | `git log --oneline` |
|
||||
| 创建并切换分支 | `git switch -c 分支名` |
|
||||
| 切换分支 | `git switch 分支名` |
|
||||
| 合并分支 | `git merge 分支名` |
|
||||
| 拉取远程更新 | `git pull` |
|
||||
| 推送到远程 | `git push` |
|
||||
| 临时保存修改 | `git stash` |
|
||||
| 恢复临时保存 | `git stash pop` |
|
||||
| 撤销工作区修改 | `git restore 文件名` |
|
||||
| 取消暂存 | `git restore --staged 文件名` |
|
||||
| 查看完整操作记录 | `git reflog` |
|
||||
|
||||
## 15. 学习优先级
|
||||
|
||||
第一优先级:
|
||||
|
||||
- `git status`
|
||||
- `git add`
|
||||
- `git commit`
|
||||
- `git log`
|
||||
- `git diff`
|
||||
|
||||
第二优先级:
|
||||
|
||||
- `git branch`
|
||||
- `git switch`
|
||||
- `git merge`
|
||||
- `git pull`
|
||||
- `git push`
|
||||
|
||||
第三优先级:
|
||||
|
||||
- `git restore`
|
||||
- `git reset`
|
||||
- `git revert`
|
||||
- `git stash`
|
||||
- `git reflog`
|
||||
|
||||
## 16. 容易踩坑的点
|
||||
|
||||
- `git add .` 会把当前目录下所有修改都加入暂存区,提交前要用 `git status` 和 `git diff --staged` 检查。
|
||||
- `git reset --hard` 会丢弃修改,重要内容可能找不回来。
|
||||
- 已经推送到远程的提交,优先用 `git revert`,不要随便改公共历史。
|
||||
- 拉取远程更新前,最好先确认本地是否有未提交修改。
|
||||
- 合并冲突时,必须删除冲突标记后再提交。
|
||||
- `.gitignore` 只对未被 Git 跟踪的新文件自动生效。
|
||||
|
||||
## 17. 一句话记忆
|
||||
|
||||
```text
|
||||
status 看状态,diff 看变化,add 进暂存,commit 成版本;
|
||||
branch 管分支,switch 换分支,merge 合分支;
|
||||
pull 拉远程,push 推远程;
|
||||
restore 撤文件,reset 改历史,revert 安全反做,reflog 救现场。
|
||||
```
|
||||
Reference in new issue
Block a user