Git & GitHub

Version Control, Branching Strategies & Collaboration Workflows

Git Basics

Git is a distributed version control system that tracks changes in source code during software development.

Bash
# Configuration
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
git config --global init.defaultBranch main

# Initialize repository
git init                    # New repository
git clone <url>            # Clone existing repository

# Basic workflow
git status                  # Check status
git add <file>             # Stage specific file
git add .                   # Stage all changes
git add -p                  # Interactive staging

git commit -m "message"     # Commit with message
git commit -am "message"    # Stage tracked files and commit

# Viewing history
git log                     # Full log
git log --oneline           # Compact log
git log --graph --oneline   # Visual branch history
git log -p                  # With diffs
git log --author="name"     # By author

# Undo changes
git checkout -- <file>     # Discard working directory changes
git restore <file>         # Same as above (newer syntax)
git reset HEAD <file>      # Unstage file
git reset --soft HEAD~1     # Undo last commit, keep changes staged
git reset --hard HEAD~1     # Undo last commit, discard changes

# Diff
git diff                    # Working directory vs staged
git diff --staged           # Staged vs last commit
git diff HEAD~2             # Last 2 commits

Branching

Bash
# Branch operations
git branch                  # List local branches
git branch -a               # List all branches (including remote)
git branch <name>          # Create branch
git branch -d <name>       # Delete branch (merged)
git branch -D <name>       # Force delete branch

# Switching branches
git checkout <branch>      # Switch to branch
git checkout -b <branch>   # Create and switch
git switch <branch>        # New syntax for switching
git switch -c <branch>     # Create and switch (new syntax)

# Merging
git merge <branch>         # Merge branch into current
git merge --no-ff <branch> # Create merge commit even for fast-forward

# Resolve merge conflicts
# 1. Edit conflicted files
# 2. git add <resolved-files>
# 3. git commit

# Rebasing
git rebase <branch>        # Rebase current onto branch
git rebase -i HEAD~3       # Interactive rebase last 3 commits

# Cherry-pick
git cherry-pick <commit>   # Apply specific commit to current branch

Branch Naming Conventions

TypePatternExample
Featurefeature/<name>feature/user-auth
Bug Fixfix/<name>fix/login-error
Hotfixhotfix/<name>hotfix/security-patch
Releaserelease/<version>release/v1.2.0

Remote Operations

Bash
# Remote management
git remote -v               # List remotes
git remote add origin <url>
git remote remove origin
git remote set-url origin <new-url>

# Fetching and pulling
git fetch                   # Download changes (no merge)
git fetch --prune           # Remove deleted remote branches
git pull                    # Fetch + merge
git pull --rebase           # Fetch + rebase

# Pushing
git push                    # Push to tracked branch
git push origin <branch>   # Push specific branch
git push -u origin <branch> # Set upstream and push
git push --force            # Force push (be careful!)
git push --force-with-lease # Safer force push

# Working with remote branches
git checkout -b local-branch origin/remote-branch
git branch --set-upstream-to=origin/branch

# Tags
git tag                     # List tags
git tag v1.0.0              # Create lightweight tag
git tag -a v1.0.0 -m "msg"  # Annotated tag
git push origin v1.0.0      # Push specific tag
git push origin --tags      # Push all tags

GitHub Features

Pull Requests

  • Create PR: Compare changes between branches
  • Review: Comment on specific lines
  • Approve/Request Changes: Formal review
  • Merge Options: Merge commit, Squash, Rebase

GitHub Actions (CI/CD)

YAML
# .github/workflows/ci.yml
name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v3
      
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
          
      - name: Install dependencies
        run: npm ci
        
      - name: Run tests
        run: npm test
        
      - name: Build
        run: npm run build

GitHub CLI

Bash
# Install: https://cli.github.com/
gh auth login               # Authenticate

# Repository
gh repo create              # Create repo
gh repo clone owner/repo    # Clone repo
gh repo view                # View repo info

# Pull Requests
gh pr create                # Create PR
gh pr list                  # List PRs
gh pr checkout <number>    # Checkout PR
gh pr merge                 # Merge PR
gh pr review                # Review PR

# Issues
gh issue create             # Create issue
gh issue list               # List issues
gh issue close <number>

Git Workflows

GitHub Flow (Simple)

  1. Create branch from main
  2. Make changes and commit
  3. Open Pull Request
  4. Review and discuss
  5. Merge to main
  6. Deploy

Git Flow (Complex)

  • main - Production-ready code
  • develop - Integration branch
  • feature/* - New features
  • release/* - Release preparation
  • hotfix/* - Production fixes
Best Practices:
• Commit often with meaningful messages
• Keep commits atomic (one change per commit)
• Write descriptive PR descriptions
• Use branch protection rules
• Review code before merging

Advanced Git

Bash
# Stashing
git stash                   # Stash current changes
git stash save "message"    # Stash with message
git stash list              # List stashes
git stash pop               # Apply and remove latest stash
git stash apply             # Apply without removing
git stash drop              # Remove latest stash

# Interactive rebase
git rebase -i HEAD~3
# Commands: pick, reword, edit, squash, fixup, drop

# Bisect (find bug-introducing commit)
git bisect start
git bisect bad              # Current commit is bad
git bisect good <commit>   # Known good commit
# Git will checkout commits for you to test
git bisect reset            # When done

# Reflog (recover lost commits)
git reflog                  # Show all HEAD movements
git checkout HEAD@{2}       # Go to specific state

# Submodules
git submodule add <url> <path>
git submodule update --init --recursive

# Worktrees (multiple working directories)
git worktree add ../feature-branch feature-branch
git worktree list
git worktree remove ../feature-branch

# Aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.lg "log --oneline --graph"