Git Tricks Worth Actually Remembering

The handful of Git commands beyond add/commit/push that save real time — undoing safely, finding the commit that broke things, and rescuing work you thought was gone.

· 2 min read · beginner

Everyone knows add, commit, push. These are the next ten that pay for themselves.

Undo the last commit but keep the changes

Committed too early? Move the commit off but leave your edits staged:

bash
git reset --soft HEAD~1

Use --mixed (the default) to also unstage, or --hard to throw the changes away entirely — the only one of the three that loses work.

Fix the last commit message (or add a forgotten file)

bash
git commit --amend                 # edit the message
git add forgotten.txt && git commit --amend --no-edit   # sneak a file in, keep the message

Only amend commits you haven't pushed to a shared branch.

Stash with a name, and get it back

bash
git stash push -m "wip: half-done refactor"
git stash list
git stash pop            # apply newest and drop it
git stash apply stash@{1}   # apply a specific one, keep it in the list

Find the commit that broke something — bisect

Binary-search your history. Mark a known-good and known-bad commit and Git walks you to the culprit:

bash
git bisect start
git bisect bad                 # current commit is broken
git bisect good v1.4.0         # this old tag worked
# Git checks out the midpoint; test it, then:
git bisect good   # or: git bisect bad
# repeat until Git names the first bad commit
git bisect reset

Ten steps bisects a thousand commits.

See who last touched a line — blame

bash
git blame -L 40,60 app.c    # authorship for lines 40–60

Pair it with git log -p -S "someString" to find when a string was introduced or removed ("pickaxe" search).

The safety net: reflog

git reflog records where HEAD has been, even across resets and rebases. It is how you recover "lost" commits:

bash
git reflog
# find the hash you want back, then:
git reset --hard <hash>     # or: git checkout -b rescue <hash>

Clean up a messy branch before a PR

bash
git rebase -i HEAD~5    # squash, reword, reorder the last 5 commits

Interactive rebase lets you present a clean story even if your actual work was chaotic. (Rebase only your own un-shared commits.)

Quick quality-of-life

bash
git switch -c feature/x      # create+switch (clearer than checkout -b)
git restore file.txt         # discard unstaged changes to a file
git log --oneline --graph --all   # readable history at a glance
git diff --staged            # review exactly what you're about to commit

Takeaways

  • reset --soft and commit --amend fix fresh mistakes without losing work.
  • bisect finds regressions fast; blame + pickaxe find when/why a line changed.
  • reflog is the undo-for-your-undo — learn it before you need it.