🌿 Git Cheat Sheet

87+ commands covering setup, branching, merging, rebase, stash, remote, tags and undo — with real examples.

87 commands
Setup & Config
Command Description Example
git config --global user.name "Your Name"
Set your name for all commits globally git config --global user.name "Amit Jain"
git config --global user.email "you@example.com"
Set your email for all commits globally git config --global user.email "amit@freebytes.in"
git config --list
List all Git configuration settings git config --list
git config --global core.editor "code --wait"
Set VS Code as the default editor git config --global core.editor "code --wait"
git config --global alias.st status
Create a shorthand alias for a command git config --global alias.lg "log --oneline --graph --all"
Basics
Command Description Example
git init
Initialise a new Git repository in the current directory mkdir myproject && cd myproject && git init
git clone <url>
Clone a remote repository locally git clone https://github.com/user/repo.git
git clone <url> <dir>
Clone into a specific directory name git clone https://github.com/user/repo.git my-folder
git status
Show the working tree status — staged, unstaged, and untracked files git status
git add <file>
Stage a specific file for the next commit git add index.php
git add .
Stage all changes in the current directory git add .
git add -p
Interactively stage chunks of changes (patch mode) git add -p
git commit -m "message"
Commit staged changes with a message git commit -m "add user login feature"
git commit -am "message"
Stage all tracked-file changes and commit in one step git commit -am "fix typo in header"
git diff
Show unstaged changes git diff
git diff --staged
Show changes that are staged but not yet committed git diff --staged
git rm <file>
Remove a file from working tree and stage the deletion git rm old-file.txt
git mv <old> <new>
Rename or move a file and stage the change git mv app.js main.js
Branching
Command Description Example
git branch
List all local branches (* marks the current one) git branch
git branch -a
List all branches including remote-tracking branches git branch -a
git branch <name>
Create a new branch at the current commit git branch feature/login
git checkout <branch>
Switch to an existing branch git checkout main
git checkout -b <name>
Create and switch to a new branch in one command git checkout -b feature/payment
git switch <branch>
Switch branches (modern syntax, Git 2.23+) git switch develop
git switch -c <name>
Create and switch to a new branch (modern syntax) git switch -c bugfix/null-pointer
git branch -d <name>
Delete a branch that has been merged git branch -d feature/login
git branch -D <name>
Force-delete a branch regardless of merge status git branch -D wip/experiment
git branch -m <old> <new>
Rename a local branch git branch -m old-name new-name
Merge & Rebase
Command Description Example
git merge <branch>
Merge the specified branch into the current branch git merge feature/login
git merge --no-ff <branch>
Merge and always create a merge commit (no fast-forward) git merge --no-ff feature/payment
git merge --squash <branch>
Squash all branch commits into a single staged change git merge --squash feature/cleanup
git merge --abort
Abort an in-progress merge and restore pre-merge state git merge --abort
git rebase <branch>
Reapply current branch commits on top of another branch git rebase main
git rebase -i HEAD~3
Interactive rebase — squash, reorder, or edit the last 3 commits git rebase -i HEAD~5
git rebase --abort
Abort an in-progress rebase git rebase --abort
git rebase --continue
Continue rebase after resolving conflicts git rebase --continue
git cherry-pick <hash>
Apply a specific commit from another branch to the current branch git cherry-pick a1b2c3d
Remote
Command Description Example
git remote -v
List all remotes with their URLs git remote -v
git remote add origin <url>
Add a remote named origin git remote add origin https://github.com/user/repo.git
git remote remove <name>
Remove a remote connection git remote remove origin
git remote rename <old> <new>
Rename a remote git remote rename origin upstream
git fetch
Download objects from all remotes without merging git fetch
git fetch origin
Fetch from the origin remote only git fetch origin
git pull
Fetch and merge remote changes into the current branch git pull
git pull --rebase
Fetch and rebase instead of merge (cleaner linear history) git pull --rebase origin main
git push origin <branch>
Push a local branch to the remote git push origin feature/login
git push -u origin <branch>
Push and set upstream tracking for the branch git push -u origin main
git push origin --delete <branch>
Delete a branch on the remote git push origin --delete feature/old
git remote prune origin
Remove stale remote-tracking references git remote prune origin
Stash
Command Description Example
git stash
Stash uncommitted changes (tracked files only) git stash
git stash push -m "message"
Stash with a descriptive name git stash push -m "WIP: payment form"
git stash -u
Stash including untracked files git stash -u
git stash list
List all stashes git stash list
git stash pop
Apply the latest stash and remove it from the stash list git stash pop
git stash apply stash@{2}
Apply a specific stash without removing it git stash apply stash@{2}
git stash drop stash@{1}
Delete a specific stash git stash drop stash@{1}
git stash clear
Delete all stashes git stash clear
Undo & Reset
Command Description Example
git restore <file>
Discard unstaged changes in a file (modern syntax) git restore index.php
git restore --staged <file>
Unstage a file (keep changes in working tree) git restore --staged style.css
git reset --soft HEAD~1
Undo last commit, keep changes staged git reset --soft HEAD~1
git reset --mixed HEAD~1
Undo last commit, unstage changes (default) git reset HEAD~1
git reset --hard HEAD~1
Undo last commit and discard all changes permanently git reset --hard HEAD~1
git revert <hash>
Create a new commit that reverses a specific commit (safe for shared branches) git revert a1b2c3d
git clean -fd
Remove untracked files and directories from the working tree git clean -fd
Tags
Command Description Example
git tag
List all tags git tag
git tag v1.0.0
Create a lightweight tag at the current commit git tag v1.0.0
git tag -a v1.0.0 -m "Release 1.0"
Create an annotated tag with a message git tag -a v2.1.0 -m "Stable release 2.1.0"
git tag -a v1.0.0 <hash>
Tag a specific past commit git tag -a v1.0.0 a1b2c3d -m "Hotfix release"
git push origin <tagname>
Push a single tag to the remote git push origin v1.0.0
git push origin --tags
Push all tags to the remote git push origin --tags
git tag -d v1.0.0
Delete a local tag git tag -d v1.0.0-beta
git push origin --delete v1.0.0
Delete a tag on the remote git push origin --delete v1.0.0-beta
Inspect & Log
Command Description Example
git log
Show commit history git log
git log --oneline
Compact one-line log view git log --oneline
git log --oneline --graph --all
Visual branch tree with all branches git log --oneline --graph --all
git log -p
Show diff introduced by each commit git log -p
git log --author="name"
Filter commits by author git log --author="Amit"
git log --since="2 weeks ago"
Show commits in a date range git log --since="2026-01-01" --until="2026-06-01"
git log -- <file>
Show history for a specific file git log -- src/app.js
git show <hash>
Show details and diff of a specific commit git show a1b2c3d
git blame <file>
Show who last modified each line of a file git blame index.php
git shortlog -sn
Summarise commits by author, sorted by count git shortlog -sn
Advanced
Command Description Example
git bisect start
Start binary search to find the commit that introduced a bug git bisect start && git bisect bad && git bisect good v1.0
git reflog
Show a log of where HEAD has been (recovery tool) git reflog
git worktree add <path> <branch>
Check out a branch into a new working directory alongside the current one git worktree add ../hotfix hotfix/critical-bug
git submodule add <url>
Add a Git repository as a submodule git submodule add https://github.com/user/lib.git vendor/lib
git archive --format=zip HEAD > out.zip
Export the repository as a zip without .git history git archive --format=zip HEAD > release.zip

