MeshWorld India Logo MeshWorld.
Cheatsheet Git Version Control Terminal Developer Tools GitHub 8 min read

Git Cheat Sheet: Every Command You Actually Need

Vishnu
By Vishnu
| Updated: Mar 11, 2026
Git Cheat Sheet: Every Command You Actually Need
TL;DR
  • Stage: git add -p (interactively), commit: git commit -m with present tense
  • Undo: git restore (unstage), git checkout (discard changes), git reset --soft HEAD~1 (undo last commit)
  • Branch: git switch -c (create + switch), git switch - (toggle last)
  • Push: git push -u origin branchname (first push with tracking)
  • Fix: git rebase -i HEAD~3 (interactive rebase), git commit --amend (change last commit)

Quick reference tables

Setup & config

| Command | What it does | |---|---| | git config --global user.name "Name" | Set your commit name | | git config --global user.email "you@x.com" | Set your commit email | | git config --global init.defaultBranch main | Set default branch to main | | git config --global core.editor "code --wait" | Set VS Code as default editor | | git config --list | Show all config values |

Creating & cloning

| Command | What it does | |---|---| | git init | Initialize a repo in current directory | | git init my-project | Initialize in a new folder | | git clone <url> | Clone a remote repo | | git clone <url> my-folder | Clone into a specific folder name | | git clone --depth 1 <url> | Shallow clone (latest commit only) |

Staging & committing

| Command | What it does | |---|---| | git status | Show working tree status | | git add file.js | Stage a specific file | | git add . | Stage all changes in current directory | | git add -p | Stage changes interactively (chunk by chunk) | | git commit -m "message" | Commit with a message | | git commit --amend | Edit the last commit (message or files) | | git commit --amend --no-edit | Add staged files to last commit, keep message |

Branching

| Command | What it does | |---|---| | git branch | List local branches | | git branch -a | List all branches (local + remote) | | git branch feature | Create a new branch | | git switch feature | Switch to a branch | | git switch -c feature | Create and switch in one step | | git branch -d feature | Delete a branch (safe — merged only) | | git branch -D feature | Force delete a branch | | git branch -m old new | Rename a branch |

Merging & rebasing

| Command | What it does | |---|---| | git merge feature | Merge feature into current branch | | git merge --no-ff feature | Merge with a merge commit (no fast-forward) | | git merge --squash feature | Squash all commits into one before merging | | git rebase main | Rebase current branch onto main | | git rebase -i HEAD~3 | Interactive rebase — edit last 3 commits | | git rebase --abort | Cancel a rebase in progress | | git rebase --continue | Continue after resolving conflicts |

Stashing

| Command | What it does | |---|---| | git stash | Stash current changes | | git stash push -m "message" | Stash with a description | | git stash list | List all stashes | | git stash pop | Apply most recent stash and remove it | | git stash apply stash@{2} | Apply a specific stash, keep it in list | | git stash drop stash@{0} | Delete a stash | | git stash clear | Delete all stashes |

Undoing changes

| Command | What it does | |---|---| | git restore file.js | Discard unstaged changes to a file | | git restore --staged file.js | Unstage a file (keep changes) | | git reset HEAD~1 | Undo last commit, keep changes staged | | git reset --soft HEAD~1 | Undo last commit, keep changes staged | | git reset --mixed HEAD~1 | Undo last commit, unstage changes | | git reset --hard HEAD~1 | Undo last commit, discard all changes | | git revert <commit> | Create a new commit that undoes a commit | | git clean -fd | Delete untracked files and directories |

Remotes

| Command | What it does | |---|---| | git remote -v | List remotes with URLs | | git remote add origin <url> | Add a remote | | git remote set-url origin <url> | Change remote URL | | git fetch | Download changes, don’t merge | | git fetch --all | Fetch all remotes | | git pull | Fetch and merge | | git pull --rebase | Fetch and rebase instead of merge | | git push | Push current branch to remote | | git push -u origin main | Push and set upstream tracking | | git push --force-with-lease | Force push safely (fails if remote changed) | | git push origin --delete feature | Delete a remote branch |

Inspecting & log

| Command | What it does | |---|---| | git log | Full commit history | | git log --oneline | Compact one-line history | | git log --oneline --graph | Branching history as ASCII graph | | git log -p | Show diffs for each commit | | git log --author="Name" | Filter by author | | git log --since="2 weeks ago" | Filter by date | | git diff | Show unstaged changes | | git diff --staged | Show staged changes | | git diff main..feature | Compare two branches | | git show <commit> | Show a specific commit | | git blame file.js | Show who changed each line |

Tags

| Command | What it does | |---|---| | git tag | List all tags | | git tag v1.0.0 | Create a lightweight tag | | git tag -a v1.0.0 -m "Release" | Create an annotated tag | | git push --tags | Push all tags to remote | | git push origin v1.0.0 | Push a specific tag |


Want to go deeper on one of the most underrated Git features? Read Git Worktree: Work on Two Branches at the Same Time.

Detailed sections

The everyday workflow

bash
git status                    # see what's changed
git add .                     # stage everything
git commit -m "feat: add login"   # commit
git push                      # push to remote

Or stage only specific changes:

bash
git add -p                    # review and stage chunk by chunk
git commit -m "fix: edge case in auth"

Branching workflow

bash
git switch main               # start from main
git pull                      # get latest
git switch -c feature/login   # create and switch to feature branch

# do your work...

git add .
git commit -m "feat: add login form"
git push -u origin feature/login

# open a PR, get it merged, then clean up:
git switch main
git pull
git branch -d feature/login

Fixing the last commit

bash
# Wrong message?
git commit --amend

# Forgot to add a file?
git add forgotten-file.js
git commit --amend --no-edit

Only amend commits that haven’t been pushed. If already pushed, use git revert instead.

Undoing things safely

Unstaged changes you don’t want:

bash
git restore file.js

A file you staged by mistake:

bash
git restore --staged file.js

A commit that hasn’t been pushed:

bash
git reset --soft HEAD~1   # undo commit, keep staged
git reset HEAD~1           # undo commit, unstage changes
git reset --hard HEAD~1    # undo commit, discard everything

A commit that’s already pushed (use revert — it’s safe):

bash
git revert <commit-hash>   # creates a new undo commit
git push

Interactive rebase — clean up before merging

bash
git rebase -i HEAD~4    # open editor for last 4 commits

In the editor, change pick to:

  • r — reword: edit commit message
  • s — squash: merge into previous commit
  • f — fixup: squash silently (discard message)
  • d — drop: delete this commit

Resolving merge conflicts

When Git reports conflicts:

bash
# Open conflicting files, look for conflict markers:
# <<<<<<< HEAD
# your changes
# =======
# their changes
# >>>>>>> feature

# Edit the file to keep what you want, then:
git add resolved-file.js
git commit   # or git rebase --continue if rebasing

Useful aliases

Add these to ~/.gitconfig:

ini
[alias]
  st = status
  co = switch
  lg = log --oneline --graph --all
  undo = reset HEAD~1
  staged = diff --staged

Then use: git lg, git st, git undo.

Related: SSH Keys for GitHub Setup | SSH & GPG Cheat Sheet | GitHub Actions CI/CD Setup