Git Cheatsheet - Commit, Branch, Merge, Rebase & Log
Git essentials organized into Commit, Branch, Revert, Remote, and History groups: fix a bad commit with amend/restore/reset, untangle branches with reflog/switch/rebase, inspect changes with git log. Each command notes when to use it and when to be careful.
Typical Use Case
Daily version control: commit, branch, merge and rebase, revert mistakes, recover deleted branches or staged content, and resolve merge conflicts and remote sync problems.
Commit & Stash 6
git status -sgit add -pgit commit -m "msg"git commit --amendgit stashgit stash popBranch & Merge 13
git branch -agit branch -vvgit switch -c feat/xgit merge --no-ff feat/xgit rebase maingit rebase -i HEAD~3git rebase --abortgit cherry-pick <sha>git push -u origin feat/xgit push origin --delete feat/xgit branch -d feat/xgit branch -D feat/xgit branch --mergedRevert & Undo 5
git restore <file>git restore --staged <file>git reset --soft HEAD~1git reset --hard HEAD~1git revert <sha>Remote & Diagnostics 5
git remote -vgit fetch --prunegit log --oneline --graphgit refloggit blame <file>Commit History (git log) 10
git log -pgit log --statgit log --author="Alice"git log --since="2 weeks ago"git log --grep="fix"git log -S "function_name"git log origin/main..HEADgit log --follow -- file.jsgit log --name-statusgit shortlog -snFAQ 5
Q: How do I undo the last commit?Q: What is the difference between git merge and git rebase?Q: How do I change the last commit message?Q: How do I stash unfinished changes?Q: How do I resolve a merge conflict?Parameter matrix
| 参数 | Effect | Example |
|---|---|---|
--amend | 修改最近一次提交(补文件或改说明) | git commit --amend |
-p | 按片段(hunk)暂存,精细控制提交内容 | git add -p |
--soft/--mixed/--hard | reset 三种模式:保留/丢弃暂存/丢弃工作区 | git reset --soft HEAD~1 |
-i | 交互式变基,可合并/重排/改写提交 | git rebase -i HEAD~3 |
--rebase | 合并时优先变基保持线性历史 | git pull --rebase |
--no-ff | 合并保留合并提交,便于回溯分支 | git merge --no-ff feature |
-u | 推送时关联上游分支 | git push -u origin feature |
--force-with-lease | 安全强推,避免覆盖他人提交 | git push --force-with-lease |
--stat | 显示每次提交的改动统计 | git log --stat |
--graph | 图形化展示分支拓扑 | git log --oneline --graph |
Common pitfalls
Symptomgit reset --hard 后误删的提交/改动找不回来。
Cause--hard 同时丢弃暂存区与工作区,且未保留 reflog。
Fix先用 git reflog 找到丢失提交的 SHA,再 git reset --hard <sha> 恢复;危险操作前先 git stash 或建分支。
Symptompush 被拒:non-fast-forward。
Cause远端有本地没有的提交(他人已推送)。
Fix先 git pull --rebase 把本地变基到远端之上,解决冲突后再 push,避免盲目 --force。
Symptom强制推送覆盖了同事的提交。
Cause直接 git push --force 无视远端新提交。
Fix一律用 git push --force-with-lease,它会在远端有本地未知提交时拒绝推送。
Symptom合并冲突后文件里出现 <<<<<<< 标记。
Cause同一处被两边改动,Git 无法自动合并。
Fix用 git status 看冲突文件,手动编辑去掉标记并保留正确内容,git add 后再 commit。
Symptomgit stash 后内容貌似丢了。
Causestash 后未 pop/apply,或 stash 列表被清理。
Fixgit stash list 查看;git stash apply stash@{n} 恢复;若已 drop,可用 git fsck --lost-found 找回悬空对象。
Symptom提交里混入了不该提交的大文件/密钥。
Cause未在 .gitignore 中排除,或提交后才加忽略。
Fix用 git rm --cached 移除跟踪并用 BFG/filter-repo 重写历史;密钥立即作废并轮换。
Troubleshooting
1找回误删的提交
git reflog列出 HEAD 的所有移动记录,定位丢失提交的 SHA 后用 reset 恢复。
2查看某次提交改了什么
git show <sha>同时看提交元信息与该提交的具体 diff。
3定位某行代码是谁、何时引入
git blame -L 10,20 file.ts输出每行的最近修改提交与作者,便于追责与回溯。
4合并冲突后确认未残留冲突标记
grep -rn "<<<<<<<" .提交前全局搜索冲突标记,避免把 <<<<<<< 提交进仓库。
Command Examples
Recover a commit dropped by reset
git reset --hard HEAD~2\ngit reflog\ngit reset --hard HEAD@{2}reset --hard 后工作区被清空,但只要没跑 gc,reflog 仍记录每次 HEAD 移动,用 HEAD@{n} 就能找回上一位置。
Output
ba9c3e1 (HEAD) fix: drop temp debug log <- reflog 里的历史 HEAD\n8f10a42 feat: add pagination\nHEAD@{2}: reset: moving to HEAD~2Cherry-pick a single commit
git log --oneline -3\ngit cherry-pick 9f2c14b想只拿另一个分支的某个提交,而不合并整个分支时用 cherry-pick;有冲突时按提示解决后 git cherry-pick --continue。
Common Pitfalls
- git push origin --delete and git branch -D are irreversible — verify with git branch -a and git log before deleting.
- Resetting a commit already pushed to a shared branch then force-pushing overwrites teammates' history; use revert on shared commits.
- git clean -fd (not listed here) deletes untracked files unrecoverably — always preview with git clean -n first.
- git stash does not include untracked files by default; use git stash -u to include them.
Tips
- reset --hard discards changes — double-check before running; use git reflog to recover if truly lost.
- Use revert to undo commits already pushed to shared branches — never reset and force-push or you will break your teammates.
- switch/restore are newer commands with clearer semantics than checkout; older scripts still use checkout.
FAQ
When should I use git rebase vs git merge?
merge preserves divergent history and creates a merge commit, so it suits shared branches such as merging a feature into main, is easy to roll back as a unit, and never rewrites existing commits. rebase replays your commits onto the target branch to produce a clean linear history, which is great for tidying your own feature branch, but it rewrites commits and should be avoided on branches others share.
Can I undo an already-pushed commit with git reset and then force-push?
Not recommended. reset rewrites history, and force-pushing it can overwrite teammates commits, causing conflicts or lost work. For pushed commits, safely undo with git revert <sha>, which creates a reverse commit. It is fine to use reset --soft/hard only while the commit is still local and unpushed.
I accidentally committed node_modules or .env. How do I remove them?
Run git rm --cached <path> to stop tracking while keeping the file locally, add the rule to .gitignore, then commit once. This only removes the files from version control without deleting them from disk. If a secret was exposed, also rewrite history and rotate the credential.
Can I recover a deleted branch or a commit dropped by reset?
Yes. Unless gc has run, git reflog records every prior position of HEAD and branches; restore with git reset --hard HEAD@{n} or git checkout -b <name> <sha>. If a deleted branch still has its commits, find the sha with git reflog and recreate the branch.
My git stash changes suddenly disappeared?
First check git stash list, and inspect contents with git stash show -p stash@{0}. A pop may leave conflicts that appear as conflict markers you must resolve manually. If it is really gone, use git fsck --lost-found to recover the unreferenced stash commit from the object database. Note git stash does not include untracked files unless you use git stash -u.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Sep 10, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us