git config --global user.name "name" | Set username |
git config --global user.email "email" | Set email |
git config --global core.editor "code --wait" | Set default editor |
git config --global init.defaultBranch main | Set default branch name |
git config --list | List all settings |
git config --global alias.co checkout | Create alias |
git init | Initialize new repository |
git clone <url> | Clone remote repository |
git clone <url> <dir> | Clone to specific directory |
git clone --depth 1 <url> | Shallow clone (latest commit only) |
git clone --branch <branch> <url> | Clone specific branch |
git status | Show working tree status |
git add <file> | Stage specific file |
git add . | Stage all changes |
git add -p | Stage changes interactively |
git commit -m "message" | Commit with message |
git commit -am "message" | Stage tracked files and commit |
git commit --amend | Modify last commit |
git commit --amend --no-edit | Add to last commit without editing |
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
git diff <branch1> <branch2> | Compare two branches |
git log | Show commit history |
git log --oneline | Compact commit history |
git log --graph --oneline | Show branch graph |
git log -p <file> | Show file change history |
git show <commit> | Show commit details |
git blame <file> | Show who changed each line |
git branch | List local branches |
git branch -a | List all branches |
git branch <name> | Create new branch |
git branch -d <name> | Delete branch (safe) |
git branch -D <name> | Force delete branch |
git branch -m <old> <new> | Rename branch |
git checkout <branch> | Switch to branch |
git checkout -b <name> | Create and switch to branch |
git switch <branch> | Switch to branch (modern) |
git switch -c <name> | Create and switch (modern) |
git merge <branch> | Merge branch into current |
git merge --no-ff <branch> | Merge with commit (no fast-forward) |
git merge --squash <branch> | Squash merge |
git rebase <branch> | Rebase onto branch |
git rebase -i HEAD~n | Interactive rebase last n commits |
git rebase --abort | Abort rebase |
git rebase --continue | Continue rebase after conflict |
git cherry-pick <commit> | Apply specific commit |
git remote -v | List remote repositories |
git remote add <name> <url> | Add remote repository |
git remote remove <name> | Remove remote |
git remote rename <old> <new> | Rename remote |
git remote set-url <name> <url> | Change remote URL |
git fetch | Download remote changes |
git fetch --all | Fetch from all remotes |
git pull | Fetch and merge |
git pull --rebase | Fetch and rebase |
git push | Push to remote |
git push -u origin <branch> | Push and set upstream |
git push --force | Force push (dangerous) |
git push --force-with-lease | Safe force push |
git push origin --delete <branch> | Delete remote branch |
git checkout -- <file> | Discard file changes |
git restore <file> | Discard file changes (modern) |
git restore --staged <file> | Unstage file |
git reset HEAD <file> | Unstage file (legacy) |
git reset --soft HEAD~1 | Undo commit, keep changes staged |
git reset --mixed HEAD~1 | Undo commit, keep changes unstaged |
git reset --hard HEAD~1 | Undo commit, discard changes |
git revert <commit> | Create commit that undoes changes |
git stash | Stash changes |
git stash push -m "message" | Stash with message |
git stash list | List stashes |
git stash pop | Apply and remove latest stash |
git stash apply | Apply latest stash (keep stash) |
git stash drop | Remove latest stash |
git stash clear | Remove all stashes |
git tag | List tags |
git tag <name> | Create lightweight tag |
git tag -a <name> -m "msg" | Create annotated tag |
git tag -d <name> | Delete local tag |
git push origin <tag> | Push tag to remote |
git push origin --tags | Push all tags |
git clean -n | Preview files to be removed |
git clean -fd | Remove untracked files and dirs |
git gc | Garbage collection |
git reflog | Show reference log |
git bisect start | Start binary search for bug |
git bisect good/bad | Mark commit good or bad |
git bisect reset | End bisect session |
git worktree list | List worktrees |
git worktree add <path> <branch> | Create worktree for branch |
git worktree add -b <new> <path> | Create new branch in worktree |
git worktree remove <path> | Remove worktree |
git worktree prune | Remove stale worktrees |
git submodule add <url> <path> | Add submodule |
git submodule init | Initialize submodules |
git submodule update | Update submodules |
git submodule update --init --recursive | Init and update all |
git clone --recurse-submodules <url> | Clone with submodules |
git submodule foreach git pull | Pull all submodules |
git submodule status | Show submodule status |
.git/hooks/pre-commit | Run before commit |
.git/hooks/commit-msg | Validate commit message |
.git/hooks/pre-push | Run before push |
.git/hooks/post-merge | Run after merge |
.git/hooks/pre-rebase | Run before rebase |
chmod +x .git/hooks/<hook> | Make hook executable |
.gitignore to exclude files from trackinggit diff --stagedgit stash to save work in progress