What Is Git?
Git is a distributed version control system created by Linus Torvalds in 2005. Every developer working on modern software uses Git daily — it tracks changes, enables collaboration, and provides a safety net to undo mistakes. This git commands cheatsheet online covers the commands you'll use most frequently, organized by workflow.
How to Use This Git Commands Cheatsheet
- Find your scenario — Each section covers a specific workflow area.
- Copy the command — Commands are shown with exact syntax and common flags.
- Check the explanation — Brief notes clarify what each command does and when to use it.
- Bookmark this page — Reference it whenever you need a quick reminder.
- Practice in a test repo — Create a dummy repository to experiment with destructive commands safely.
Essential Git Commands by Category
Setup and Configuration
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global core.editor "code --wait"
git config --list
Starting a Repository
git init # Initialize new repo
git clone <url> # Clone remote repo
git clone <url> --depth 1 # Shallow clone (faster)
Staging and Committing
git status # Show working tree status
git add <file> # Stage specific file
git add . # Stage all changes
git commit -m "message" # Commit with message
git commit --amend # Modify last commit
Branching
git branch # List branches
git branch <name> # Create branch
git checkout -b <name> # Create and switch
git switch <name> # Switch branch (modern)
git branch -d <name> # Delete merged branch
git branch -D <name> # Force delete branch
Merging and Rebasing
git merge <branch> # Merge branch into current
git merge --no-ff <branch> # Merge with merge commit
git rebase <branch> # Rebase onto branch
git rebase -i HEAD~3 # Interactive rebase last 3 commits
Remote Operations
git remote -v # List remotes
git remote add origin <url> # Add remote
git fetch origin # Fetch without merging
git pull origin main # Fetch and merge
git push origin <branch> # Push branch to remote
git push -u origin <branch> # Push and set upstream
Undoing Changes
git restore <file> # Discard working dir changes
git restore --staged <file> # Unstage file
git reset HEAD~1 # Undo last commit, keep changes
git reset --hard HEAD~1 # Undo last commit, discard changes
git revert <commit> # Create revert commit (safe)
Stashing
git stash # Stash current changes
git stash pop # Apply and remove latest stash
git stash list # List all stashes
git stash drop stash@{0} # Delete specific stash
Use Cases
Hotfix Workflow
When a production bug needs immediate fixing: git checkout -b hotfix/bug-123 main, fix the bug, commit, then merge back to main and your development branch.
Cleaning Up Feature Branch History
Before merging a feature branch with messy WIP commits, use git rebase -i HEAD~N to squash, reorder, or reword commits into a clean, meaningful history.
aiforeverthing.com — Code formatters, validators, and more. No signup required.
Frequently Asked Questions
What is the difference between git fetch and git pull?
git fetch downloads remote changes but does not apply them to your working branch. git pull fetches and immediately merges (or rebases with --rebase). Use git fetch when you want to review changes before integrating them.
When should I use git rebase vs git merge?
Use git merge to preserve the full history of when branches diverged and merged. Use git rebase to create a linear history that's easier to read. Never rebase commits that have been pushed to a shared branch — it rewrites history and disrupts teammates.
How do I undo a git push?
If you pushed to a private branch, use git push --force-with-lease after resetting locally. If you pushed to a shared branch, use git revert to create a new commit that undoes the changes without rewriting history.
What is git stash used for?
Stash temporarily shelves changes you're not ready to commit, letting you switch branches with a clean working directory. Use git stash when you need to urgently switch context, then git stash pop to restore your work.
How do I see git log in a readable format?
Use git log --oneline --graph --decorate --all for a compact visual representation of branch history. Add this as a git alias: git config --global alias.lg "log --oneline --graph --decorate --all".
Recommended Hosting for Developers
- Hostinger — From $2.99/mo. Excellent for static sites and Node.js apps.
- DigitalOcean — $200 free credit for new accounts. Best for scalable backends.
- Namecheap — Budget-friendly shared hosting with free domain.