我早该知道的 Git 命令
Source: Dev.to
我希望更早知道的 Git 命令
前言
当我刚开始使用 Git 时,我只会几个最基本的命令:git add、git commit、git push。随着项目变得更复杂,我发现自己经常在寻找更高效的工作流。下面列出的是我在日常开发中最常用、且在早期就想掌握的 Git 命令和技巧。
常用命令速查表
| 命令 | 作用 |
|---|---|
git status | 查看工作区和暂存区的当前状态。 |
git add . | 将所有修改(包括新文件、删除和修改)加入暂存区。 |
git commit -m "提交信息" | 将暂存区的更改提交到本地仓库。 |
git log --oneline --graph --decorate | 以简洁、图形化的方式展示提交历史。 |
git branch -a | 列出本地和远程的所有分支。 |
git checkout -b new-branch | 创建并切换到名为 new-branch 的新分支。 |
git reset --hard HEAD~1 | 丢弃最近一次提交(包括工作区更改),回到上一个提交。 |
git reflog | 查看 HEAD、分支等引用的移动记录,常用于找回误删的提交。 |
git stash | 暂时保存当前工作进度,以便切换分支或拉取最新代码。 |
git stash pop | 恢复最近一次 stash 的内容并从 stash 列表中移除。 |
git cherry-pick <commit> | 将指定的单个提交应用到当前分支。 |
git bisect startgit bisect badgit bisect good <good-commit>git bisect run <script> | 使用二分查找定位引入 bug 的提交。 |
git remote -v | 查看已配置的远程仓库地址。 |
git pull --rebase | 拉取远程更新并在本地提交之上进行 rebase,保持线性历史。 |
git fetch --prune | 拉取远程更新并删除已不存在的远程分支引用。 |
git clean -fd | 删除工作区中未被跟踪的文件和目录(慎用)。 |
git diff | 查看工作区与暂存区、或两个提交之间的差异。 |
git show <commit> | 显示指定提交的详细信息(包括改动内容)。 |
git tag -a v1.0 -m "Release 1.0" | 为当前提交打上带注释的标签。 |
git rebase -i HEAD~3 | 交互式 rebase 最近 3 次提交,用于合并、编辑或重新排序提交。 |
git config --global alias.st status | 为常用命令创建别名(此例中 git st 等同于 git status)。 |
实用技巧
1. 快速查看最近的提交图谱
git log --oneline --graph --decorate --all
这条命令可以让你在终端里一眼看到所有分支的合并情况,尤其在处理复杂的 PR 时非常有帮助。
2. 用 git stash 暂时切换任务
当你在一个功能分支上工作,却需要紧急切到 master 修 bug 时,git stash 能把当前未提交的改动保存起来,等回到原分支后再 git stash pop 恢复。
3. 利用 reflog 恢复误删的分支
即使你误用了 git branch -D <branch>,只要在 git reflog 中找到该分支最近的 HEAD,使用 git checkout -b <branch> <sha> 即可找回。
4. 使用别名提升效率
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
以后只需要敲 git co、git br 等简短命令即可。
5. 交互式 rebase 清理提交历史
在准备合并 PR 前,使用 git rebase -i 合并无意义的 “fix typo” 或 “add whitespace” 提交,使提交历史更干净、易读。
小结
掌握这些命令后,你会发现 Git 不再是“只能提交、推送、拉取”的工具,而是一个强大的版本控制系统,能够帮助你在任何情况下保持代码库的整洁与可追溯性。希望这些技巧能让你的工作流更顺畅,也祝你在 Git 的世界里玩得开心!
Source: …
暂存更改
当你深入开发某个功能且需要一个干净的工作目录来进行紧急审查时:
# Save all your changes to the stash
git stash
# Do your other work...
# Bring your changes back
git stash pop
实用的 stash 变体
# Stash with a descriptive message
git stash save "halfway through login refactor"
# View all stashes
git stash list
# Apply a specific stash without removing it from the list
git stash apply stash@{2}
# Stash only specific files
git stash push -m "navbar changes" src/components/Navbar.jsx
# Stash including untracked files
git stash -u
二分查找定位错误
当一周前通过的测试突然失败时,你可以使用二分查找定位导致问题的提交:
# Start the bisect session
git bisect start
# Mark the current (broken) commit as bad
git bisect bad
# Mark a known good commit (e.g., from last week)
git bisect good a1b2c3d
# Git checks out the middle commit; test it, then tell Git:
git bisect good # if this commit works
git bisect bad # if this commit is broken
# Repeat until Git finds the exact bad commit
git bisect reset
如果你有自动化测试脚本:
git bisect start HEAD a1b2c3d
git bisect run npm test
Git 将自动找到出错的提交。
交互式变基
当一个分支包含一系列小的或拼写错误修复的提交,而你想在合并前对其进行清理时:
# Rebase the last 5 commits interactively
git rebase -i HEAD~5
编辑器会打开并显示类似以下的列表:
pick a1b2c3d Add user authentication
pick b2c3d4e Fix typo in auth
pick c3d4e5f Fix typo again
pick d4e5f6g Add logout feature
pick e5f6g7h Forgot semicolon
你可以修改这些命令:
pick a1b2c3d Add user authentication
squash b2c3d4e Fix typo in auth
squash c3d4e5f Fix typo again
pick d4e5f6g Add logout feature
fixup e5f6g7h Forgot semicolon
命令含义
squash (s): 将提交合并到前一个提交,并合并提交信息。fixup (f): 将提交合并到前一个提交,丢弃该提交的信息。reword (r): 保留提交,但编辑其信息。drop (d): 完全删除该提交。edit (e): 暂停以修改该提交。
⚠️ 警告: 切勿对已经推送到共享分支的提交进行变基。
实用 Git 别名
将以下内容添加到 ~/.gitconfig,即可加快日常操作:
[alias]
# Shorter status
s = status -sb
# Pretty log graph
lg = log --oneline --graph --decorate --all
# Undo the last commit but keep changes
undo = reset --soft HEAD~1
# Amend without editing message
oops = commit --amend --no-edit
# Show what you did today
today = log --since='midnight' --author='Your Name' --oneline
# Delete all merged branches (except main/master)
cleanup = "!git branch --merged | grep -v '\\*\\|main\\|master' | xargs -n 1 git branch -d"
# Quick commit with message
cm = commit -m
# Create and switch to a new branch
cob = checkout -b
现在 git s 替代 git status --short --branch。
修订提交
忘记添加文件或需要修改提交信息?
# Add forgotten file to the last commit
git add forgotten-file.js
git commit --amend --no-edit
# Or just fix the message
git commit --amend -m "Better commit message"
恢复丢失的工作
如果你不小心删除了分支或执行了过度的硬重置,reflog 会记录所有操作:
git reflog
典型输出:
a1b2c3d HEAD@{0}: reset: moving to HEAD~3
b2c3d4e HEAD@{1}: commit: Add feature
...
恢复到之前的状态:
git checkout b2c3d4e
# or
git reset --hard b2c3d4e
挑选特定提交
git cherry-pick a1b2c3d
查找提交
# Search commit messages
git log --grep="bug fix"
# Search code changes for a specific string
git log -S "functionName"
# Show commits that touched a specific file (including renames)
git log --follow -p -- path/to/file.js
清理未跟踪的文件
先进行干运行:
git clean -n
删除未跟踪的文件:
git clean -f
删除未跟踪的文件和目录:
git clean -fd
快速参考表
| Command | 功能描述 |
|---|---|
git stash | 暂时保存更改 |
git stash pop | 恢复已暂存的更改 |
git bisect | 二分查找错误 |
git rebase -i HEAD~n | 交互式变基最近的 n 次提交 |
git commit --amend | 修改最近一次提交 |
git reflog | 查看所有 HEAD 更改的历史 |
git cherry-pick | 复制特定提交 |
git log -S "text" | 查找修改特定文本的提交 |
git clean -fd | 删除未跟踪的文件和目录 |
Git 学习曲线陡峭,但掌握这些命令彻底改变了我的工作流。我曾经害怕 Git,如今(大多数情况下)真的开始享受版本控制。挑选列表中的一个命令,在本周开始使用它;等它成为肌肉记忆后,再添加另一个。祝编码愉快! 🚀