Git 是目前最流行的分布式版本控制系统,由 Linux 之父 Linus Torvalds 开发。掌握 Git 命令能让你高效管理代码版本、协作开发、回滚错误等。
下面按使用场景分类整理最常用、最实用的 Git 命令,每条都包含:
(推荐先安装最新版 Git:https://git-scm.com/downloads)
1. 配置 Git(必须先做)
git config --global user.name "你的名字"
git config --global user.email "你的邮箱@example.com"
git config --global core.editor vim # 设置默认编辑器
git config --list # 查看所有配置
git config --global alias.st status # 设置别名(git st = git status)
2. 初始化与克隆仓库
| 命令 | 作用 | 示例 |
|---|
| git init | 在当前目录初始化本地仓库 | git init |
| git init --bare | 创建裸仓库(适合做远程仓库) | git init --bare /path/to/repo.git |
| git clone <url> | 克隆远程仓库到本地 | git clone https://github.com/user/repo.git |
| git clone --depth=1 <url> | 浅克隆(只下载最新一次提交,速度快) | git clone --depth=1 <url> |
| git clone -b <branch> <url> | 克隆指定分支 | git clone -b dev <url> |
3. 查看状态与差异
| 命令 | 作用 | 示例 |
|---|
| git status | 查看工作区、暂存区状态(最常用!) | git status |
| git status -s | 简洁模式显示 | git status -s |
| git diff | 比较工作区 vs 暂存区 | git diff |
| git diff --cached | 比较暂存区 vs 上次提交 | git diff --cached |
| git diff HEAD | 比较工作区 vs 最后一次提交 | git diff HEAD |
| git diff <commit1>..<commit2> | 比较两个提交 | git diff abc123..def456 |
4. 文件操作(添加、删除、重命名)
| 命令 | 作用 | 示例 |
|---|
| git add <file> | 添加文件到暂存区 | git add README.md |
| git add . 或 git add -A | 添加所有修改(推荐) | git add . |
| git add -p | 交互式分块添加(精细控制) | git add -p |
| git rm <file> | 删除文件并放入暂存区 | git rm old.txt |
| git rm --cached <file> | 取消追踪,但保留本地文件 | git rm --cached .env |
| git mv <old> <new> | 重命名/移动文件 | git mv a.txt b.txt |
5. 提交操作
| 命令 | 作用 | 示例 |
|---|
| git commit -m "描述" | 提交暂存区内容 | git commit -m "feat: 添加登录页面" |
| git commit -am "描述" | 自动 add 修改的文件并提交(不含新文件) | git commit -am "fix: 修复bug" |
| git commit --amend | 修改上一次提交(消息或内容) | git commit --amend -m "新消息" |
| git commit --amend --no-edit | 只改内容,不改消息 | git commit --amend --no-edit |
6. 查看历史
| 命令 | 作用 | 示例 |
|---|
| git log | 查看提交历史 | git log |
| git log --oneline --graph | 简洁 + 分支图形 | git log --oneline --graph |
| git log -p | 显示每次提交的差异 | git log -p |
| git log --author="名字" | 查看某人提交 | git log --author="张三" |
| git show <commit> | 查看某次提交详情 | git show abc123 |
| git reflog | 查看所有 HEAD 移动记录(找回丢失 commit 神器) | git reflog |
7. 分支管理(最核心功能)
| 命令 | 作用 | 示例 |
|---|
| git branch | 列出本地分支 | git branch |
| git branch -a | 列出所有分支(含远程) | git branch -a |
| git branch <name> | 创建分支 | git branch feature/login |
| git checkout -b <name> | 创建并切换分支(旧方式) | git checkout -b dev |
| git switch -c <name>(Git 2.23+ 推荐) | 创建并切换分支 | git switch -c dev |
| git checkout <branch> / git switch <branch> | 切换分支 | git switch main |
| git merge <branch> | 合并分支(普通合并) | git merge dev |
| git merge --no-ff <branch> | 非快进合并(保留分支记录) | git merge --no-ff dev |
| git rebase <branch> | 变基合并(让历史更线性) | git rebase main |
| git branch -d <branch> | 删除已合并分支 | git branch -d old-branch |
| git branch -D <branch> | 强制删除 | git branch -D old-branch |
8. 远程仓库操作
| 命令 | 作用 | 示例 |
|---|
| git remote -v | 查看远程仓库 | git remote -v |
| git remote add origin <url> | 添加远程仓库 | git remote add origin https://... |
| git fetch | 获取远程最新内容(不合并) | git fetch origin |
| git pull | 拉取并合并(= fetch + merge) | git pull origin main |
| git push | 推送本地分支 | git push origin main |
| git push -u origin <branch> | 第一次推送并建立追踪 | git push -u origin main |
| git push origin --delete <branch> | 删除远程分支 | git push origin --delete old |
9. 撤销与恢复(救命命令)
| 命令 | 作用 | 示例 |
|---|
| git reset --soft HEAD~1 | 撤销上次 commit(保留修改在暂存区) | git reset --soft HEAD~1 |
| git reset --mixed HEAD~1(默认) | 撤销 commit(修改回到工作区) | git reset HEAD~1 |
| git reset --hard HEAD~1 | 彻底撤销(危险,会丢弃修改) | git reset --hard origin/main |
| git revert <commit> | 撤销某次提交(生成新 commit,安全) | git revert abc123 |
| git checkout -- <file> / git restore <file> | 丢弃工作区单个文件修改 | git restore README.md |
| git restore . | 丢弃所有工作区修改 | git restore . |
| git clean -fd | 删除未跟踪文件/目录 | git clean -fd |
10. 临时保存(Stash)
git stash # 保存当前修改
git stash save "描述" # 带描述保存
git stash list # 查看所有 stash
git stash pop # 恢复并删除最新 stash
git stash apply # 恢复但不删除
git stash drop stash@{0} # 删除指定 stash
11. 标签(Tag)与 cherry-pick
| 命令 | 作用 | 示例 |
|---|
| git tag v1.0 | 打轻量标签 | git tag v1.0 |
| git tag -a v1.0 -m "描述" | 打带注解标签 | git tag -a v1.0 -m "发布1.0" |
| git push origin v1.0 | 推送单个标签 | git push origin v1.0 |
| git push origin --tags | 推送所有标签 | git push origin --tags |
| git cherry-pick <commit> | 把某个 commit 应用到当前分支 | git cherry-pick abc123 |
12. 其他实用命令
- git bisect:二分查找引入 bug 的 commit(调试神器)
- git submodule:管理子模块
- git worktree:多工作目录(同时开多个分支)
- git archive:打包仓库
日常最常用 6 个命令(记住这 6 个就够 80% 场景)
- git status
- git add .
- git commit -m "xxx"
- git pull
- git push
- git checkout -b new-branch 或 git switch -c new-branch