DevToolsForYou

Git Commands Cheatsheet

A quick reference for everyday Git commands — setup, staging, committing, branching, merging, rebasing, and remote operations.

Updated Apr 11, 2026

Sections

  1. Setup & Config
  2. Staging & Committing
  3. Branching
  4. Merging & Rebasing
  5. Remote Operations
  6. Undoing Changes
  7. Stash
  8. Tags

Setup & Config

CommandDescription
git initInitialise a new repository in the current directory
git clone <url>Clone a remote repository locally
git config --global user.name "Name"Set your commit author name globally
git config --global user.email "…"Set your commit author email globally
git config --listShow all config values

Staging & Committing

CommandDescription
git statusShow working tree status — staged, unstaged, and untracked files
git add <file>Stage a specific file
git add .Stage all changes in the current directory
git add -pInteractively stage hunks
git commit -m "msg"Commit staged changes with a message
git commit --amendRewrite the last commit (message and/or content)
git diffShow unstaged changes
git diff --stagedShow staged changes vs last commit
git log --onelineCompact one-line commit history
git log --graphASCII graph of branch history

Branching

CommandDescription
git branchList local branches
git branch -aList all branches (local + remote)
git branch <name>Create a new branch
git switch <name>Switch to an existing branch (modern syntax)
git switch -c <name>Create and switch to a new branch
git branch -d <name>Delete a merged branch
git branch -D <name>Force-delete a branch (even if unmerged)
git branch -m <old> <new>Rename a branch

Merging & Rebasing

CommandDescription
git merge <branch>Merge branch into the current branch
git merge --no-ff <branch>Merge with a merge commit (no fast-forward)
git merge --squash <branch>Squash all commits from branch into one staged change
git rebase <branch>Rebase current branch on top of branch
git rebase -i HEAD~nInteractive rebase — squash, edit, reorder last n commits
git cherry-pick <hash>Apply a specific commit onto the current branch

Remote Operations

CommandDescription
git remote -vShow remote URLs
git remote add origin <url>Add a remote named origin
git fetchDownload remote changes without merging
git pullFetch and merge remote changes
git pull --rebaseFetch and rebase instead of merge
git push origin <branch>Push branch to remote
git push -u origin <branch>Push and set upstream tracking
git push --force-with-leaseSafe force push — fails if remote has new commits

Undoing Changes

CommandDescription
git restore <file>Discard unstaged changes to a file
git restore --staged <file>Unstage a file (keep changes in working tree)
git revert <hash>Create a new commit that undoes a commit (safe for shared history)
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --mixed HEAD~1Undo last commit, keep changes unstaged (default)
git reset --hard HEAD~1Undo last commit and discard all changes — destructive
git clean -fdDelete untracked files and directories

Stash

CommandDescription
git stashStash uncommitted changes
git stash push -m "msg"Stash with a descriptive message
git stash listShow all stashes
git stash popApply latest stash and remove it from the list
git stash applyApply latest stash without removing it
git stash dropDelete the latest stash entry
git stash clearRemove all stashes

Tags

CommandDescription
git tagList all tags
git tag v1.0.0Create a lightweight tag on HEAD
git tag -a v1.0.0 -m "msg"Create an annotated tag with a message
git push origin v1.0.0Push a specific tag to remote
git push origin --tagsPush all tags to remote
git tag -d v1.0.0Delete a local tag
git push origin --delete v1.0.0Delete a remote tag