MeshWorld India LogoMeshWorld.
CheatsheetGitVersion ControlTerminalDeveloper ToolsGitHub8 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

CommandWhat it does
git config --global user.name "Name"Set your commit name
git config --global user.email "[email protected]"Set your commit email
git config --global init.defaultBranch mainSet default branch to main
git config --global core.editor "code --wait"Set VS Code as default editor
git config --listShow all config values

Creating & cloning

CommandWhat it does
git initInitialize a repo in current directory
git init my-projectInitialize in a new folder
git clone <url>Clone a remote repo
git clone <url> my-folderClone into a specific folder name
git clone --depth 1 <url>Shallow clone (latest commit only)

Staging & committing

CommandWhat it does
git statusShow working tree status
git add file.jsStage a specific file
git add .Stage all changes in current directory
git add -pStage changes interactively (chunk by chunk)
git commit -m "message"Commit with a message
git commit --amendEdit the last commit (message or files)
git commit --amend --no-editAdd staged files to last commit, keep message

Branching

CommandWhat it does
git branchList local branches
git branch -aList all branches (local + remote)
git branch featureCreate a new branch
git switch featureSwitch to a branch
git switch -c featureCreate and switch in one step
git branch -d featureDelete a branch (safe — merged only)
git branch -D featureForce delete a branch
git branch -m old newRename a branch

Merging & rebasing

CommandWhat it does
git merge featureMerge feature into current branch
git merge --no-ff featureMerge with a merge commit (no fast-forward)
git merge --squash featureSquash all commits into one before merging
git rebase mainRebase current branch onto main
git rebase -i HEAD~3Interactive rebase — edit last 3 commits
git rebase --abortCancel a rebase in progress
git rebase --continueContinue after resolving conflicts

Stashing

CommandWhat it does
git stashStash current changes
git stash push -m "message"Stash with a description
git stash listList all stashes
git stash popApply 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 clearDelete all stashes

Undoing changes

CommandWhat it does
git restore file.jsDiscard unstaged changes to a file
git restore --staged file.jsUnstage a file (keep changes)
git reset HEAD~1Undo last commit, keep changes staged
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --mixed HEAD~1Undo last commit, unstage changes
git reset --hard HEAD~1Undo last commit, discard all changes
git revert <commit>Create a new commit that undoes a commit
git clean -fdDelete untracked files and directories

Remotes

CommandWhat it does
git remote -vList remotes with URLs
git remote add origin <url>Add a remote
git remote set-url origin <url>Change remote URL
git fetchDownload changes, don’t merge
git fetch --allFetch all remotes
git pullFetch and merge
git pull --rebaseFetch and rebase instead of merge
git pushPush current branch to remote
git push -u origin mainPush and set upstream tracking
git push --force-with-leaseForce push safely (fails if remote changed)
git push origin --delete featureDelete a remote branch

Inspecting & log

CommandWhat it does
git logFull commit history
git log --onelineCompact one-line history
git log --oneline --graphBranching history as ASCII graph
git log -pShow diffs for each commit
git log --author="Name"Filter by author
git log --since="2 weeks ago"Filter by date
git diffShow unstaged changes
git diff --stagedShow staged changes
git diff main..featureCompare two branches
git show <commit>Show a specific commit
git blame file.jsShow who changed each line

Tags

CommandWhat it does
git tagList all tags
git tag v1.0.0Create a lightweight tag
git tag -a v1.0.0 -m "Release"Create an annotated tag
git push --tagsPush all tags to remote
git push origin v1.0.0Push 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

Share_This Twitter / X
Vishnu
Written By

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Enjoyed this article?

Support MeshWorld and help us create more technical content