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.docx
  • report_v2_final.docx
  • report_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

TermWhat It Means
Repository (Repo)A folder tracked by Git
CommitA saved snapshot of changes
BranchA parallel version of the code
MergeCombining branches together
CloneCopying a repo to your computer
PullGetting latest changes from remote
PushSending 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

BranchPurpose
mainProduction code, always deployable
feature/xxxNew features in development
fix/xxxBug fixes
experiment/xxxTrying 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:

  1. Open the conflicted file
  2. Look for conflict markers:
    <<<<<<< HEAD
    your changes
    =======
    their changes
    >>>>>>> branch-name
    
  3. Edit to keep what you want
  4. Remove the markers
  5. git add . and git 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

GoodBad
"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 statusgit status
Save changesgit add . && git commit -m "message"
Upload to GitHubgit push
Get latest changesgit pull
Create branchgit checkout -b branch-name
Switch branchgit checkout branch-name
See historygit log --oneline
Undo last commitgit reset --soft HEAD~1

Resources