Files
mkdocs/docs/git/git常用命令.md
2026-01-13 11:12:42 +08:00

230 lines
5.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
我来为你详细介绍 Git 的使用方法和常用命令。
## **Git 基础概念**
- **仓库 (Repository)**:存储项目代码和历史记录的地方
- **工作区 (Working Directory)**:你正在编辑的文件
- **暂存区 (Staging Area)**:准备提交的文件
- **本地仓库 (Local Repository)**:本地保存的版本历史
- **远程仓库 (Remote Repository)**GitHub/GitLab 等平台的仓库
## **Git 工作流程**
```
工作区 → `git add` → 暂存区 → `git commit` → 本地仓库 → `git push` → 远程仓库
```
## **常用命令分类**
### **1. 配置相关**
```bash
# 设置用户信息
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"
# 查看配置
git config --list
# 设置默认编辑器(如 VS Code
git config --global core.editor "code --wait"
```
### **2. 仓库操作**
```bash
# 初始化新仓库
git init
# 克隆远程仓库
git clone <仓库URL>
git clone https://github.com/username/repo.git
# 查看仓库状态
git status
# 查看远程仓库
git remote -v
# 添加远程仓库
git remote add origin <仓库URL>
```
### **3. 基础操作**
```bash
# 添加文件到暂存区
git add 文件名 # 添加特定文件
git add . # 添加所有修改
git add *.js # 添加所有js文件
# 提交到本地仓库
git commit -m "提交说明"
# 添加并提交(两步合并)
git commit -am "提交说明" # 只能提交已跟踪的文件
# 查看提交历史
git log
git log --oneline # 简洁显示
git log --graph # 图形化显示分支
git log -p # 显示具体修改内容
```
### **4. 分支管理**
```bash
# 查看分支
git branch # 查看本地分支
git branch -a # 查看所有分支(包括远程)
git branch -r # 查看远程分支
# 创建分支
git branch 分支名 # 创建分支
git checkout -b 分支名 # 创建并切换到该分支
git switch -c 分支名 # 新版本推荐Git 2.23+
# 切换分支
git checkout 分支名
git switch 分支名 # 新版本推荐
# 合并分支
git merge 分支名 # 将指定分支合并到当前分支
# 删除分支
git branch -d 分支名 # 删除已合并的分支
git branch -D 分支名 # 强制删除分支
```
### **5. 远程操作**
```bash
# 推送代码
git push origin 分支名
git push -u origin main # 首次推送并建立关联
# 拉取代码
git pull origin 分支名 # 拉取并合并
git fetch origin # 只拉取不合并
# 同步远程分支
git fetch --prune # 删除本地不存在的远程分支引用
```
### **6. 撤销操作**
```bash
# 撤销工作区修改
git checkout -- 文件名 # 丢弃工作区修改
git restore 文件名 # 新版本推荐
# 撤销暂存区文件
git reset HEAD 文件名 # 从暂存区移除
git restore --staged 文件名 # 新版本推荐
# 修改上次提交
git commit --amend # 修改提交信息或内容
# 回退到指定版本
git reset --soft HEAD^ # 撤销commit保留修改到暂存区
git reset --mixed HEAD^ # 撤销commit保留修改到工作区默认
git reset --hard HEAD^ # 彻底删除commit和修改
git reset --hard commit_id # 回退到指定commit
```
### **7. 暂存修改**
```bash
# 临时保存修改
git stash # 保存当前修改
git stash save "说明" # 保存并添加说明
git stash list # 查看暂存列表
git stash apply # 恢复最近暂存的修改
git stash pop # 恢复并删除暂存记录
git stash drop # 删除暂存记录
```
### **8. 标签管理**
```bash
# 创建标签
git tag v1.0 # 创建轻量标签
git tag -a v1.0 -m "版本说明" # 创建附注标签
# 查看标签
git tag # 列出所有标签
git show v1.0 # 查看标签详情
# 推送标签
git push origin v1.0 # 推送单个标签
git push origin --tags # 推送所有标签
```
### **9. 比较差异**
```bash
git diff # 比较工作区和暂存区
git diff --staged # 比较暂存区和最新提交
git diff HEAD # 比较工作区和最新提交
git diff 分支1 分支2 # 比较两个分支
git diff commit1 commit2 # 比较两次提交
```
## **实用工作流程示例**
### **场景1日常开发流程**
```bash
# 1. 开始新功能
git checkout main
git pull origin main
git checkout -b feature/new-feature
# 2. 开发
git add .
git commit -m "添加新功能"
# 3. 推送
git push origin feature/new-feature
# 4. 合并到主分支
git checkout main
git merge feature/new-feature
git push origin main
```
### **场景2处理紧急bug**
```bash
# 1. 暂存当前工作
git stash
# 2. 创建修复分支
git checkout -b hotfix/bug-fix
# 3. 修复并提交
# ... 修复代码 ...
git add .
git commit -m "修复紧急bug"
# 4. 合并修复
git checkout main
git merge hotfix/bug-fix
git push origin main
# 5. 恢复之前的工作
git checkout feature/your-branch
git stash pop
```
## **实用技巧**
1. **设置别名**
```bash
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
```
2. **查看帮助**
```bash
git help <命令>
git <命令> --help
```
3. **忽略文件**:创建 `.gitignore` 文件
## **注意事项**
- 重要操作前先备份或确认
- 频繁提交小改动,保持提交信息清晰
- 推送前先拉取最新代码
- 使用分支进行功能开发
这些是 Git 最常用的命令,掌握了这些就能应对大部分日常开发需求。