Git Commands Cheatsheet
Essential Git commands for version control: setup, commits, branches, merging, and remote operations.
| Command | Description | Syntax | Example | Category |
|---|---|---|---|---|
| git init | Initialize a new git repository | git init [project-name] | git init my-project | Setup |
| git clone | Clone a repository | git clone <url> | git clone https://github.com/user/repo.git | Setup |
| git config | Set git configuration | git config --global user.name "Name" | git config --global user.email "user@example.com" | Setup |
| git status | Check repository status | git status | git status | Local |
| git add | Stage files for commit | git add <files> | git add . git add file.js | Local |
| git commit | Commit staged changes | git commit -m "message" | git commit -m "Add new feature" | Local |
| git log | View commit history | git log [options] | git log --oneline --graph | History |
| git diff | Show changes between commits | git diff [branch1] [branch2] | git diff main develop | History |
| git restore | Restore working tree files | git restore <file> | git restore file.js | Local |
| git reset | Reset changes | git reset [--hard|--soft] [HEAD~n] | git reset --hard HEAD~1 | History |
| git revert | Create a new commit reverting changes | git revert <commit> | git revert abc123 | History |
| git branch | Manage branches | git branch [branch-name] | git branch feature/new-feature | Branches |
| git checkout | Switch branches | git checkout <branch> | git checkout develop | Branches |
| git switch | Switch branches (new syntax) | git switch <branch> | git switch main | Branches |
| git merge | Merge branches | git merge <branch> | git merge feature/new-feature | Branches |
| git rebase | Rebase commits onto another branch | git rebase <branch> | git rebase main | Advanced |
| git stash | Save uncommitted changes | git stash [save "message"] | git stash save "WIP" | Local |
| git stash pop | Restore stashed changes | git stash pop | git stash pop | Local |
| git push | Push commits to remote | git push <remote> <branch> | git push origin main | Remote |
| git pull | Fetch and merge remote changes | git pull <remote> <branch> | git pull origin main | Remote |
| git fetch | Fetch remote changes | git fetch <remote> | git fetch origin | Remote |
| git remote | Manage remote repositories | git remote [add|remove|set-url] | git remote add upstream https://github.com/upstream/repo.git | Remote |
| git tag | Create version tags | git tag <tag-name> | git tag v1.0.0 | Advanced |
| git cherry-pick | Apply specific commits | git cherry-pick <commit> | git cherry-pick abc123 | Advanced |
| git clean | Remove untracked files | git clean -fd | git clean -fd | Local |