Git and GitHub¶
Git is a distributed version control system. Every developer has full repo history. GitHub hosts remote repos and enables collaboration.
Configuration¶
git config --global user.name "Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
Basic Workflow¶
git init # New repo
git clone <url> # Clone existing
git status # Modified/staged/untracked
git add file.txt # Stage specific file
git add . # Stage all (careful)
git commit -m "Add feature" # Commit staged changes
Staging Area¶
Viewing Changes¶
git diff # Unstaged changes
git diff --staged # Staged changes
git diff branch1 branch2 # Between branches
git log --oneline --graph # Visual history
Undoing¶
git restore --staged file # Unstage (keep changes)
git restore file # Discard working changes
git commit --amend # Fix last commit
git revert <hash> # New commit undoing changes
git reset --soft HEAD~1 # Undo commit, keep staged
git reset --mixed HEAD~1 # Undo commit, keep in working dir
git reset --hard HEAD~1 # Undo commit, discard everything (dangerous!)
Branches¶
git branch feature # Create
git switch feature # Switch
git switch -c feature # Create + switch
git branch -d feature # Delete (safe)
git branch -D feature # Force delete
Merging¶
Fast-forward: main has no new commits, pointer moves forward (no merge commit). Three-way merge: both branches diverged, creates merge commit.
Conflict Resolution¶
Manually edit, remove markers,git add, git commit. Rebase¶
| Merge | Rebase | |
|---|---|---|
| History | Non-linear (preserves) | Linear (cleaner) |
| Safety | Safe (no rewrite) | Dangerous for shared branches |
| When | Public/shared | Local/private before merge |
Never rebase pushed commits on shared branches.
Cherry-Pick and Stash¶
git cherry-pick <hash> # Apply specific commit
git stash # Save uncommitted changes
git stash pop # Restore most recent
git stash list # List all stashes
.gitignore¶
Add before first commit. Already-tracked files need git rm --cached to untrack.
GitHub Remote¶
git remote add origin <url>
git push -u origin main # Push + set tracking
git push # Push to tracked
git pull # Fetch + merge
git fetch # Download without merging
Pull Requests¶
- Create feature branch, push to remote
- Open PR on GitHub (base: main, compare: feature)
- Code review, CI checks
- Merge (merge commit, squash, or rebase)
Fork Workflow¶
- Fork repo -> clone your fork
- Add upstream remote
- Branch, commit, push to fork
- Open PR to original
Commit Messages¶
Types: feat:, fix:, refactor:, style:, docs:, test:, chore:
Gotchas¶
- Committing node_modules: always .gitignore before first commit
- Force push:
git push --forceoverwrites remote history - dangerous on shared branches - Detached HEAD: checkout a commit directly puts you in detached state; create branch to save work
- Large files: git stores full history; use Git LFS for binary files
- Sensitive data committed: even after removal, exists in history; use
git filter-repoor BFG
See Also¶
- npm and task runners - .gitignore for node_modules
- frontend build systems - CI/CD build triggers
- [[linux-cli]] - Terminal commands for git workflows