What is Git?

Git is a distributed version control system that tracks changes in your code over time. It's like a time machine for your project - you can save snapshots (commits) of your code and go back to any point in history.

Think of Git as Google Docs version history, but much more powerful. Instead of just "Save" and "Undo", you get complete control over your code's history, can work on multiple features simultaneously, and collaborate with teams without overwriting each other's work.

Why Git Matters

  • Track Changes: Know exactly what changed, when, and why
  • Undo Mistakes: Easily revert to any previous version of your code
  • Collaborate: Multiple developers can work on the same project without conflicts
  • Branching: Work on new features without affecting the main code
  • Backup: Your code is stored remotely (GitHub/GitLab), never lose work
  • Industry Standard: Every tech company uses Git, essential skill
  • Open Source: Contribute to millions of projects on GitHub
  • Portfolio: Your GitHub profile is your developer resume

Git vs GitHub: Understanding the Difference

Git (Local)
- Software installed on your computer
- Tracks changes locally
- Works offline
- Created by Linus Torvalds (Linux creator)

GitHub (Remote)
- Cloud platform for hosting Git repositories
- Share code with others
- Collaboration features (pull requests, issues)
- Social coding (follow developers, star projects)
- Owned by Microsoft

Analogy:
Git = Microsoft Word (software on your computer)
GitHub = Google Drive (cloud storage and sharing)

Alternatives to GitHub:
- GitLab (open source, self-hosted option)
- Bitbucket (free private repos)
- Azure DevOps (Microsoft)
- Gitea (self-hosted)

Core Concepts: Repository, Commits, Branches

Repository (Repo)
- Folder containing your project + Git tracking
- Local repo: On your computer
- Remote repo: On GitHub/GitLab

Commit
- Snapshot of your code at a specific point
- Like saving a game checkpoint
- Contains: changes, message, author, timestamp

Branch
- Independent line of development
- Like parallel universes of your code
- Default branch: main (or master)

Working Directory → Staging Area → Repository

┌─────────────────┐
│ Working Dir     │  Your files (make changes here)
│ (modified)      │
└────────┬────────┘
         │ git add
         ↓
┌─────────────────┐
│ Staging Area    │  Files ready to commit
│ (staged)        │
└────────┬────────┘
         │ git commit
         ↓
┌─────────────────┐
│ Repository      │  Permanent history
│ (.git folder)   │
└─────────────────┘

Commit History (timeline):
main: A ← B ← C ← D (latest)
      ↓
   feature: E ← F (working on new feature)

Essential Git Commands

// SETUP (one-time configuration)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --list  // View settings

// CREATING REPOSITORIES

// Create new repository
git init
→ Creates .git folder in current directory

// Clone existing repository
git clone https://github.com/user/repo.git
→ Downloads remote repo to your computer

// BASIC WORKFLOW

// Check status
git status
→ See modified files, staged changes

// Stage files (prepare for commit)
git add index.html        // Add specific file
git add .                 // Add all changes
git add *.js              // Add all .js files

// Commit changes (save snapshot)
git commit -m "Add login feature"
→ Save staged changes with message

// Shortcut: stage and commit
git commit -am "Fix bug in header"
→ Only works for modified files (not new files)

// View commit history
git log
git log --oneline         // Compact view
git log --graph --all     // Visual tree

// WORKING WITH REMOTES

// View remote repositories
git remote -v

// Add remote
git remote add origin https://github.com/user/repo.git

// Push changes to remote
git push origin main
→ Upload commits to GitHub

// Pull changes from remote
git pull origin main
→ Download and merge remote changes

// Fetch (download without merging)
git fetch origin

// UNDOING CHANGES

// Discard changes in working directory
git checkout -- file.txt
→ Restore file to last commit

// Unstage file (keep changes)
git reset file.txt
→ Remove from staging area

// Undo last commit (keep changes)
git reset --soft HEAD~1
→ Moves back 1 commit, changes stay staged

// Undo last commit (discard changes)
git reset --hard HEAD~1
→ WARNING: Permanently deletes changes!

// Revert commit (safe way)
git revert abc123
→ Creates new commit that undoes previous commit

Branching: Parallel Development

Branches allow you to work on features independently without affecting the main codebase. Think of them as alternate timelines.

// VIEW BRANCHES
git branch              // List local branches
git branch -a           // List all (local + remote)

