Setup
Configure identity and view configuration.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --list
git config user.name
git config user.email
Create / Get a Repo
git init # Initialize new repo in current dir
git init <dir> # Initialize in a new directory
git clone <url> # Clone via HTTPS/SSH
git clone --depth 1 <url> # Shallow clone for speed
Status & Inspection
git status
git log # Full log
git log --oneline --graph --decorate --all
git log -p # Show diffs per commit
git show <commit> # Show details of a commit
git diff # Working tree vs index
git diff --staged # Index vs HEAD
Add / Stage Files
Classic and modern equivalents.
# Classic
git add <file>
git add . # Track all changes in current dir
git add -A # Track all changes repo-wide (incl. deletes)
git reset <file> # Unstage a file (leave working changes)
# Modern
git restore --staged <file> # Unstage (modern)
git restore <file> # Discard working changes to file
Commit
git commit -m "message"
git commit -am "message" # Stage tracked files and commit
git commit --amend # Edit last commit (message + content)
git commit --amend --no-edit # Amend content only, keep message
Branching & Switching
# Classic
git branch # List branches
git branch <name> # Create branch
git checkout <branch> # Switch
git checkout -b <new-branch> # Create + switch
# Modern
git switch <branch> # Switch branches
git switch -c <new-branch> # Create + switch
# Delete
git branch -d <branch> # Safe delete (merged only)
git branch -D <branch> # Force delete
Merging & Rebasing
git merge <branch> # Merge into current branch
git merge --no-ff <branch> # Always create a merge commit
git rebase <base> # Rebase current branch onto <base>
git rebase -i <base> # Interactive rebase (squash, reorder)
git rebase --continue
git rebase --abort
Remote Repositories
git remote -v
git remote add origin <url>
git remote remove <name>
git remote rename <old> <new>
Fetch / Pull / Push
git fetch # Update remote-tracking branches
git fetch --all --prune
git pull # fetch + merge
git pull --rebase # fetch + rebase (cleaner history)
git push origin <branch>
git push -u origin <branch> # Set upstream
git push # After upstream is set
git push --force-with-lease # Safer force push
Stash
git stash # Stash tracked changes
git stash -u # Include untracked
git stash list
git stash show -p stash@{0}
git stash pop # Apply latest and drop
git stash apply stash@{n} # Apply without dropping
git stash drop stash@{n}
git stash clear # Remove all stashes
Undo / Recovery
# Unstage or discard
git restore --staged <file> # Unstage
git restore <file> # Discard working changes to file
# Reset (move HEAD)
git reset --soft <commit> # Keep index & working tree
git reset --mixed <commit> # Default; keep working tree, unstage
git reset --hard <commit> # WARNING: discard all changes
# Revert (safe public undo)
git revert <commit> # Create a new commit that undoes <commit>
# Restore file from a commit
git checkout <commit> -- <path> # Classic
git restore --source=<commit> <path>
# Reflog (find lost commits/branches)
git reflog
git reset --hard HEAD@{1}
Ignoring Files
# .gitignore examples
node_modules/
*.log
.env
# Apply ignore to already tracked files
git rm -r --cached .
git add .
git commit -m "Apply .gitignore"
Searching
git grep "pattern" # Search tracked files
git grep -n "pattern" # Show line numbers
git log -S "string" # Commits that add/remove a string
git log -G "regex" # Commits with regex matches in diffs
Submodules
git submodule add <url> <path>
git submodule update --init --recursive
git clone --recursive <url>
git submodule foreach git pull origin main
Common Workflows
# Start a feature branch
git switch -c feature/cool-thing
git add -A
git commit -m "Implement cool thing"
git push -u origin feature/cool-thing
# Sync with main
git fetch origin
git switch main
git pull --rebase
git switch feature/cool-thing
git rebase main
git rebase --continue
git push --force-with-lease
# Hotfix on main
git switch main
git pull
git add -A
git commit -m "Hotfix: correct production bug"
git push
git tag -a v1.0.1 -m "Hotfix release"
git push origin v1.0.1
Safety Notes
- Prefer
git revertovergit reset --hardon shared/public branches. - Use
git push --force-with-leaseinstead of--forceto avoid overwriting others’ work. - Run
git fetch --all --pruneregularly to keep remote-tracking branches tidy.