Useful git commands that I always forget
November 29, 2023
Here are a couple of git commands I always seem to forget. I’m writing them down, so I can stop googling them every time (doubt). You probably have these commit-ted to memory, but my brain refuses to stash ‘em.
Basic but essential
Pull changes and rebase my commits on top
No merge commits needed - keeps history looking tidy.
git pull --rebase origin mainInteractive rebase for the last 2 commits
git rebase -i HEAD~2Undo the last commit but keep the changes
git reset --soft HEAD~1Quickly stash/un-stash changes without a message
I have to jump branches a lot at work.
git stash
git stash popSee what I’m about to commit
Just gotta be sure…
git diff --stagedCreate a new branch
There’s other newer ways to achieve this, but this is the one command here that actually lodged itself into my brain.
git checkout -b [branch_name]For when you’re feeling ~fancy~
Reset to a better state
Sometimes you need to blow away your changes.
git reset --hard HEADAnd sometimes you messed up your local branch and want to get it back to the state it’s at on the remote.
git reset --hard origin/[branch_name]Clean up references locally that are gone from remote
git fetch --prune [remote]git revert [commit-hash]
Undo the changes in a commit
Create a new commit, reverting changes from the specified commit. It generates an inversion of changes.
git revert [commit-hash]Grab a specific commit from another branch
git cherry-pick [commit-hash]Pray you don’t have to use these
Show a log of all reference changes
Lifesaver when you’ve really messed up and need to recover a lost commit or branch. Is it ref log or re-flog? 🤔
git reflogStart a binary search
Finds which commit introduced a bug.
git bisect start