Appearance
Git 常用命令速查表
我的常用
shell
git config --global user.name "sys_failure"
git config --global user.email "15789273+sys_failure@user.noreply.gitee.com"
git config --global user.name "idler365"
git config --global user.email "rjx1535189527@gmail.com"1. 基本配置
bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global --list
git config --list推荐设置默认分支:
bash
git config --global init.defaultBranch main设置编辑器:
bash
git config --global core.editor "vim"
git config --global core.editor "code --wait"2. 初始化与克隆
bash
git init
git clone <repo-url>
git clone <repo-url> <directory>查看远程仓库:
bash
git remote -v添加或修改远程仓库:
bash
git remote add origin <repo-url>
git remote set-url origin <repo-url>3. 日常开发流程
最常用的完整流程:
bash
git status
git add .
git commit -m "feat: add login page"
git pull --rebase
git push精细控制暂存内容:
bash
git add <file>
git add <directory>
git add -u
git add -A
git add -p含义:
| 命令 | 作用 |
|---|---|
git add . | 暂存当前目录下的新增、修改、删除 |
git add -u | 暂存已跟踪文件的修改和删除,不包含新文件 |
git add -A | 暂存整个仓库的所有变化 |
git add -p | 逐块选择需要提交的修改 |
提交:
bash
git commit -m "fix: handle empty input"
git commit
git commit -am "fix: update config"注意:git commit -am 只会提交已经被 Git 跟踪的文件,不会包含新建文件。
4. 查看状态与历史
bash
git status
git status -s查看提交记录:
bash
git log
git log --oneline
git log --oneline --graph --decorate --all
git log --stat
git log -p实用别名:
bash
git config --global alias.lg "log --oneline --graph --decorate --all"之后可以直接使用:
bash
git lg查看单次提交:
bash
git show <commit>
git show HEAD
git show HEAD~1查看某个文件的历史:
bash
git log -- <file>
git log -p -- <file>查看每一行最后由谁修改:
bash
git blame <file>5. 查看修改内容
查看工作区中尚未暂存的修改:
bash
git diff查看已经暂存、准备提交的修改:
bash
git diff --staged比较两个提交:
bash
git diff <commit1> <commit2>查看某个文件:
bash
git diff -- <file>
git diff --staged -- <file>6. 分支操作
查看分支:
bash
git branch
git branch -a
git branch -vv创建并切换分支:
bash
git switch -c feature/login切换已有分支:
bash
git switch main
git switch feature/login旧写法:
bash
git checkout -b feature/login
git checkout main删除分支:
bash
git branch -d feature/login
git branch -D feature/login区别:
| 命令 | 作用 |
|---|---|
git branch -d | 安全删除,只允许删除已经合并的分支 |
git branch -D | 强制删除,即使尚未合并 |
重命名当前分支:
bash
git branch -m new-name7. 合并分支
将某分支合并到当前分支:
bash
git switch main
git merge feature/login常见流程:
bash
git switch main
git pull
git merge feature/login
git push发生冲突后:
bash
git status
# 手动编辑冲突文件
git add <resolved-file>
git commit取消尚未完成的合并:
bash
git merge --abort8. Rebase
将当前分支变基到 main:
bash
git switch feature/login
git rebase main发生冲突后:
bash
git status
# 手动解决冲突
git add <resolved-file>
git rebase --continue跳过当前提交:
bash
git rebase --skip取消变基:
bash
git rebase --abort拉取远程更新时,推荐:
bash
git pull --rebase这样可以减少不必要的 merge commit。
merge 与 rebase 的区别
| 操作 | 特点 |
|---|---|
merge | 保留真实分叉历史,安全直观 |
rebase | 历史更线性,但会改写提交 ID |
原则:
不要对已经推送到公共分支、且其他人可能基于它继续开发的提交执行 rebase。
9. 远程仓库操作
查看远程仓库:
bash
git remote -v拉取远程更新,不自动合并:
bash
git fetch拉取并合并:
bash
git pull拉取并变基:
bash
git pull --rebase推送:
bash
git push
git push origin main首次推送并建立跟踪关系:
bash
git push -u origin main推送新分支:
bash
git push -u origin feature/login删除远程分支:
bash
git push origin --delete feature/login查看本地分支对应的远程分支:
bash
git branch -vv10. 撤销修改
这是 Git 中最容易误操作的部分。
10.1 撤销工作区修改
撤销某个文件尚未暂存的修改:
bash
git restore <file>撤销所有尚未暂存的修改:
bash
git restore .旧写法:
bash
git checkout -- <file>10.2 取消暂存
保留文件修改,但将文件移出暂存区:
bash
git restore --staged <file>
git restore --staged .旧写法:
bash
git reset HEAD <file>10.3 修改最近一次提交
修改 commit message:
bash
git commit --amend -m "new message"补充遗漏文件:
bash
git add <file>
git commit --amend --no-edit注意:如果旧提交已经推送,--amend 会改变提交 ID。
10.4 回退提交
仅移动 HEAD,保留修改:
bash
git reset --soft HEAD~1移动 HEAD,并取消暂存,但保留文件修改:
bash
git reset HEAD~1等价于:
bash
git reset --mixed HEAD~1彻底丢弃最近一次提交及修改:
bash
git reset --hard HEAD~1--hard 风险很高,使用前先确认。
10.5 安全撤销已经推送的提交
生成一个新的反向提交:
bash
git revert <commit>撤销最近一次提交:
bash
git revert HEAD适合公共分支,因为不会改写历史。
11. 临时保存修改:stash
当工作未完成,但需要临时切换分支时:
bash
git stash保存并添加说明:
bash
git stash push -m "unfinished login page"查看暂存列表:
bash
git stash list恢复最近一次 stash,但保留 stash 记录:
bash
git stash apply恢复并删除 stash 记录:
bash
git stash pop恢复指定 stash:
bash
git stash apply stash@{1}删除 stash:
bash
git stash drop stash@{0}
git stash clear默认情况下,未跟踪的新文件不会被 stash。需要显式加入:
bash
git stash -u12. 标签:tag
创建轻量标签:
bash
git tag v1.0.0创建带注释标签:
bash
git tag -a v1.0.0 -m "release v1.0.0"查看标签:
bash
git tag
git show v1.0.0推送标签:
bash
git push origin v1.0.0
git push origin --tags删除标签:
bash
git tag -d v1.0.0
git push origin --delete v1.0.013. 选择性复制提交:cherry-pick
将其他分支上的某个提交复制到当前分支:
bash
git cherry-pick <commit>发生冲突:
bash
git add <resolved-file>
git cherry-pick --continue取消:
bash
git cherry-pick --abort典型用途:将某个 bug 修复同步到另一个版本分支。
14. 删除文件
删除文件,并将删除操作加入暂存区:
bash
git rm <file>仅停止跟踪,但保留本地文件:
bash
git rm --cached <file>例如误提交了配置文件:
bash
git rm --cached .env
echo ".env" >> .gitignore
git commit -m "chore: stop tracking .env"15. .gitignore
常见示例:
gitignore
# Python
__pycache__/
*.pyc
.venv/
# Node.js
node_modules/
dist/
# Java
*.class
target/
# C/C++
build/
*.o
*.exe
# IDE
.vscode/
.idea/
# Environment files
.env
# OS
.DS_Store
Thumbs.db已经被 Git 跟踪的文件,不会因为加入 .gitignore 而自动停止跟踪。需要:
bash
git rm --cached <file>16. 清理未跟踪文件
预览将被删除的文件:
bash
git clean -n删除未跟踪文件:
bash
git clean -f同时删除未跟踪目录:
bash
git clean -fd危险操作。建议始终先执行:
bash
git clean -n17. 查找丢失提交:reflog
即使执行了 reset --hard,提交通常仍可通过 reflog 找回:
bash
git reflog恢复到某个历史位置:
bash
git reset --hard <commit>或新建一个恢复分支:
bash
git switch -c recovery <commit>reflog 是 Git 误操作后的重要救援工具。
18. 子模块
添加子模块:
bash
git submodule add <repo-url> <path>克隆包含子模块的仓库:
bash
git clone --recurse-submodules <repo-url>已有仓库初始化子模块:
bash
git submodule update --init --recursive更新子模块:
bash
git submodule update --remote19. 常用工作流
个人项目
bash
git pull --rebase
git status
git add .
git commit -m "feat: implement parser"
git push新功能分支
bash
git switch main
git pull --rebase
git switch -c feature/parser
# 修改代码
git add .
git commit -m "feat: implement parser"
git push -u origin feature/parser合并完成后清理分支
bash
git switch main
git pull --rebase
git branch -d feature/parser
git push origin --delete feature/parser临时修复 bug
bash
git switch main
git pull --rebase
git switch -c fix/null-pointer
# 修改代码
git add .
git commit -m "fix: handle null pointer"
git push -u origin fix/null-pointer最值得记住的 20 条命令
bash
git status
git add .
git add -p
git commit -m "message"
git log --oneline --graph --decorate --all
git diff
git diff --staged
git switch -c <branch>
git switch <branch>
git merge <branch>
git rebase <branch>
git fetch
git pull --rebase
git push
git push -u origin <branch>
git restore <file>
git restore --staged <file>
git stash
git revert <commit>
git reflog撤销操作速查
| 场景 | 推荐命令 |
|---|---|
修改了文件,但还没有 git add | git restore <file> |
已经 git add,想取消暂存 | git restore --staged <file> |
| 最近一次 commit 信息写错 | git commit --amend |
| 本地提交想撤回,但保留文件修改 | git reset --soft HEAD~1 |
| 已推送提交需要撤销 | git revert <commit> |
| 工作做到一半,需要切分支 | git stash |
reset --hard 后想找回提交 | git reflog |