Git Advanced Commands

Go beyond commit and push. Master the tools that help you rewrite history, find bugs effortlessly, and save your work when things go wrong.

Interactice Rebase

Clean up your local commit history before merging. Combine, reorder, or drop commits.

git rebase -i HEAD~3
# Opens an editor where you can pick, squash, or fixup commits.

Git Bisect

Use binary search to find the commit that introduced a bug. Git automatically checks out commits for you to test.

git bisect start
git bisect bad            # Current version is broken
git bisect good v1.0.0    # Version 1.0.0 was working
# Git will checkout a middle commit
# Test the app...
git bisect good           # If it works
# ... repeat until found
git bisect reset

Reflog

A safety net. It records updates to the tip of branches. If you accidentally reset --hard and lost commits, reflog can find them.

git reflog
# Identify the HEAD@{n} you want to go back to
git reset --hard HEAD@{n}

Cherry Pick

Apply the changes introduced by some existing commits.

git cherry-pick <commit-hash>

Stash

Temporarily shelve (or stash) changes you've made to your working copy.

git stash
git stash pop   # Apply and remove from list
Knowledge is power.