跪拜 Guibai
← All articles
Frontend · JavaScript · Git

The 10 Git Pitfalls Frontend Devs Google Weekly — With a Quick-Reference Cheatsheet

By kyriewen ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Every developer has a handful of Git commands they look up repeatedly because the operations are too infrequent to commit to memory. A single-page cheatsheet organized by the exact moment of panic — wrong branch, bad commit message, hard reset — saves the context-switch to a search engine.

Summary

A developer compiled the ten Git operations that send them to a search engine every week. Each pitfall comes with the exact command to fix it: amending a commit message before pushing, stashing work when you realize you're on the wrong branch, aborting a merge mid-conflict, and recovering code after an accidental `git reset --hard` using `reflog`.

The cheatsheet also covers removing accidentally-added files from staging, the three modes of `git reset` and when to use each, cherry-picking a single commit across branches, and cleaning up local branches that were already merged. A final table distills all ten scenarios into a one-glance reference.

Shell aliases like `gs` for `git status` and `gst`/`gsp` for stash operations cut down on typing. The underlying point is that Git isn't hard — it's just that some commands are needed so rarely that memorization never sticks.

Takeaways
Amend the most recent commit message with `git commit --amend -m "..."` only if it hasn't been pushed; force-pushing an amended commit rewrites shared history.
Stash uncommitted changes with `git stash`, switch branches, then `git stash pop` to move work to the correct branch.
Abort a merge in progress with `git merge --abort`; discard resolved-but-uncommitted conflicts with `git checkout --conflict=merge .`.
Remove a mistakenly added file from staging with `git reset HEAD <file>`; if already committed, use `git rm --cached <file>` and update `.gitignore`.
`git reset --soft HEAD~1` undoes the last commit but keeps changes staged; `--mixed` keeps them unstaged; `--hard` discards them entirely.
Recover code lost to `git reset --hard` by finding the commit hash in `git reflog` and resetting to it — reflog retains history for roughly 30 days.
View a file's past version with `git show <hash>:<path>`; compare versions with `git diff HEAD~1 -- <path>` or across branches with `git diff main..feature -- <path>`.
Apply a single commit from another branch without merging the whole branch using `git cherry-pick <hash>`.
List branches already merged into main with `git branch --merged main`; delete them in bulk by piping to `xargs git branch -d`.
When `git push` is rejected because the remote has advanced, either `git pull` (creates a merge commit) or `git pull --rebase` (keeps history linear).
Conclusions

The cheatsheet format acknowledges a truth about developer tooling: infrequent operations don't stick, so reference material should be organized by the exact moment of panic, not by command taxonomy.

`git reflog` is under-taught relative to how often `--hard` mistakes happen. Knowing that Git keeps a 30-day undo log changes the risk calculation of destructive commands.

The `--soft` / `--mixed` / `--hard` distinction is one of the most-Googled concepts in Git because the flags describe internal state rather than user intent. Reframing them by use case — 'reorganize commits' vs. 're-select files' — makes them easier to recall.

Concepts & terms
git reflog
A local log that records every movement of the HEAD pointer, including commits, resets, and checkouts. It acts as a safety net for recovering 'lost' commits for roughly 30 days, until garbage collection runs.
git cherry-pick
Applies the changes introduced by a specific existing commit to the current branch, creating a new commit with a different hash. Useful for selectively moving a bugfix to a release branch without merging unrelated work.
git stash
Temporarily shelves uncommitted changes, reverting the working directory to match the HEAD commit. Changes can be reapplied later with `git stash pop`, making it useful for switching contexts mid-work.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