// CREATE BRANCH
git branch feature-login
→ Creates new branch (doesn't switch to it)

git checkout -b feature-login
→ Create and switch to new branch (shortcut)

git switch -c feature-login
→ Modern way (Git 2.23+)

// SWITCH BRANCHES
git checkout main
git switch main         // Modern way

// MERGE BRANCHES
// Scenario: Merge feature into main

git checkout main       // Switch to main
git merge feature-login // Merge feature into main

// DELETE BRANCH
git branch -d feature-login      // Delete (safe, checks if merged)
git branch -D feature-login      // Force delete

// TYPICAL WORKFLOW

1. Create feature branch from main
   git checkout main
   git pull origin main
   git checkout -b feature-payment

2. Work on feature (make commits)
   git add .
   git commit -m "Add payment integration"

3. Push feature branch to remote
   git push origin feature-payment

4. Create Pull Request on GitHub
   (Review → Approve → Merge)

5. Delete feature branch
   git branch -d feature-payment

// BRANCH NAMING CONVENTIONS
feature/user-authentication
bugfix/header-alignment
hotfix/security-patch
release/v1.2.0

Merge Conflicts: Resolution Guide

Merge conflicts occur when Git can't automatically merge changes because the same lines were modified in both branches.

// Conflict example
git merge feature-branch
→ CONFLICT (content): Merge conflict in index.html
→ Automatic merge failed; fix conflicts and then commit

// Conflicted file looks like this:
<<<<<<< HEAD (current branch)

Welcome to Our Site

=======

Welcome to My Website

>>>>>>> feature-branch // How to resolve: 1. Open the conflicted file 2. Decide which version to keep (or combine) 3. Remove conflict markers (<<<, ===, >>>) 4. Save the file 5. Stage and commit // After fixing: git add index.html git commit -m "Resolve merge conflict in header" // Abort merge (go back) git merge --abort // PREVENTING CONFLICTS 1. Pull often git pull origin main 2. Keep branches small and short-lived Merge frequently 3. Communicate with team Don't work on same files simultaneously 4. Use .gitignore Don't track generated files (node_modules, .env)

GitHub Workflow: Pull Requests

// FORKING WORKFLOW (Open Source)

1. Fork repository on GitHub
   → Creates your own copy

2. Clone your fork
   git clone https://github.com/YOUR-USERNAME/repo.git

3. Add upstream remote
   git remote add upstream https://github.com/ORIGINAL/repo.git

4. Create feature branch
   git checkout -b fix-typo

5. Make changes and commit
   git add .
   git commit -m "Fix typo in README"

6. Push to your fork
   git push origin fix-typo

7. Create Pull Request on GitHub
   → Compare: ORIGINAL:main ← YOUR-FORK:fix-typo

8. Wait for review and approval

9. After merge, sync your fork
   git checkout main
   git pull upstream main
   git push origin main

// BRANCH WORKFLOW (Team Projects)

1. Clone repository
   git clone https://github.com/company/project.git

2. Create feature branch
   git checkout -b feature-search

3. Work and commit
   git add .
   git commit -m "Add search functionality"

4. Push to remote
   git push origin feature-search

5. Create Pull Request
   → Compare: main ← feature-search
   → Add description, screenshots, tests

6. Code Review
   → Team reviews your code
   → Request changes or approve

7. Merge Pull Request
   → Squash and merge (clean history)
   → Merge commit (keep all commits)
   → Rebase and merge (linear history)

8. Delete branch
   git branch -d feature-search
   git push origin --delete feature-search

// PULL REQUEST BEST PRACTICES
- Small, focused changes (easier to review)
- Clear, descriptive title
- Detailed description (what, why)
- Link related issues
- Add screenshots for UI changes
- Write tests
- Keep commits clean and meaningful

Git Ignore: What Not to Track

// .gitignore file (place in root directory)
// Tell Git which files to ignore

# Dependencies
node_modules/
vendor/

# Environment variables
.env
.env.local
config.local.js

# Build outputs
dist/
build/
*.min.js
*.min.css

# IDE files
.vscode/
.idea/
*.swp
*.swo

# OS files
.DS_Store
Thumbs.db

# Logs
*.log
logs/

# Database
*.sqlite
*.db

# Temporary files
*.tmp
temp/

// Example .gitignore for Node.js project
node_modules/
.env
dist/
.DS_Store
*.log

// How to use:
1. Create .gitignore in project root
2. Add patterns for files to ignore
3. Commit .gitignore
   git add .gitignore
   git commit -m "Add gitignore"

// Already tracked files?
// Remove from Git but keep locally
git rm --cached .env

// Global .gitignore (for all projects)
git config --global core.excludesfile ~/.gitignore_global

Git Best Practices

  • Commit often: Small, focused commits are better than large ones
  • Write good commit messages: "Fix login bug" not "fixes"
  • Pull before you push: Always sync with remote first
  • Use branches: Never work directly on main
  • Review before committing: Use git diff to check changes
  • Don't commit secrets: Use .env files and .gitignore
  • Keep commits atomic: One feature/fix per commit
  • Use meaningful branch names: feature/user-auth not feature1
  • Delete merged branches: Keep repository clean
  • Learn to resolve conflicts: Essential team skill

Common Git Workflows

// DAILY WORKFLOW

// Morning - Start work
git checkout main
git pull origin main
git checkout -b feature-new-button

// During day - Save work
git add .
git commit -m "Add button component"
git push origin feature-new-button

// End of day - Push changes
git add .
git commit -m "Style button and add tests"
git push origin feature-new-button

// Next day - Continue work
git checkout feature-new-button
git pull origin main    // Get latest main changes
git merge main          // Merge into your branch
// Continue working...

// Feature complete - Create PR
// On GitHub: Create Pull Request
// After approval and merge:
git checkout main
git pull origin main
git branch -d feature-new-button

// EMERGENCY FIX (Hotfix)

git checkout main
git pull origin main
git checkout -b hotfix-critical-bug
// Fix the bug
git add .
git commit -m "Fix critical bug in payment"
git push origin hotfix-critical-bug
// Create PR, merge immediately
git checkout main
git pull origin main

// STASHING (Save work temporarily)

// You're working on feature but need to switch branches
git stash              // Save changes temporarily
git checkout main      // Switch branch
// Do urgent work
git checkout feature   // Back to feature
git stash pop          // Restore saved changes

// View stashes
git stash list

// Apply specific stash
git stash apply stash@{0}

Git Commit Message Guidelines

// GOOD COMMIT MESSAGES

feat: Add user authentication with JWT
fix: Resolve memory leak in image upload
docs: Update API documentation for v2
style: Format code with Prettier
refactor: Simplify payment processing logic
test: Add unit tests for user service
chore: Update dependencies

// BAD COMMIT MESSAGES

update
fix
changes
asdasd
WIP (Work In Progress)

// CONVENTIONAL COMMITS FORMAT

(): 



Example: feat(auth): Add password reset functionality Implement password reset via email with token validation. Token expires after 1 hour. Closes #123 Types: feat: New feature fix: Bug fix docs: Documentation style: Formatting (no code change) refactor: Code restructuring test: Adding tests chore: Build/config changes // COMMIT MESSAGE TIPS ✓ Use imperative mood: "Add feature" not "Added feature" ✓ First line under 50 characters ✓ Add detailed description if needed ✓ Reference issue numbers: "Fixes #123" ✗ Don't commit commented code ✗ Don't commit WIP (work in progress)

GitHub Profile: Your Developer Portfolio

// Your GitHub profile is your resume

What recruiters look at:
1. Contribution graph (green squares)
2. Pinned repositories (showcase best work)
3. README profile (introduce yourself)
4. Commit frequency and consistency
5. Code quality in repos
6. Documentation (README files)
7. Open source contributions

How to build a strong profile:

1. Create README.md for each project
   - What it does
   - Technologies used
   - How to install/run
   - Screenshots/demo
   - Live link

2. Pin your best 6 repositories
   - Show variety of skills
   - Complete, polished projects

3. Create profile README
   - Create repo: username/username
   - Add README.md with:
     * Bio/Introduction
     * Skills/Technologies
     * Projects
     * Contact info
     * GitHub stats badges

4. Contribute consistently
   - Better 1 commit/day than 50 once/month
   - Shows dedication and habit

5. Contribute to open source
   - Find "good first issue" labels
   - Documentation improvements
   - Bug fixes

6. Add topics/tags to repos
   - javascript, react, nodejs
   - Makes projects discoverable

7. Write good documentation
   - Clear README
   - Code comments
   - API documentation

Master Git for Professional Development

Our Full Stack JavaScript program includes Git from basics to advanced workflows. Learn industry-standard practices for version control and collaboration.

Explore JavaScript Program

Related Articles