keepcalculator

06 Numbers and Powers

Git Features That Feel Like Cheating

Card No. 06·83920 · Updated · Standard formula, shown below

Git has several built-in features that let you rewrite history, recover lost work, and manipulate commits in ways that feel like you’re breaking the rules. This article explains those features, how they actually work, and when to use them safely.

What Makes Git Feel Like Cheating

Git’s power comes from its object model: every commit, tree, and blob is stored as a content-addressed object with a SHA-1 hash. Because history is just a directed graph of immutable objects, Git can create new branches, rewrite commits, and even delete history without affecting other users—until you push. This design enables features that would be impossible in most version control systems, and it’s why commands like rebase, cherry-pick, and reflog feel almost supernatural.

Interactive Rebase: Rewriting the Past

Interactive rebase (git rebase -i) lets you edit, reorder, squash, and drop commits before they’re shared. It works by replaying your commits onto a new base, one at a time, allowing you to change the message, combine multiple commits into one, or split a commit into several. For example, to clean up three messy commits before pushing:

  1. Run git rebase -i HEAD~3.
  2. In the editor, change pick to squash for the commits you want to merge.
  3. Save and write a new commit message.

This rewrites the commit hashes, so only use it on commits that haven’t been pushed to a shared branch. If you’ve already pushed, you’ll need to force-push, which can disrupt collaborators.

Cherry-Pick: Transplanting Commits

Cherry-picking copies a commit from one branch to another without merging the entire branch. It applies the diff of the chosen commit onto your current HEAD. This is handy when a hotfix was committed to main but you need it in a release branch. The command is simple:

git cherry-pick <commit-hash>

If conflicts arise, resolve them and commit. Cherry-picking creates a new commit with a different hash, so it’s safe for shared branches. It feels like cheating because you’re grabbing exactly what you want without the baggage of a merge.

Reflog: Undo Almost Anything

The reflog is Git’s safety net. It records every movement of your HEAD and branch pointers, including resets, rebases, and even deleted branches. If you accidentally run git reset --hard and lose work, you can recover it with:

git reflog

This lists recent actions with their commit hashes. Find the one before your mistake and run git reset --hard <hash> to restore. The reflog is local to your repository and expires after 90 days by default, but it’s the closest thing to a time machine Git offers.

Stash: Save Work Without a Commit

Stashing temporarily shelves changes so you can switch branches or pull updates without committing incomplete work. git stash saves your working directory and index, then cleans them. Later, git stash pop reapplies the changes. You can even stash untracked files with -u and name stashes for clarity. This feels like cheating because you can vanish your changes and bring them back later, as if they were never touched.

Bisect: Find the Culprit Commit

Bisect uses binary search to find the exact commit that introduced a bug. You start by marking a known-good commit and a known-bad commit, then Git checks out the midpoint. You test it, mark it good or bad, and repeat. After about log2(n) steps, you’ve isolated the culprit. For a 1000-commit history, that’s only 10 tests. This is a massive time-saver and feels like cheating because it automates what would otherwise be a tedious manual search.

Worktree: Multiple Branches at Once

Worktrees let you check out multiple branches simultaneously in separate directories, all sharing the same repository. You can work on a feature while reviewing another branch without stashing or cloning. Create one with:

git worktree add ../feature-branch feature

Each worktree has its own working directory and index, but they share the same object store. This is perfect for switching context instantly, and it feels like cheating because you’re not limited to one checkout at a time.

Frequently Asked Questions

Is it safe to force-push after a rebase?

Force-pushing rewrites history on the remote, which can break other people’s clones. It’s safe only if you’re the sole user of the branch or you’ve coordinated with your team. Use --force-with-lease to ensure no one else has pushed in the meantime.

Can I undo a push?

You can’t truly undo a push, but you can reset locally and force-push the corrected history. This rewrites the remote, so it’s disruptive. A safer alternative is to revert the commit with git revert, which creates a new commit that undoes the changes without altering history.

Why does rebase feel like cheating?

Rebase rewrites commit hashes, making it look like your branch was always based on the latest main. It creates a linear history that’s easier to read, but it’s a form of history rewriting. As long as you only rebase local, unpushed commits, it’s perfectly safe.

What if I lose a commit that’s not in the reflog?

If a commit isn’t referenced by any branch or tag and its reflog entry has expired, it can be garbage-collected. However, you can often find it with git fsck --lost-found, which lists dangling commits. Act quickly before the next gc.

Does cherry-pick cause merge conflicts?

Yes, if the same lines were changed in both branches. You resolve them the same way as a merge. Cherry-pick is not a magic bullet, but it’s a targeted way to apply specific changes.

Can I use these features on shared branches?

Only cherry-pick and stash are safe on shared branches. Rebase, reset, and amend rewrite history, so they should be reserved for local, unpublished work. Always communicate with your team before rewriting anything that has been pushed.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →