Git Basics Cheat Sheet

Essential Git commands for everyday development — clone, commit, push, pull, and branch management.

Last Updated: May 1, 2025

Setup & Configuration

ItemDescription
git config --global user.name NameSet your commit author name globally
git config --global user.email emailSet your commit author email globally
git config --global core.editor vimSet default text editor for commit messages
git config --listShow all current configuration settings
git config --global init.defaultBranch mainSet default branch name to main
git initCreate a new Git repository in current directory
git clone urlClone an existing repository from a remote URL
git clone url --depth 1Shallow clone — only the latest commit
git remote add origin urlLink local repository to a remote
git remote -vShow all remote repositories with URLs

Daily Workflow

git status
Show working tree status: modified, staged, untracked
git add file
Stage a specific file for the next commit
git add .
Stage all changes in the current directory
git add -p
Interactively stage hunks of changes (very useful!)
git commit -m message
Commit staged changes with a message
git commit -am message
Add all tracked files and commit in one step
git push origin main
Push commits to the remote main branch
git pull origin main
Fetch and merge remote changes into local
git fetch origin
Download remote changes without merging
git log --oneline --graph --all
Compact visual history of all branches

Undoing Changes

git restore file
Discard changes in working directory
git restore --staged file
Unstage a file (keep the changes)
git reset HEAD~1
Undo last commit, keep changes as unstaged
git reset --soft HEAD~1
Undo last commit, keep changes staged
git reset --hard HEAD~1
Undo last commit AND discard all changes
git revert commit
Create a new commit that undoes a specific commit
git stash
Temporarily save uncommitted changes
git stash pop
Restore the most recently stashed changes
git stash list
List all stashed changes
git clean -fd
Remove untracked files and directories

Inspecting History

git log
Full commit history with author, date, and message
git log --oneline
Compact one-line-per-commit view
git log -p file
Show commits that changed a specific file
git show commit
Show full details of a specific commit
git diff
Show unstaged changes since last commit
git diff --staged
Show staged changes about to be committed
git blame file
Show who last modified each line of a file
git tag
List all tags
git tag -a v1.0 -m message
Create an annotated tag
git shortlog -sn
Show commit count per author
Pro Tip: Use `git status` frequently — it tells you exactly what state your repository is in before every command.