Beyond Add, Commit, Push - The Git Commands That Make You Dangerous (in a Good Way)

Beyond Add, Commit, Push - The Git Commands That Make You Dangerous (in a Good Way)
Beyond Add, Commit, Push - The Git Commands That Make You Dangerous (in a Good Way)

If you can clone a repository, stage some files, commit, and push, you already know enough Git to get through the day. Then something awkward happens: a rebase goes sideways, a commit lands on the wrong branch, a bug appears somewhere in the last 200 commits, or a colleague asks you to review a pull request while your own work is half-finished.

That is when the basics stop being enough. You do not need all of Git’s 150-odd commands. You need to know which tool matches which mess.

Here are four situations most developers hit, and the commands that get you out. The examples assume modern Git (2.23+ for switch and restore). Run git --version if you are not sure what you have.


Checkout stopped meaning one thing

For years, git checkout did two jobs: move between branches and overwrite files. Same verb, opposite intent. That overload is why so many people froze before typing it.

Git 2.23 split those responsibilities. git switch moves you around. git restore puts files back.

git switch main
git switch -c feature/login
git switch -                    # previous branch

git switch - is the one that sticks. You flip to main to check something, then bounce straight back without remembering the branch name.

git restore app.py              # discard unstaged edits
git restore --staged app.py     # unstage, keep the edits

One catch: git restore <file> without --staged discards uncommitted changes permanently. They were never committed, so the reflog cannot bring them back. If the change mattered, commit or stash first.


Your pull request history is a diary, not a story

Your feature branch has been open for a week. main moved. You could merge main into the feature branch and keep working. The downside shows up in the history: a trail of “Merge branch ‘main’” commits that say nothing useful to a reviewer.

Rebasing lifts your commits off the old base and replays them on top of the latest main:

git switch feature/login
git fetch origin
git rebase origin/main

If there is a conflict, Git pauses. Fix the files, stage them, then git rebase --continue. Or git rebase --abort if you want out.

Interactive rebase is where this gets useful for day-to-day work. Before you open a pull request, your branch often looks like real work: a solid first commit, then “wip”, then “fix typo”, then “actually fix it.” Reviewers should not have to reconstruct that timeline.

git rebase -i origin/main

Your editor opens with a to-do list:

git-rebase-interactive

A common tidy: keep the real feature commits, fold the noise into them:

pick f82761d Add login form
fixup 918b876 WIP
fixup dd191d4 Fix typo in login form
pick ca4b237 Add password validation

Save and quit. Git replays that into two commits: “Add login form” (with the WIP and typo folded in) and “Add password validation.” The other verbs (reword, squash, edit, drop) are there when you need them. fixup is the one you will use most.

When you notice a mistake in an earlier private commit, record a fixup instead of inventing a “fix the earlier thing” commit for the pull request:

git commit --fixup=f82761d
git rebase -i --autosquash origin/main

Only rebase commits that have not been shared, or that only you work on. Rebasing rewrites commit IDs. When you push a rebased branch, use git push --force-with-lease, never plain --force. It refuses to overwrite the remote if someone pushed in the meantime.


Slack lights up and your diff is half-finished

You are halfway through a login form. The diff is ugly. The tests are red. Then production is broken and someone needs you now.

You could commit the half-finished work with a “wip” message, but then that commit sits on the branch, or you have to remember to undo it later. Stash parks the dirty working tree so you can switch context and come back when the fire is out:

git stash push -m "login form half done"

By default that covers tracked files. Add -u if you also created new files Git has never seen. Always use -m. A week later you will not remember what stash@{3} was.

git-stash

pop re-applies the latest stash and removes it. apply re-applies and keeps it, which is safer when you are not sure the apply will be clean.

Stash is for a short pause. When you need two branches checked out at once (keep building the feature while you review a pull request, or hotfix without dismantling your WIP), people often clone the repo a second time. That works. It also doubles disk use and drifts when you forget which clone you were in.

git worktree gives you a second working directory that shares the same repository data:

