How We Use Git
Understanding version control and Git fundamentals for collaborative development
How We Use Git
If you're new to development, version control might seem confusing. This guide explains what Git is, why it matters, and how to use it daily.
Core idea: Version control is like "track changes" for code, but much more powerful.
What is Version Control?
Imagine you're writing a document. You might save versions like:
report_v1.docxreport_v2_final.docxreport_v2_final_ACTUAL.docx
This gets messy fast. Version control solves this by:
- Tracking every change automatically
- Letting you go back to any previous version
- Allowing multiple people to work simultaneously
- Showing who changed what and when
What is Git?
Git is the most popular version control system. It runs on your computer and tracks changes to files in a folder (called a "repository" or "repo").
Key Concepts
| Term | What It Means |
|---|---|
| Repository (Repo) | A folder tracked by Git |
| Commit | A saved snapshot of changes |
| Branch | A parallel version of the code |
| Merge | Combining branches together |
| Clone | Copying a repo to your computer |
| Pull | Getting latest changes from remote |
| Push | Sending your changes to remote |
How Git Works
Your Computer GitHub (Remote)
┌─────────────────┐ ┌─────────────────┐
│ Working Files │ │ │
│ ↓ │ │ Repository │
│ Staging Area │ push → │ │
│ ↓ │ ← pull │ (shared) │
│ Local Commits │ │ │
└─────────────────┘ └─────────────────┘
Basic Git Workflow
# 1. Check what's changed
git status
# 2. Stage changes you want to save
git add . # Add all changes
git add specific-file.js # Add one file
# 3. Commit with a message
git commit -m "Add login feature"
# 4. Push to GitHub
git push
Branches
Branches let you work on features without affecting the main code.
How Branches Work
main: A → B → C → D → E
↘
feature: X → Y → Z
↘
merge → F
Our Branch Strategy
| Branch | Purpose |
|---|---|
main | Production code, always deployable |
feature/xxx | New features in development |
fix/xxx | Bug fixes |
experiment/xxx | Trying new approaches |
Branch Commands
# Create and switch to new branch
git checkout -b feature/add-login
# Switch to existing branch
git checkout main
# See all branches
git branch
# Delete a branch
git branch -d feature/add-login
Common Situations
I Made Changes on Wrong Branch
# Stash your changes
git stash
# Switch to correct branch
git checkout correct-branch
# Apply stashed changes
git stash pop
I Need to Undo Last Commit
# Undo commit but keep changes
git reset --soft HEAD~1
# Undo commit and discard changes (DANGEROUS)
git reset --hard HEAD~1
I Have Merge Conflicts
When Git can't automatically merge:
- Open the conflicted file
- Look for conflict markers:
<<<<<<< HEAD your changes ======= their changes >>>>>>> branch-name - Edit to keep what you want
- Remove the markers
git add .andgit commit
I Want to See What Changed
# See uncommitted changes
git diff
# See commit history
git log --oneline
# See who changed a line
git blame filename.js
Best Practices
Commit Messages
| Good | Bad |
|---|---|
| "Add keyword export CSV functionality" | "update" |
| "Fix clustering timeout for large datasets" | "fix bug" |
| "Refactor API client error handling" | "changes" |
Commit Frequency
- Commit often: Small, logical chunks
- Push regularly: Don't lose work
- One thing per commit: Easier to review and revert
Quick Reference
| I want to... | Command |
|---|---|
| See status | git status |
| Save changes | git add . && git commit -m "message" |
| Upload to GitHub | git push |
| Get latest changes | git pull |
| Create branch | git checkout -b branch-name |
| Switch branch | git checkout branch-name |
| See history | git log --oneline |
| Undo last commit | git reset --soft HEAD~1 |