Git Commands Cheatsheet — The Only Reference You Need
DodaTech
3 min read
In this tutorial, you'll learn about Git Commands Cheatsheet. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
What You'll Learn
A complete Git commands reference — organized by task with practical examples. Print this or bookmark it.
Setup and Config
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
git config --global init.defaultBranch main
git config --global core.editor vim
git config --list
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
Creating Repositories
git init # New repo
git init my-project # New repo in directory
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git # SSH
git clone --depth 1 <url> # Shallow clone (faster)
Basic Workflow
git status # Check file states
git status -s # Short status
git add file.txt # Stage a file
git add . # Stage all changes
git add -p # Interactive staging (hunk by hunk)
git commit -m "Message" # Commit with message
git commit -a -m "Message" # Stage tracked files + commit
git commit --amend -m "New" # Update last commit message
git commit --amend --no-edit # Add staged changes to last commit
git diff # Unstaged changes
git diff --staged # Staged changes
git diff main..feature # Compare branches
Branching
git branch # List local branches
git branch -a # All branches (local + remote)
git branch feature # Create branch
git branch -d feature # Delete branch (safe)
git branch -D feature # Delete branch (force)
git branch -m old new # Rename branch
git checkout feature # Switch to branch
git checkout -b feature # Create and switch
git checkout -b feature main # Create from main and switch
git switch feature # Modern switch (Git 2.23+)
git switch -c feature # Create and switch
Merging and Rebasing
git merge feature # Merge feature into current branch
git merge --no-ff feature # Force merge commit
git merge --abort # Abort merge with conflicts
git rebase main # Rebase current branch onto main
git rebase -i HEAD~3 # Interactive rebase last 3 commits
git rebase --continue # Continue after resolving conflicts
git rebase --skip # Skip a commit during rebase
git rebase --abort # Abort rebase
Remote Repositories
git remote -v # List remotes
git remote add origin <url> # Add remote
git push origin main # Push to remote
git push -u origin main # Push + set upstream (first time)
git push origin --delete feature # Delete remote branch
git push --tags # Push all tags
git pull # Fetch + merge
git pull --rebase # Fetch + rebase
git fetch # Fetch without merging
git fetch --prune # Fetch + remove deleted remote branches
git remote prune origin # Clean up stale remote-tracking branches
Undoing Changes
git restore file.txt # Discard unstaged changes
git restore --staged file.txt # Unstage a file (keep changes)
git reset HEAD~1 # Undo last commit, keep changes
git reset --hard HEAD~1 # Undo last commit, DISCARD changes
git reset --hard origin/main # Reset to remote state
git revert HEAD # Create a new commit that undoes HEAD
git revert <commit-hash> # Revert a specific commit
Stashing
git stash # Save changes temporarily
git stash push -m "message" # Stash with message
git stash list # List stashes
git stash pop # Apply + drop latest stash
git stash apply stash@{2} # Apply specific stash
git stash drop stash@{0} # Drop specific stash
git stash clear # Clear all stashes
History
git log # Full commit history
git log --oneline # One line per commit
git log --graph # Show branch graph
git log --oneline --graph --all
git log -p file.txt # Show patch for file
git log --author="name" # Filter by author
git log --since="2 weeks ago" # Filter by date
git blame file.txt # Who changed each line
git reflog # Reference log (everything you did)
git shortlog -sn # Contributors sorted by commits
Tags
git tag # List tags
git tag v1.0.0 # Create lightweight tag
git tag -a v1.0.0 -m "v1.0" # Annotated tag
git tag -d v1.0.0 # Delete local tag
git push origin tag v1.0.0 # Push a specific tag
git push --tags # Push all tags
Troubleshooting
git status # Start here
git log --oneline -5 # Recent history
git diff # See what changed
git reflog # Find "lost" commits
git fsck # Check repo integrity
git clean -fd # Remove untracked files
← Previous
Git Workflow — GitHub Flow, Git Flow, and Trunk-Based Compared
Next →
Git Reset, Revert, and Checkout — What's the Difference?
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro