env.dev

Git Cheat Sheet — Undo, Rebase & Resolve Conflicts

Essential Git commands: how to undo the last commit, resolve merge conflicts, rebase branches, squash commits, and one-liners you always forget.

Last updated:

A quick reference for everyday Git commands — including how to undo the last commit, resolve merge conflicts, rebase branches, and squash commits. Organized by workflow so you can find what you need fast, with copy-paste one-liners for the commands you use often but never remember.

Configuration

CommandDescription
git config --global user.name "Name"Set your name
git config --global user.email "email"Set your email
git config --global core.editor vimSet default editor
git config --listShow all settings

Creating Repositories

CommandDescription
git initCreate a new local repo
git clone <url>Clone a remote repo
git clone --depth 1 <url>Shallow clone (latest commit only)

Staging & Committing

CommandDescription
git statusShow working tree status
git add <file>Stage a file
git add -pStage changes interactively (by hunk)
git commit -m "msg"Commit with message
git commit --amendAmend the last commit
git diffShow unstaged changes
git diff --stagedShow staged changes

Branching

CommandDescription
git branchList local branches
git branch <name>Create a new branch
git switch <name>Switch to a branch
git switch -c <name>Create and switch to a new branch
git merge <branch>Merge branch into current
git rebase <branch>Rebase current onto branch
git branch -d <name>Delete a merged branch
git branch -D <name>Force delete a branch

Remote Repositories

CommandDescription
git remote -vList remotes
git remote add origin <url>Add a remote
git fetchDownload remote changes
git pullFetch and merge
git pull --rebaseFetch and rebase
git pushPush to remote
git push -u origin <branch>Push and set upstream

Viewing History

CommandDescription
git log --onelineCompact commit log
git log --graph --allVisual branch graph
git log -p <file>Show changes to a file over time
git show <commit>Show a specific commit
git blame <file>Show who changed each line
git reflogShow history of HEAD changes

Undoing Changes

CommandDescription
git restore <file>Discard working directory changes
git restore --staged <file>Unstage a file
git reset --soft HEAD~1Undo last commit (keep changes staged)
git reset --mixed HEAD~1Undo last commit (keep changes unstaged)
git reset --hard HEAD~1Undo last commit (discard changes)
git revert <commit>Create a new commit that undoes a commit

Stashing

CommandDescription
git stashStash working changes
git stash popRestore and remove latest stash
git stash listList all stashes
git stash dropRemove the latest stash
git stash applyRestore latest stash (keep in stash list)

Tags

CommandDescription
git tag <name>Create a lightweight tag
git tag -a <name> -m "msg"Create an annotated tag
git tagList all tags
git push origin <tag>Push a tag to remote
git push origin --tagsPush all tags

How do I undo the last commit in Git?

Use git reset to undo the last commit. The three modes control what happens to your changes: soft keeps them staged, mixed (default) unstages them, and hard discards them entirely.

CommandWhat happens
git reset --soft HEAD~1Undo commit, keep changes staged — ready to re-commit
git reset HEAD~1Undo commit, keep changes as unstaged files
git reset --hard HEAD~1Undo commit and discard all changes permanently
git revert HEADCreate a new commit that reverses the last one (safe for shared branches)
git commit --amend -m "new msg"Replace the last commit message (before pushing)
git commit --amend --no-editAdd staged changes to the last commit without changing the message
git reset --soft HEAD~1 && git stashUndo last commit and stash the changes for later

How do I resolve a merge conflict in Git?

When Git cannot automatically merge changes, it marks the conflicted files. Open each file, choose which changes to keep, remove the conflict markers, then stage and commit.

CommandWhat it does
git merge <branch>Start a merge — conflicts will be flagged if they exist
git diff --name-only --diff-filter=UList all files with unresolved conflicts
git checkout --theirs <file>Accept the incoming (theirs) version of a file
git checkout --ours <file>Keep your current (ours) version of a file
git add <file>Mark a conflicted file as resolved after editing
git merge --continueComplete the merge after resolving all conflicts
git merge --abortAbort the merge and return to the state before it started

How do I rebase in Git?

Rebasing replays your branch commits on top of another branch, creating a linear history without merge commits. Use it to keep feature branches up to date with main before merging.

CommandWhat it does
git rebase mainRebase current branch onto main
git rebase origin/mainRebase onto remote main (after git fetch)
git pull --rebasePull remote changes and rebase your local commits on top
git rebase --continueContinue rebase after resolving a conflict
git rebase --skipSkip the current conflicting commit during rebase
git rebase --abortAbort and return to the state before rebase started
git push --force-with-leasePush rebased branch safely (fails if remote has new commits)

How do I squash the last N commits in Git?

Squashing combines multiple commits into one, keeping your history clean before merging. Use interactive rebase to pick which commits to squash, or use soft reset for a quick squash without an editor.

CommandWhat it does
git rebase -i HEAD~NInteractive rebase — mark commits as "squash" or "fixup" in the editor
git reset --soft HEAD~N && git commitQuick squash: collapse last N commits into one new commit
git merge --squash <branch>Squash all commits from a branch into a single staged changeset
git rebase -i --autosquash HEAD~NAuto-arrange fixup!/squash! commits during interactive rebase
git commit --fixup <commit-hash>Create a fixup commit to be auto-squashed later

Git one-liners you always forget

Copy-paste commands for common tasks that are hard to remember. Useful one-liners for everyday Git workflows including deleting branches, searching history, and recovering lost work.

CommandWhat it does
git log --all --oneline --graph --decoratePretty-print the full branch graph in your terminal
git branch -a --merged | grep -v mainList all branches already merged into main
git remote prune originRemove local refs to deleted remote branches
git log -S "searchterm" --onelineFind commits that added or removed a specific string
git diff main..HEAD --statShow a summary of all changes vs main
git stash -uStash all changes including untracked files
git cherry-pick <commit-hash>Apply a single commit from another branch
git clean -fdRemove all untracked files and directories
git log --oneline --since="1 week ago"Show commits from the last week
git reflog | head -20Show recent HEAD movements — useful for recovering lost commits