git

VERSION CONTROL · DISTRIBUTED · LINUS TORVALDS · 2005
Setup & Init
git init
Initialize a new repository in the current directory
git init <dir>
Create a new repo in a named subdirectory
git clone <url>
Clone a remote repository locally
git clone <url> --depth 1
Shallow clone — only the latest commit (fast)
git config --global user.name "Name"
Set your global commit author name
git config --global user.email "e@mail"
Set your global commit author email
Staging & Committing
git status
Show working tree status — staged, unstaged, untracked files
git add <file>
Stage a specific file for the next commit
git add -p
Interactively stage hunks — review each change before staging
git add .
Stage all changes in the current directory (use with caution)
git commit -m "message"
Commit staged changes with a message
git commit --amend
Amend the last commit — change message or add staged files
git reset HEAD <file>
Unstage a file without discarding changes
git restore <file>
Discard working-directory changes for a file
git rm <file>
Remove a file and stage the deletion
git mv <old> <new>
Move or rename a file and stage the change
Inspection & Diff
git log
Show commit history
git log --oneline --graph --all
Compact branch graph of all refs — essential for orientation
git diff
Show unstaged changes vs the last commit
git diff --staged
Show staged changes that will go into the next commit
git show <hash>
Show details and diff for a specific commit
git blame <file>
Show who last changed each line of a file, and when
git shortlog -sn
Summary of commits per author, sorted by count
Branches
git branch
List local branches
git branch -a
List all local and remote-tracking branches
git branch <name>
Create a new branch at HEAD (does not switch to it)
git switch <name>
Switch to a branch (modern replacement for checkout)
git switch -c <name>
Create and switch to a new branch in one step
git merge <branch>
Merge named branch into the current branch
git rebase <branch>
Rebase current branch onto another — linear history
git branch -d <name>
Delete a merged branch (safe)
git branch -D <name>
Force-delete a branch even if unmerged
Remote Operations
git remote -v
List remotes with their URLs
git remote add origin <url>
Add a remote named origin
git fetch
Download new commits/refs from remote without merging
git pull
Fetch + merge (or rebase with --rebase) remote into current branch
git push -u origin <branch>
Push branch and set upstream tracking reference
git push origin --delete <branch>
Delete a remote branch
git push --tags
Push all local tags to the remote
Stash & Clean
git stash
Stash dirty working tree and index for later
git stash push -m "msg"
Stash with a descriptive label
git stash list
Show all stash entries
git stash pop
Apply and remove the most recent stash
git stash drop stash@{n}
Delete a specific stash entry
git clean -fd
Remove untracked files and directories (irreversible)
Undo & Reset
git revert <hash>
Create a new commit that undoes the given commit — safe for shared history
git reset --soft HEAD~1
Undo last commit, keep changes staged
git reset --mixed HEAD~1
Undo last commit, keep changes unstaged (default)
git reset --hard HEAD~1
Undo last commit and discard all changes — destructive
git reflog
Show a log of all HEAD movements — last resort recovery tool
git cherry-pick <hash>
Apply a single commit from another branch onto current branch
Tags
git tag
List all tags
git tag v1.0.0
Create a lightweight tag at HEAD
git tag -a v1.0.0 -m "msg"
Create an annotated tag with a message
git tag -d v1.0.0
Delete a local tag