Git Commands That Feel Like Magic
In this article
Some Git commands feel like magic because they solve a problem you didn't know had a solution, or they undo a mistake that seemed permanent. This article explains the most useful of these commands—what they do, when to use them, and how they work under the hood—so you can use them with confidence.
Why Git Feels Magical (and Why It Isn't)
Git's reputation for being confusing comes from its object model. Every commit, branch, and tag is just a snapshot of your files plus metadata, stored as a directed acyclic graph. When you run a command like git rebase, you're not destroying history; you're creating new commits that replace old ones. The old commits remain in the object database until garbage collection removes them. This is why many "magic" commands work: they're just manipulating the graph in ways that are reversible.
Understanding this model demystifies the commands below. They aren't magic; they're precise tools that operate on a well-defined structure. Once you see that, you can predict what a command will do before you run it.
The Reflog: Your Safety Net
git reflog is the closest thing Git has to an undo button. It records every movement of your HEAD pointer—every commit, checkout, reset, merge, and rebase—on your local repository. If you ever run a command that seems to lose work (like a hard reset or a botched rebase), the reflog shows you the commit hashes of where you were before.
To recover a lost commit, run git reflog to find the hash, then git checkout <hash> or git reset --hard <hash>. The reflog is local to your machine, so it only helps if the work existed in your local repo. It doesn't help if you never committed, or if you cloned a fresh repo and lost uncommitted changes.
Interactive Rebase: Rewriting History Cleanly
git rebase -i (interactive rebase) lets you edit, squash, reorder, or drop commits before they're finalized. It's the command behind "clean up your commits before pushing." You specify a range, like git rebase -i HEAD~3 to rewrite the last three commits, and Git opens an editor with a list of actions: pick, squash, fixup, edit, reword, drop.
The magic part is that you can squash a dozen messy commits into one coherent commit, or split a commit that mixed two unrelated changes. The catch: rewriting history changes commit hashes, so you should only do 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. The reflog will save you if you mess up the rebase, so it's safe to experiment.
git stash: Temporarily Shelve Work
git stash saves your uncommitted changes (both tracked and, with -u, untracked files) and reverts your working directory to the last commit. It's perfect when you need to switch branches quickly but aren't ready to commit. To bring the changes back, run git stash pop.
What feels magical is that you can have multiple stashes (git stash list), and even apply a stash without removing it (git stash apply). You can also create a branch from a stash with git stash branch new-branch, which is handy if you realize the work deserves its own branch. One common mistake is stashing untracked files without -u, so they remain in your working directory. Use git stash -u to include them.
git cherry-pick: Copy a Commit Anywhere
git cherry-pick <commit-hash> applies the changes from a specific commit onto your current branch. It's like a merge, but for a single commit. This is invaluable when you have a hotfix on a release branch and want the same fix on main, or when you accidentally committed to the wrong branch.
The command creates a new commit with the same message and changes, but a different hash. If there are conflicts, Git pauses and asks you to resolve them, just like a merge. You can cherry-pick multiple commits by listing their hashes, or a range with git cherry-pick A..B. It doesn't alter the original commit, so it's safe and reversible.
git bisect: Find the Commit That Broke Everything
When a bug appears and you don't know which commit introduced it, git bisect performs a binary search through your history. You start with git bisect start, then mark a known-good commit (git bisect good <hash>) and a known-bad commit (git bisect bad). Git checks out a middle commit, and you test it, marking it good or bad. After a few iterations, Git identifies the exact commit that introduced the bug.
This feels like magic because it turns a potentially hours-long manual search into a handful of tests. For a history of 1000 commits, it takes about 10 checks. You can automate testing with git bisect run <script>, where the script returns 0 for good and non-zero for bad. Just remember to run git bisect reset when you're done to return to your original branch.
git clean: Remove Untracked Files
git clean -fd deletes all untracked files and directories from your working tree. It's the complement to git stash -u—where stash saves them, clean removes them. This is useful when you want a pristine checkout, but it's dangerous because it permanently deletes files that aren't in Git.
Always run git clean -n first to preview what will be removed. The -n flag does a dry run. If you're sure, use git clean -fd (force and include directories). There's no undo for this command, so double-check the preview. If you have files you might need, stash them instead.
Frequently Asked Questions
Can I undo a git reset --hard?
Yes, if you have the reflog. Run git reflog to see the hash of the commit you were on before the reset, then git reset --hard <hash>. The reflog keeps entries for about 90 days by default, so you have time.
What's the difference between git revert and git reset?
git revert creates a new commit that undoes the changes of a previous commit, keeping history intact. git reset moves the branch pointer backward, removing commits from the branch (but they remain in the reflog). Use revert for shared branches, reset for local cleanup.
How do I fix a commit message after I already pushed?
Use git commit --amend to change the last commit's message, then force-push with git push --force-with-lease. Be cautious if others have pulled your branch, as force-pushing rewrites history. The --force-with-lease flag is safer than --force because it checks that the remote hasn't changed.
Why does git rebase sometimes ask me to resolve conflicts multiple times?
Rebase replays each commit one at a time, so the same conflict can appear in multiple commits. This is normal. You resolve it once per commit, and Git continues. If the conflicts are repetitive, consider squashing the commits first or using git rerere (reuse recorded resolution) to remember how you resolved them.
Is git cherry-pick safe to use on a shared branch?
Yes, cherry-pick creates new commits and doesn't modify existing ones, so it's safe. However, if you cherry-pick a commit that already exists on the target branch, you might get a duplicate. Use git cherry-pick -x to add a reference to the original commit in the message, which helps track provenance.