The Git Commands Cheatsheet Every Developer Needs in 2026

#1Git commands developers actually use day to day
What we tested: We evaluated the tools and techniques described in this article using real developer workflows. All browser-based tools on this site run locally, your data never leaves your machine.
Git has a lot of commands, but most developers only need a small subset most of the time.
This reference sticks to that subset: everyday work, branching, recovery, and the commands that help when history gets messy.
#2Everyday Commands
# Status
git status # what is staged, unstaged, untracked
git status -s # short format, faster to scan
# Stage
git add file.ts # stage specific file
git add src/ # stage entire directory
git add -p # interactive staging, pick hunks (VERY useful)
git add -u # stage all modified/deleted, skip untracked
# Commit
git commit -m "feat: add user auth"
git commit --amend # modify last commit (message or staged files)
git commit --amend --no-edit # amend without changing the message
# View history
git log --oneline -20 # last 20 commits, compact
git log --oneline --graph --all # visual branch graph
git log -p file.ts # full diff history for one file
git log --since="2 weeks ago"
git log --author="Alice"
# Diff
git diff # unstaged changes
git diff --staged # staged changes (about to be committed)
git diff main..feature/auth # diff between branches
git diff HEAD~3 # diff vs 3 commits ago#2Branching
# Create and switch
git checkout -b feature/auth # old way, still works
git switch -c feature/auth # new way (git 2.23+)
# Switch without creating
git checkout main
git switch main
# List branches
git branch # local
git branch -a # local + remote
git branch -vv # show tracking info
# Delete
git branch -d feature/auth # safe delete, blocked if unmerged
git branch -D feature/auth # force delete, even if unmerged
# Rename current branch
git branch -m new-name
# Track a remote branch
git branch --track feature/auth origin/feature/auth
# Or when switching:
git switch --track origin/feature/auth#2Remote Operations
# Fetch vs Pull
git fetch # download changes, don't merge
git fetch --prune # also remove deleted remote branches locally
git pull # fetch + merge (or rebase with config)
git pull --rebase # fetch + rebase, cleaner history
# Push
git push origin feature/auth
git push -u origin feature/auth # set upstream tracking
git push --force-with-lease # force push safely, fails if remote changed
# NEVER use git push --force on shared branches
# Remove remote branch
git push origin --delete feature/auth#2Stash, Temporary Shelf
# Save work in progress
git stash # stash with auto-generated message
git stash push -m "WIP: login form validation" # with custom message
# Include untracked files
git stash push --include-untracked
# List stashes
git stash list
# Apply
git stash pop # apply most recent stash + remove it
git stash apply stash@{2} # apply specific stash, keep it in list
# Show stash contents
git stash show -p # diff of most recent stash
git stash show -p stash@{1}
# Delete
git stash drop stash@{0}
git stash clear # delete ALL stashes#2Rebase
# Rebase feature branch onto main
git switch feature/auth
git rebase main
# Interactive rebase, squash, reorder, edit commits
git rebase -i HEAD~5 # rebase last 5 commits interactively
# In interactive mode, each line is a commit:
# pick abc1234 feat: add login form
# squash def5678 fix: typo in login
# reword ghi9012 WIP stuff → rename this commit
# Continue / abort
git rebase --continue # after resolving conflicts
git rebase --abort # cancel and return to original state
git rebase --skip # skip the conflicting commit
# Pull with rebase instead of merge (recommended for cleaner history)
git pull --rebase origin main⚠️ Never rebase commits that have been pushed to a shared branch. Rebase rewrites history, it is safe only on your local or solo branches.
#2Cherry-Pick, Bring One Commit to Another Branch
# Apply a specific commit to your current branch
git cherry-pick abc1234
# Apply a range of commits
git cherry-pick abc1234..def5678
# Cherry-pick without committing (apply changes only, stage them)
git cherry-pick --no-commit abc1234
# Continue / abort
git cherry-pick --continue
git cherry-pick --abortUse cherry-pick to backport a bug fix from main to a release branch, or to bring one specific feature commit to another branch without merging everything.
#2Rescue Commands. When Things Go Wrong
# Undo last commit, keep changes staged
git reset --soft HEAD~1
# Undo last commit, keep changes unstaged
git reset HEAD~1
# Undo last commit, discard all changes (DESTRUCTIVE)
git reset --hard HEAD~1
# Restore a single file to last commit state
git restore file.ts # discard unstaged changes to file.ts
git restore --staged file.ts # unstage file.ts (don't discard changes)
# Recover a deleted branch or lost commit
git reflog # shows every HEAD movement, including deleted branches
git switch -c recovered-branch abc1234 # create branch at the recovered commit
# Undo a pushed commit safely (creates a new reverting commit)
git revert abc1234
# Find which commit introduced a bug
git bisect start
git bisect bad # current commit is broken
git bisect good v1.2.0 # tag or hash where it was working
# Git checks out a midpoint, test it
git bisect good # if this commit is fine
git bisect bad # if this commit is broken
# Repeat until git identifies the culprit
git bisect reset # return to original HEAD#2Git Worktrees, Multiple Working Directories
Git worktrees let you check out multiple branches simultaneously without stashing or committing:
# Create a worktree for a different branch
git worktree add ../project-hotfix hotfix/critical-bug
# Now you have two directories:
# /project , your current branch
# /project-hotfix , the hotfix branch
# List worktrees
git worktree list
# Remove when done
git worktree remove ../project-hotfixUse worktrees when you need to fix a critical bug while keeping your feature work intact, or when you need to run two branches simultaneously for comparison.
#2Sparse Checkout. Only Check Out Part of a Monorepo
# Enable sparse checkout (for large monorepos)
git clone --filter=blob:none --sparse https://github.com/org/monorepo.git
cd monorepo
git sparse-checkout set apps/my-app packages/shared-ui
# Only downloads and shows those directories#2Configuration and Aliases
# Essential global config
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global pull.rebase true # always rebase on pull
git config --global rebase.autoStash true # auto-stash before rebase
git config --global push.default current # push current branch by default
# The 10 most useful aliases
git config --global alias.st "status -s"
git config --global alias.co "checkout"
git config --global alias.sw "switch"
git config --global alias.br "branch"
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.last "log -1 HEAD --stat"
git config --global alias.unstage "restore --staged"
git config --global alias.undo "reset HEAD~1"
git config --global alias.aliases "config --get-regexp alias"
git config --global alias.whoami "config user.email"
# Use them
git st # git status -s
git lg # visual log
git undo # undo last commit, keep changes#2Common Mistakes
- Using
git push --forceon a shared branch. This overwrites teammates' commits. Always usegit push --force-with-lease, it fails if someone pushed since your last fetch, preventing accidental data loss. - Committing to
maindirectly. Use feature branches + PRs. Direct commits tomainbypass review and make reverting harder. - Not using
git add -pfor large changes. Staging entire files when you only want some hunks leads to commits that mix unrelated changes.git add -plets you stage individual chunks interactively. - Running
git reset --hardwithout checkinggit stashorgit reflogfirst. Hard reset is permanent. Always check if you need to save anything first. - Rebasing pushed commits. Rebasing rewrites commit hashes. If your branch is on the remote, teammates will get a diverged history. Only rebase local-only commits.
#2Frequently Asked Questions
#3What is the difference between git merge and git rebase?
Both integrate changes from one branch into another. merge creates a new merge commit that preserves the full history of both branches. rebase replays your commits on top of the target branch, creating a linear history but rewriting commit hashes. Use merge for long-running branches and shared work; use rebase to keep feature branches linear before merging into main.
#3How do I fix "detached HEAD" state?
git switch -c new-branch-name # create a branch from your current position
# or
git switch main # discard uncommitted changes and return to mainDetached HEAD means HEAD points to a commit, not a branch. Any commits you make will be orphaned unless you create a branch first.
#3What does git fetch do differently from git pull?
git fetch downloads changes from the remote but does not change your local files or branches. It updates origin/main, origin/feature/auth, etc. git pull is git fetch followed by git merge (or git rebase with pull.rebase true). Use fetch first when you want to inspect changes before integrating.
#3How do I squash all commits on a feature branch into one?
git switch main
git merge --squash feature/auth
git commit -m "feat: complete auth implementation"Or use interactive rebase: git rebase -i main and change all pick to squash except the first.
#3How do I find who changed a specific line?
git blame file.ts # shows last commit that touched each line
git blame -L 42,55 file.ts # just lines 42-55
git log -p -S "functionName" --all # find all commits that added/removed "functionName"Written by Rahul Jalavadiya, founder of AllDevToolsHub. All tools run locally in your browser.
#2Sources / Further reading
- Git SCM - Official documentation
- Git SCM - Git reference
- Atlassian - Git cheatsheet
#2Try These Tools on AllDevToolsHub
- Commit Message Generator — Generate conventional commit messages following the Conventional Commits specification
- Changelog Generator — Auto-generate changelogs from your Git history following Keep a Changelog format
- .gitignore Generator — Build comprehensive .gitignore files for any tech stack
All tools run entirely in your browser. No sign-up, no data upload, no server round-trips.
Quick Summary
>- The definitive git commands reference for 2026. Covers everyday commands, branching and merging, stash, rebase, cherry-pick, git worktrees, sparse-checkout, and the 10 most useful aliases — all with practical examples. Includes the commands that rescue you when things go wrong.
Key Takeaways
- Git's three-tree architecture (HEAD, index, working directory) is the mental model for understanding every Git command.
- git reset, git restore, and git checkout overlap in functionality — use git restore for file operations and git reset for commit operations.
- Interactive rebase (git rebase -i) is the most powerful history-rewriting tool — use it to squash, reorder, and edit commits before pushing.
When to use it
- Undoing the last commit while keeping changes staged: git reset --soft HEAD~1.
- Cleaning up a feature branch before merging: git rebase -i main to squash and reorder commits.
- Finding which commit introduced a bug: git bisect start with binary search between known good and bad commits.
Common Mistakes
- Force-pushing to shared branches — this rewrites history for everyone and causes merge conflicts.
- Using git checkout for both branch switching and file restoration — the newer git switch and git restore commands are clearer.
- Committing directly to main instead of feature branches — this makes code review and rollback difficult.
The Git Commands Cheatsheet Every Developer Needs in 2026, Frequently Asked
What is the difference between git reset and git restore?
git reset moves the HEAD pointer and optionally updates the index — it operates on commits. git restore operates on files in the working directory and index — it undoes changes without moving HEAD.
How do I undo a git push?
You cannot truly undo a push. You can push a revert commit (git revert) which adds an inverse commit, or force-push a rewritten history (dangerous on shared branches).
Tools Mentioned in This Article
.gitignore Generator
Generate .gitignore files for any language, framework, OS, or editor.
Git Commit Message Generator
Generate structured conventional commit messages from git diffs or change descriptions with AI or instant rule-based parsing.
.env File Parser
Parse, edit, and export .env files as JSON, Docker flags, or shell exports.
Crontab Expression Generator
Build and validate cron expressions with a visual editor.
Tools, tactics, and toughened-up tips, once a week
New tools, deep-dives on developer workflows, and the occasional gem we found this week. No spam, no tracking. Unsubscribe anytime.
Found an error or have feedback?
We correct errors quickly and document changes in our changelog. Report issues at support@alldevtoolshub.com.