Home › Templates › Git commands

🌿 Git commands cheat sheet: undo, branch, stash, rebase and clean up

Most of git is a handful of commands plus the ones you only need when something goes wrong. This pack holds the ones people look up over and over.

A few commands rewrite history or throw work away. Those are labelled, so read them twice before running. Save the pack to CopyCat to have them handy.

New branch from latest main

git switch main && git pull && git switch -c [feature/name]

Undo last commit, keep changes

git reset --soft HEAD~1

Undo last commit, discard changes

git reset --hard HEAD~1

Discard all local changes

git restore . && git clean -fd

Amend the last commit

git commit --amend -m "[new message]"

Stash and restore

git stash push -u -m "[note]" git stash list git stash pop

Safer force push

git push --force-with-lease origin [branch]

Rebase onto main

git fetch origin && git rebase origin/main

Delete merged local branches

git branch --merged main | grep -v "main" | xargs -n 1 git branch -d

Compact log

git log --oneline --graph --decorate -20

Who changed this line

git blame -L [10,20] [path/to/file]

Revert a pushed commit

git revert [commit-hash]

More template packs

Stop retyping these

Add the whole "Git commands" pack to CopyCat in one click and edit anything you like.

Add this pack