Shallow clone (only latest commit, much faster for large repos)
Basics
git status
Show working tree status (modified, staged, untracked files)
git add file.txt
Stage a specific file for commit
git add .
Stage all changes in current directory and subdirectories
git add -p
Interactively stage chunks of changes (patch mode)
git commit -m "Your commit message"
Commit staged changes with a message
git commit -am "Message"
Stage all tracked modified files and commit in one step
git commit --amend
Modify the last commit (message or add forgotten files)
git diff
Show unstaged changes (working directory vs staging area)
git diff --staged
Show staged changes (staging area vs last commit)
git diff HEAD
Show all changes since last commit (staged + unstaged)
git rm file.txt
Remove a file from working directory and stage the deletion
git mv old.txt new.txt
Rename/move a file and stage the change
Branching
git branch
List all local branches (current branch marked with *)
git branch -a
List all branches (local + remote)
git branch feature-name
Create a new branch (does not switch to it)
git checkout -b feature-name
Create and switch to a new branch
git switch feature-name
Switch to an existing branch (modern alternative to checkout)
git switch -c feature-name
Create and switch to a new branch (modern syntax)
git branch -d feature-name
Delete a branch (only if fully merged)
git branch -D feature-name
Force delete a branch (even if not merged) destructive
git branch -m old-name new-name
Rename a branch
switch vs checkout:git switch (added in Git 2.23) is specifically for switching branches. git checkout does more (file restore, detached HEAD). Use switch for clarity.
Merging & Rebasing
git merge feature-name
Merge feature-name into current branch
git merge --no-ff feature-name
Merge with a merge commit (even if fast-forward is possible)
git merge --squash feature-name
Squash all commits from feature into a single commit
git merge --abort
Abort a merge in progress (return to pre-merge state)
git rebase main
Rebase current branch onto main (replay commits on top)
git rebase --abort
Abort a rebase in progress
git rebase --continue
Continue rebase after resolving conflicts
git cherry-pick abc1234
Apply a specific commit from another branch to current branch
Rebase warning: Never rebase commits that have been pushed to a shared remote branch. Rebase rewrites commit history, which causes problems for other collaborators.
Download objects and refs from remote (does not merge)
git fetch --prune
Fetch and remove remote-tracking branches that no longer exist
git pull
Fetch and merge changes from remote (git fetch + git merge)
git pull --rebase
Fetch and rebase instead of merge (cleaner history)
git push
Push commits to the remote tracking branch
git push -u origin feature-name
Push and set upstream for a new branch
git push origin --delete feature-name
Delete a remote branch
git push --force-with-lease
Force push safely (fails if remote has new commits) use with care
Stashing
Temporarily save changes without committing. Useful when you need to switch branches.
git stash
Stash all modified tracked files
git stash -u
Stash including untracked files
git stash save "work in progress on login"
Stash with a descriptive message
git stash list
List all stashes
git stash pop
Apply the most recent stash and remove it from the list
git stash apply stash@{2}
Apply a specific stash without removing it
git stash drop stash@{0}
Delete a specific stash
git stash clear
Delete all stashes destructive
git stash show -p stash@{0}
Show the diff of a specific stash
Undoing Changes
git restore file.txt
Discard changes in working directory (restore to last commit) destructive
git restore --staged file.txt
Unstage a file (keep changes in working directory)
git reset HEAD~1
Undo last commit, keep changes staged
git reset --soft HEAD~1
Undo last commit, keep changes staged (same as above)
git reset --mixed HEAD~1
Undo last commit, unstage changes (keep in working directory)
git reset --hard HEAD~1
Undo last commit and discard all changes destructive
git revert abc1234
Create a new commit that undoes a specific commit (safe for shared branches)
git clean -fd
Remove untracked files and directories destructive
git clean -fdn
Dry run: show what would be removed (add -n flag)
git reflog
Show history of HEAD changes (use to recover lost commits)
Lost a commit?git reflog shows every place HEAD has been. Find the commit hash you need, then git checkout abc1234 or git reset --hard abc1234 to recover it. Reflog entries expire after 90 days.
reset vs revert: Use git revert on shared/pushed branches (it creates a new commit). Use git reset on local-only branches (it rewrites history).
Log & History
git log
Show commit history
git log --oneline
Compact log (one line per commit)
git log --oneline --graph --all
Visual branch graph of all branches
git log -n 10
Show only the last 10 commits
git log --author="name"
Filter commits by author
git log --since="2 weeks ago"
Show commits from the last 2 weeks
git log --grep="fix"
Search commit messages for "fix"
git log -p file.txt
Show full diff history for a specific file
git log --stat
Show files changed in each commit with insertions/deletions
git blame file.txt
Show who last modified each line of a file
git show abc1234
Show details and diff of a specific commit
git shortlog -sn
Show commit count per author (leaderboard)
Tags & Releases
git tag
List all tags
git tag v1.0.0
Create a lightweight tag at current commit
git tag -a v1.0.0 -m "Release version 1.0.0"
Create an annotated tag with a message
git tag -a v1.0.0 abc1234
Tag a specific past commit
git push origin v1.0.0
Push a specific tag to remote
git push origin --tags
Push all tags to remote
git tag -d v1.0.0
Delete a local tag
Advanced Commands
git bisect start
Start binary search for the commit that introduced a bug
git bisect good abc1234
Mark a known good commit
git bisect bad
Mark the current commit as bad
git bisect reset
End bisect session and return to original branch
git worktree add ../feature-branch feature-branch
Check out a branch in a separate directory (work on two branches simultaneously)