Git and GitHub

Introduction

Git is a distributed version control system designed to track changes in source code during software development. GitHub is a platform for hosting Git repositories with additional collaboration features.

Key Features:

  • Distributed version control
  • Branching and merging
  • Code review tools
  • Issue tracking
  • Project management
  • CI/CD integration
  • Collaboration tools
  • Documentation hosting

Initial Setup

# Configure Git globally
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor "code --wait"
git config --global init.defaultBranch main

# Generate SSH key
ssh-keygen -t ed25519 -C "your.email@example.com"

# Start new repository
git init
git add .
git commit -m "Initial commit"

# Connect to GitHub
git remote add origin git@github.com:username/repo.git
git push -u origin main

Basic Workflow

Daily Commands

# Check status
git status

# Stage changes
git add file.txt
git add .  # Stage all changes

# Commit changes
git commit -m "Add new feature"
git commit -am "Update documentation"  # Stage and commit

# View history
git log
git log --oneline
git log --graph --oneline --all

# Sync with remote
git fetch origin
git pull origin main
git push origin main

# Undo changes
git checkout -- file.txt  # Discard changes
git reset HEAD file.txt  # Unstage changes
git revert commit-hash  # Revert commit
git reset --hard HEAD~1  # Delete last commit

File Operations

# Track/untrack files
git rm --cached file.txt  # Untrack file
git mv old.txt new.txt    # Rename file

# Ignore files
echo "*.log" >> .gitignore
echo "node_modules/" >> .gitignore
echo "build/" >> .gitignore

# Show changes
git diff                  # Working directory
git diff --staged        # Staged changes
git diff branch1..branch2 # Between branches
git blame file.txt       # Line-by-line history

Branching & Merging

Branch Management

# Create branch
git branch feature/login
git checkout -b feature/login  # Create and switch
git switch -c feature/login    # Modern syntax

# List branches
git branch
git branch -a  # Show remote branches
git branch -v  # Show last commit

# Switch branches
git checkout main
git switch main  # Modern syntax

# Delete branch
git branch -d feature/done
git branch -D feature/force  # Force delete

# Merge branches
git checkout main
git merge feature/login
git merge --no-ff feature/login  # Create merge commit

# Handle conflicts
git mergetool
git commit  # After resolving conflicts

Advanced Branching

# Rebase branch
git checkout feature
git rebase main

# Interactive rebase
git rebase -i HEAD~3

# Cherry-pick commits
git cherry-pick commit-hash

# Create tag
git tag v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0

# Stash changes
git stash
git stash push -m "WIP: feature"
git stash list
git stash pop
git stash apply stash@{0}

Advanced Git

History Rewriting

# Amend commit
git commit --amend
git commit --amend --no-edit

# Squash commits
git rebase -i HEAD~3
# Change 'pick' to 'squash' or 's'

# Reset branch
git reset --soft HEAD~1   # Keep changes staged
git reset --mixed HEAD~1  # Keep changes unstaged
git reset --hard HEAD~1   # Discard changes

# Clean repository
git clean -n  # Dry run
git clean -fd # Force delete untracked
git clean -fx # Delete ignored files

Submodules & Worktrees

# Submodules
git submodule add repo-url path
git submodule update --init --recursive
git submodule update --remote

# Worktrees
git worktree add ../path branch
git worktree list
git worktree remove ../path

# Bisect
git bisect start
git bisect bad
git bisect good v1.0.0
git bisect reset

GitHub Features

Pull Requests

# Pull Request Template
## Description
Describe the changes and motivation

## Type of change
- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update

## Checklist
- [ ] Tests added/updated
- [ ] Documentation updated
- [ ] Code follows style guidelines
- [ ] All tests passing
- [ ] No new warnings

## Screenshots
If applicable, add screenshots

## Related Issues
Fixes #123

GitHub CLI

# Authentication
gh auth login

# Repository
gh repo create
gh repo clone owner/repo
gh repo view

# Pull Requests
gh pr create
gh pr checkout number
gh pr review number
gh pr merge number

# Issues
gh issue create
gh issue list
gh issue view number
gh issue close number

# Releases
gh release create v1.0.0
gh release list
gh release view tag

GitHub Features:

  • Actions (CI/CD)
  • Projects (Project management)
  • Discussions (Community)
  • Wikis (Documentation)
  • Pages (Static sites)
  • Packages (Package registry)
  • Security (Vulnerability scanning)
  • Codespaces (Cloud development)

Best Practices

Commit Messages:

  • Use present tense ("Add feature" not "Added feature")
  • Be descriptive but concise
  • Reference issues when applicable
  • Separate subject from body with blank line
  • Capitalize the subject line
  • Do not end the subject line with a period
  • Wrap body at 72 characters
  • Use body to explain what and why vs. how

Branching Strategy:

  • Use feature branches
  • Keep branches short-lived
  • Delete merged branches
  • Use descriptive branch names
  • Regularly sync with main branch
  • Consider Git flow for larger projects
  • Use protection rules for important branches
  • Require code reviews

Repository Management:

  • Use .gitignore properly
  • Keep repositories focused
  • Document setup and contribution
  • Use semantic versioning
  • Back up repositories
  • Regularly clean up old branches
  • Monitor repository size
  • Use Git LFS for large files