No commands match your search. Try a different keyword.

Git Workflow Overview

Git tracks changes through four areas: the working tree (files you edit), the staging area / index (changes you've marked for the next commit with git add), the local repository (your committed history), and the remote repository (the shared server, usually GitHub or GitLab). Understanding this flow — edit → stage → commit → push — is the foundation of every Git workflow.

Git Branching Strategy

Most teams follow a variant of Git Flow or trunk-based development. In Git Flow: main holds production-ready code, develop is the integration branch, feature/* branches are created from develop, and hotfix/* branches are cut directly from main for urgent production fixes. In trunk-based development, everyone commits frequently to a single main branch using short-lived feature branches or feature flags.

Merge vs Rebase — When to Use Which

Use merge when working on shared branches (main, develop) — it preserves the full history of when branches were created and joined. Use rebase to clean up local feature branches before merging — it produces a linear history that is easier to read with git log. The golden rule: never rebase commits that have been pushed to a shared remote branch, as it rewrites history and causes conflicts for collaborators.

Undoing Mistakes in Git

Git gives you multiple ways to recover from mistakes depending on how far the change has gone. For uncommitted changes: git restore. For a commit that only exists locally: git reset (soft/mixed/hard). For a commit that has been pushed and others may have pulled: git revert — it creates a new commit that undoes the change, preserving history. In emergencies, git reflog lets you recover even from a hard reset by showing every position HEAD has been in.

Frequently Asked Questions — Git Cheat Sheet

Written and reviewed by the FreeBytes Editorial Team · Last updated: June 2026