Setup & Config
7 snippetsInitial configuration
Set Username
git config --global user.name "Your Name" Set Email
git config --global user.email "you@example.com" Set Default Editor
git config --global core.editor "code --wait" Set Default Branch
git config --global init.defaultBranch main View Config
git config --list Initialize Repo
git init Clone Repo
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git Basic Commands
6 snippetsEveryday operations
Check Status
git status Stage Files
git add file.txt # Single file
git add . # All changes
git add -p # Interactive Commit
git commit -m "message"
git commit -am "message" # Add + commit tracked View Log
git log
git log --oneline
git log --graph --oneline --all Show Diff
git diff # Unstaged changes
git diff --staged # Staged changes
git diff HEAD~1 # Last commit Remove File
git rm file.txt # Delete and stage
git rm --cached file.txt # Untrack but keep Branches
6 snippetsCreate and manage branches
List Branches
git branch # Local
git branch -r # Remote
git branch -a # All Create Branch
git branch feature-x
git checkout -b feature-x # Create and switch Switch Branch
git checkout main
git switch main # Git 2.23+ Rename Branch
git branch -m old-name new-name
git branch -m new-name # Current branch Delete Branch
git branch -d feature-x # Safe delete
git branch -D feature-x # Force delete Merge Branch
git checkout main
git merge feature-x Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Remote Operations
6 snippetsWork with remote repositories
Add Remote
git remote add origin https://github.com/user/repo.git View Remotes
git remote -v Fetch
git fetch origin # Get remote changes
git fetch --all # All remotes Pull
git pull # Fetch + merge
git pull --rebase # Fetch + rebase Push
git push origin main
git push -u origin main # Set upstream
git push --force # Force (careful!) Delete Remote Branch
git push origin --delete feature-x Undo & Reset
6 snippetsRevert and fix mistakes
Unstage File
git restore --staged file.txt
git reset HEAD file.txt # Older syntax Discard Changes
git restore file.txt
git checkout -- file.txt # Older syntax Amend Last Commit
git commit --amend -m "new message"
git commit --amend --no-edit # Keep message Reset to Commit
git reset --soft HEAD~1 # Keep changes staged
git reset --mixed HEAD~1 # Keep changes unstaged
git reset --hard HEAD~1 # Discard changes Revert Commit
git revert abc123 # Create undo commit Recover Lost Commit
git reflog
git checkout abc123 Stash
6 snippetsTemporarily save changes
Stash Changes
git stash
git stash -m "work in progress" List Stashes
git stash list Apply Stash
git stash apply # Keep stash
git stash pop # Apply and remove Apply Specific
git stash apply stash@{2} Drop Stash
git stash drop stash@{0}
git stash clear # Remove all Stash Untracked
git stash -u # Include untracked Rebase
5 snippetsRewrite commit history
Rebase onto Main
git checkout feature
git rebase main Interactive Rebase
git rebase -i HEAD~3 # Last 3 commits
# pick, squash, edit, drop, reword Continue Rebase
git rebase --continue Abort Rebase
git rebase --abort Squash Commits
# In interactive rebase:
# pick abc123 First commit
# squash def456 Second commit