Essential Git commands: how to undo the last commit, resolve merge conflicts, rebase branches, squash commits, and one-liners you always forget.
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
| Command | Description |
|---|
| git config --global user.name "Name" | Set your name |
| git config --global user.email "email" | Set your email |
| git config --global core.editor vim | Set default editor |
| git config --list | Show all settings |
Creating Repositories
| Command | Description |
|---|
| git init | Create a new local repo |
| git clone <url> | Clone a remote repo |
| git clone --depth 1 <url> | Shallow clone (latest commit only) |
Staging & Committing
| Command | Description |
|---|
| git status | Show working tree status |
| git add <file> | Stage a file |
| git add -p | Stage changes interactively (by hunk) |
| git commit -m "msg" | Commit with message |
| git commit --amend | Amend the last commit |
| git diff | Show unstaged changes |
| git diff --staged | Show staged changes |
Branching
| Command | Description |
|---|
| git branch | List 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
| Command | Description |
|---|
| git remote -v | List remotes |
| git remote add origin <url> | Add a remote |
| git fetch | Download remote changes |
| git pull | Fetch and merge |
| git pull --rebase | Fetch and rebase |
| git push | Push to remote |
| git push -u origin <branch> | Push and set upstream |
Viewing History
| Command | Description |
|---|
| git log --oneline | Compact commit log |
| git log --graph --all | Visual 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 reflog | Show history of HEAD changes |
Undoing Changes
| Command | Description |
|---|
| git restore <file> | Discard working directory changes |
| git restore --staged <file> | Unstage a file |
| git reset --soft HEAD~1 | Undo last commit (keep changes staged) |
| git reset --mixed HEAD~1 | Undo last commit (keep changes unstaged) |
| git reset --hard HEAD~1 | Undo last commit (discard changes) |
| git revert <commit> | Create a new commit that undoes a commit |
Stashing
| Command | Description |
|---|
| git stash | Stash working changes |
| git stash pop | Restore and remove latest stash |
| git stash list | List all stashes |
| git stash drop | Remove the latest stash |
| git stash apply | Restore latest stash (keep in stash list) |
Tags
| Command | Description |
|---|
| git tag <name> | Create a lightweight tag |
| git tag -a <name> -m "msg" | Create an annotated tag |
| git tag | List all tags |
| git push origin <tag> | Push a tag to remote |
| git push origin --tags | Push 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.
| Command | What happens |
|---|
| git reset --soft HEAD~1 | Undo commit, keep changes staged — ready to re-commit |
| git reset HEAD~1 | Undo commit, keep changes as unstaged files |
| git reset --hard HEAD~1 | Undo commit and discard all changes permanently |
| git revert HEAD | Create 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-edit | Add staged changes to the last commit without changing the message |
| git reset --soft HEAD~1 && git stash | Undo 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.
| Command | What it does |
|---|
| git merge <branch> | Start a merge — conflicts will be flagged if they exist |
| git diff --name-only --diff-filter=U | List 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 --continue | Complete the merge after resolving all conflicts |
| git merge --abort | Abort 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.
| Command | What it does |
|---|
| git rebase main | Rebase current branch onto main |
| git rebase origin/main | Rebase onto remote main (after git fetch) |
| git pull --rebase | Pull remote changes and rebase your local commits on top |
| git rebase --continue | Continue rebase after resolving a conflict |
| git rebase --skip | Skip the current conflicting commit during rebase |
| git rebase --abort | Abort and return to the state before rebase started |
| git push --force-with-lease | Push 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.
| Command | What it does |
|---|
| git rebase -i HEAD~N | Interactive rebase — mark commits as "squash" or "fixup" in the editor |
| git reset --soft HEAD~N && git commit | Quick 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~N | Auto-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.
| Command | What it does |
|---|
| git log --all --oneline --graph --decorate | Pretty-print the full branch graph in your terminal |
| git branch -a --merged | grep -v main | List all branches already merged into main |
| git remote prune origin | Remove local refs to deleted remote branches |
| git log -S "searchterm" --oneline | Find commits that added or removed a specific string |
| git diff main..HEAD --stat | Show a summary of all changes vs main |
| git stash -u | Stash all changes including untracked files |
| git cherry-pick <commit-hash> | Apply a single commit from another branch |
| git clean -fd | Remove all untracked files and directories |
| git log --oneline --since="1 week ago" | Show commits from the last week |
| git reflog | head -20 | Show recent HEAD movements — useful for recovering lost commits |