git worktree add ../myapp-hotfix hotfix/payment
git worktree list
git-worktree

One .git, two folders, no second clone to keep in sync. When you are done: git worktree remove ../myapp-hotfix. The same branch cannot be checked out in two worktrees at once.


You undid too much

Sooner or later you will commit something you regret: the wrong files, a bad message, a change that should never have left your laptop. Git gives you two undo tools, and mixing them up is how local cleanup turns into a team incident.

reset rewrites history. It moves your branch pointer backwards. Use it when the commit has not been pushed:

git reset --soft HEAD~1    # undo the commit, keep changes staged
git reset --mixed HEAD~1   # undo the commit, keep changes unstaged (default)
git reset --hard HEAD~1    # undo the commit and delete the changes

--soft is for “I want to redo that last commit.” --hard is the one that ruins afternoons. Use it only when you are sure those edits should not survive.

revert creates a new commit that applies the inverse of an old one. Use it on shared branches. History stays intact for everyone who already pulled:

git revert 4f9a2c1
git revert --no-edit HEAD

Not pushed yet? Reset. Already on a shared branch? Revert.

And if you already ran --hard by mistake: committed work is very hard to lose. Every time HEAD moves (commit, rebase, reset, switch), Git records it in the reflog.

git reflog
git-reflog

HEAD@{0} is the mistake. HEAD@{1} is where you want to be again:

git reset --hard HEAD@{1}

Or put a branch label on the commit first so you have a named rescue point: git branch rescue ca4b237. The same trick recovers a botched rebase. Look for the entry just before rebase (start).

Reflog entries expire by default after 90 days for reachable ones and 30 for unreachable ones. They are local to your machine. Reach for the reflog before you panic, and before you re-clone “to start clean.”


Something broke two weeks ago

Login worked on the release tag. It fails on main today. Between those two points sit hundreds of commits, and checking them by hand is tedious work a computer can do faster.

git bisect automates a binary search. You mark a bad commit (usually HEAD) and a good one (a release tag or an older SHA). Git checks out the midpoint. You test. You tell it good or bad. It halves the range again. Across 1,000 commits you need about 10 steps.

git bisect start
git bisect bad
git bisect good v2.3.0
# test the checkout, then:
git bisect good                # or: git bisect bad
git bisect reset
git-bisect

If you already have a test that exits non-zero on failure, hand the loop to Git:

git bisect start HEAD v2.3.0
git bisect run pytest tests/test_login.py

You get a specific commit to inspect, instead of a vague “something in last week’s deploys.” From there, git show and a focused diff tell you what actually changed.

When the question is about history rather than a live regression, use the right dots:

# Use when: "what commits are on my branch that main does not have?"
git log --oneline main..feature

# Use when: "what file changes would this pull request show?"
git diff main...feature

Two dots in log mean commits reachable from feature but not main. Three dots in diff compare feature against the common ancestor. That three-dot view is what a pull request shows you.


Where to go from here

  • Reach for git reflog before you panic.
  • Rebase privately, revert publicly, and always use --force-with-lease.
  • Stash for a short pause; worktree when you need two checkouts at once.
  • Let bisect find the first bad commit; use main..feature and main...feature on purpose.

If you want the rest of the map (branching and merging, rewriting history safely, recovery, inspecting history, remotes, tags, blame, aliases, and the everyday flags), my Git Commands booklet collects them in one place:

Branching, rebasing, recovery, debugging, and the power tools that make you faster and safer. Verified against Git 2.47.

If the awkward situation is a secret that landed in history, not a messy branch, see You Deleted the Secret - It’s Still in Git History.


Follow me on Twitter: https://twitter.com/DevAsService

Follow me on Instagram: https://www.instagram.com/devasservice/

Follow me on TikTok: https://www.tiktok.com/@devasservice

Follow me on YouTube: https://www.youtube.com/@DevAsService

Nuno Bispo

Nuno Bispo

Solutions Architect · Senior Python & AI Engineer · AI Audits · Helping teams fix what they shipped too fast
Netherlands