Git & GitHub Complete Guide

Master Version Control and Collaborative Development

What is Git?

Git is a distributed version control system (DVCS) created by Linus Torvalds in 2005 for Linux kernel development. It's now the most widely used version control system in software development.

Key Features

  • Distributed architecture (every clone is a full repository)
  • Lightweight branching and merging
  • Data integrity through SHA-1 hashing
  • Non-linear development support

How Git Works

Git takes snapshots of your project at different points in time. Each snapshot (commit) contains:

  • Pointer to the previous commit
  • Author information
  • Commit message
  • Hash of the file tree

What is GitHub?

GitHub is a cloud-based platform built around Git that provides:

  • Remote repository hosting
  • Collaboration tools (issues, pull requests)
  • Project management features
  • CI/CD integration (GitHub Actions)
  • Package registry

GitHub vs Git

While Git is the version control software you run locally, GitHub is a platform that hosts Git repositories in the cloud with additional collaboration features.

Create Free GitHub Account

Why Use Git & GitHub?

For Individual Developers

  • Version history: Track every change and revert when needed
  • Experimentation: Create branches to try new ideas safely
  • Portfolio: Showcase your work to potential employers

For Teams

  • Collaboration: Multiple developers can work simultaneously
  • Code review: Pull requests enable peer review
  • Conflict resolution: Built-in tools to manage merge conflicts

For Organizations

  • Workflow automation: CI/CD pipelines with GitHub Actions
  • Access control: Granular permissions management
  • Project management: Integrated with issues, milestones, and projects

Getting Started with Git

Installation

# Windows (download installer) https://git-scm.com/download/win # macOS (using Homebrew) brew install git # Linux (Debian/Ubuntu) sudo apt install git

Initial Configuration

git config --global user.name "Your Name" # Set your username git config --global user.email "your@email.com" # Set your email git config --global init.defaultBranch main # Set default branch name

Basic Git Workflow

  1. Initialize a new repository or clone an existing one
  2. Create or modify files in your working directory
  3. Stage changes you want to commit
  4. Commit the staged changes with a descriptive message
  5. Push your commits to a remote repository (like GitHub)

Essential Commands

git init # Initialize new repository git clone https://github.com/user/repo.git # Clone existing repository git status # View changed files git add file.txt # Stage specific file git add . # Stage all changes git commit -m "Descriptive message" # Commit staged changes git push origin main # Push commits to remote git pull # Fetch and merge remote changes

Branching & Merging

Branches allow you to work on different versions of your code simultaneously. The default branch is typically called main or master.

git branch # List all branches git branch new-feature # Create new branch git checkout new-feature # Switch to branch git checkout -b new-feature # Create and switch to new branch git merge new-feature # Merge branch into current branch git branch -d new-feature # Delete branch

Branching Strategies

Common workflows include:

  • Feature branching: Each new feature gets its own branch
  • Git Flow: Formal model with develop, feature, release branches
  • GitHub Flow: Simpler model focused on pull requests

GitHub Collaboration Workflow

  1. Fork the repository (creates your personal copy)
  2. Clone your fork locally
  3. Create a feature branch for your changes
  4. Make changes and commit them
  5. Push your branch to your fork
  6. Create a pull request (PR) to propose your changes
  7. Address any review feedback
  8. After approval, your changes get merged

GitHub-Specific Commands

git remote add upstream https://github.com/original/repo.git # Connect to original repo git fetch upstream # Get updates from original repo git merge upstream/main # Sync your main branch with original

Advanced Topics

Undoing Changes

git restore file.txt # Discard unstaged changes git reset --hard HEAD # Discard all uncommitted changes git revert commit-hash # Create new commit that undoes a previous commit

Rebasing

Alternative to merging that creates linear history:

git rebase main # Rebase current branch onto main

Stashing

Temporarily save changes without committing:

git stash # Save changes git stash pop # Restore most recent stash

Learning Resources

Official Documentation

Interactive Learning

Books

  • Pro Git (free online)
  • Git Pocket Guide