Undo the last commit, keep the changes
Removes the last commit but leaves its changes staged, ready to edit and commit again.
git reset --soft HEAD~1If the commit was already pushed, use git revert instead so you don’t rewrite shared history.
The Git commands people actually search for: undoing commits, fixing branches, stashing, rewriting messages and recovering lost work. Fill in the blanks and copy.
37 commands
Removes the last commit but leaves its changes staged, ready to edit and commit again.
git reset --soft HEAD~1If the commit was already pushed, use git revert instead so you don’t rewrite shared history.
Creates a new commit that reverses an earlier one. Safe on shared branches because history isn’t rewritten.
git revert Replaces the message of the most recent commit.
git commit --amend -m ""Rewrites the commit. If it was already pushed you’ll need git push --force-with-lease.
Stages the file and folds it into the previous commit without changing its message.
git add && git commit --amend --no-editRewrites the last commit; avoid on commits others have pulled.
A compact view of changed files plus how far ahead or behind your branch is.
git status -sbStages new, changed and deleted files, then commits them in one go.
git add -A && git commit -m ""Creates a branch from where you are and moves onto it.
git switch -c Moves to an existing branch.
git switch Publishes the current branch with the same name and sets it as the upstream, so plain git push works next time.
git push -u origin HEADFetches remote changes and replays your local commits on top, keeping history linear.
git pull --rebaseThrows away uncommitted edits to a file and restores the last committed version.
git restore Uncommitted edits to that file are lost.
Removes a file from the staging area but keeps your edits.
git restore --staged Throws away all local commits and changes on this branch and copies the remote version.
git fetch origin && git reset --hard origin/Deletes uncommitted work and unpushed commits. Run git stash first if you might need them.
Shelves your uncommitted changes so you can switch branches with a clean tree.
git stash push -m ""Re-applies the most recent stash and removes it from the stash list.
git stash popShows every stash with its message.
git stash listA compact visual graph of all branches and commits.
git log --oneline --graph --decorate --allShows the exact changes that are staged.
git diff --stagedAnnotates every line with the commit and author that last changed it.
git blame Lists commits that added or removed the given text. Great for "when did this disappear?"
git log -S "" --onelineApplies the changes from a single commit on another branch to your current branch.
git cherry-pick Shows everywhere HEAD has been, including commits removed by reset or rebase. Use the hash with git reset or git switch -c to get them back.
git reflogLists the untracked files and folders that git clean -fd would delete. Nothing is removed.
git clean -ndRemoves every untracked file and directory from the working tree.
git clean -fdPermanently deletes untracked files (they’re not in Git, so there’s no undo). Run git clean -nd first to preview.
Deletes a branch that has been merged. Use -D to force-delete an unmerged one.
git branch -d Removes the branch from the remote repository.
git push origin --delete Renames the branch you’re on.
git branch -m Cleans up local branches that are already merged into the base branch.
git branch --merged | grep -v '^[ *]*$' | xargs git branch -dOverwrites the remote branch only if nobody else has pushed since you last fetched.
git push --force-with-leaseStill rewrites remote history; only use on branches you own.
Combines your last N commits into a single commit.
git reset --soft HEAD~ && git commit -m ""Rewrites history; avoid on commits that are already shared.
Creates an annotated version tag and publishes it.
git tag -a v -m "Release v" && git push origin vSets the author identity used on every commit on this computer.
git config --global user.name "" && git config --global user.email ""Makes git init create a main branch instead of master.
git config --global init.defaultBranch mainPoints origin at a new address, for example when switching from HTTPS to SSH.
git remote set-url origin Downloads only the most recent commit. Much faster for big repositories.
git clone --depth 1 Ranks contributors by number of commits.
git shortlog -sn --allRemoves the file from Git (for example after adding it to .gitignore) without deleting it from disk.
git rm --cached Every command copies as plain, clean text: no curly quotes, no long dashes, no invisible characters.