btay.io/wiki
References

Git Commands

The Git commands I actually use, organized by what I'm trying to do.

Updated 18 min ago

Status & inspection

git status -sb                  # short status with branch
git log --oneline --graph -20   # last 20 commits as a graph
git diff --stat                 # which files changed, how much
git diff --cached               # what's staged
git show HEAD --stat            # what was in the last commit

Working with branches

git switch -c feature/foo       # create + switch
git switch -                    # toggle to previous branch
git branch -vv                  # branches with tracking info
git branch --merged main        # branches safe to delete

Fixing mistakes

git restore --staged <path>     # unstage without losing changes
git restore <path>              # discard unstaged changes (CAREFUL)
git commit --amend              # edit the last commit
git reset HEAD~1                # undo last commit, keep changes
git reset --hard origin/main    # nuke local changes (DESTRUCTIVE)

Rebasing

git pull --rebase               # rebase instead of merge on pull
git rebase main                 # replay current branch onto main
git rebase --continue           # after resolving conflicts
git rebase --abort              # back out of a rebase mid-way

Stashing

git stash push -m "wip: ..."    # stash with a message
git stash list                  # see all stashes
git stash pop                   # apply + drop the top stash
git stash apply stash@{2}       # apply a specific stash

Remote & sync

git fetch --all --prune         # update remotes, prune deleted
git push -u origin HEAD         # push current branch + track
git push --force-with-lease     # safer than --force

One-offs I always forget

git blame -L 10,30 path         # blame a line range
git log -p path                 # commits + diffs for a file
git log -S "needle" -- path     # commits that added/removed text
git reflog                      # local history of HEAD moves (lifesaver)

On this page