Basic Git Commands
| Command | Description |
|---|---|
git init | Initialize a new Git repository |
git clone <url> | Clone a remote repository |
git add <file> | Stage file for commit |
git add . | Stage all changes |
git commit -m "msg" | Commit staged changes |
git status | Show working tree status |
git log --oneline | View commit history (compact) |
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
Remote Operations
| Command | Description |
|---|---|
git remote add origin <url> | Add remote repository |
git push -u origin main | Push and set upstream |
git pull | Fetch and merge remote changes |
git fetch | Download remote changes without merging |
git remote -v | List remote repositories |
Branching & Merging
| Command | Description |
|---|---|
git branch | List branches |
git branch <name> | Create new branch |
git checkout <branch> | Switch to branch |
git switch <branch> | Switch to branch (modern) |
git merge <branch> | Merge branch into current |
git rebase <branch> | Rebase onto branch |
git branch -d <name> | Delete branch |
Stashing
| Command | Description |
|---|---|
git stash | Stash current changes |
git stash pop | Apply and remove last stash |
git stash list | List all stashes |
git stash drop | Delete last stash |
Undoing Changes
| Command | Description |
|---|---|
git restore <file> | Discard working directory changes |
git restore --staged <file> | Unstage a file |
git reset HEAD~1 | Undo last commit (keep changes) |
git reset --hard HEAD~1 | Undo last commit (discard changes) |
git revert <commit> | Create new commit undoing changes |