Free Interactive DevOps & Cloud Courses
Free, interactive, bite-sized tutorials designed specifically for cloud newcomers and DevOps systems practitioners. Master the core tools of modern engineering with real-world analogies and instant concept checks.
Git & GitHub Version Control
Real-World Analogy (Read This First!)
Imagine playing a difficult video game. Before a risky boss fight, you save your progress in a "save slot". If you die, you load that save slot. Git is a save-slot system for your folder. Instead of copying folders to project_v1, project_v2_final, Git takes lightweight snapshots (commits) and lets you load them instantly.
Course Modules
Core Concept
Have you ever saved a file as app_final_v3.py? In teams, that gets messy fast!
Version Control Systems (VCS) act as a code time machine, tracking every single character change, authorship, and version history.
• Centralized VCS (e.g., SVN) stores all history on a single central server. If it crashes or you lose internet access, you cannot commit or view logs.
• Distributed VCS (e.g., Git) gives everyone a full clone of the entire repository history locally. You can work, commit, and branch offline with zero central dependencies.
Compare Centralized vs. Distributed commands:
# Centralized (SVN): Must query the remote server to view history svn log -r 100:200 http://svn.company-network.com/repo # Distributed (Git): Query the complete database locally and instantly git log --oneline -n 5
Concept Checkpoint
What is the main advantage of a Distributed VCS (like Git) over a Centralized VCS (like SVN)?
Core Concept
Let's clarify the two pillars of modern version control:
• What is Git? It is a local command-line engine that tracks changes in your files. Think of it as a personal diary tracking edits on your machine.
• What is GitHub? It is a cloud hosting platform for your Git repositories. While Git tracks files locally, GitHub is the web service where you back up, share, and collaborate on those diaries with teams.
Initializing Git (Theory & Practice):
To start tracking a folder, open your terminal inside it and run git init. This spins up a hidden folder named .git. This hidden directory holds Git's entire tracking database and history. If you delete this folder, you delete your project's history, turning it back into a standard folder.
What happens when Git is initialized (The 3 Stages):
Once initialized, Git splits your workspace into three zones:
1. Stage 1: Working Directory (Workspace): Your local sandbox where you write, modify, and delete code. These changes are local and not yet saved to history.
2. Stage 2: Staging Area (Index): A preview buffer or checkout cart. You select which specific edits should go into the next save point using git add.
3. Stage 3: Local Repository (Repo): The permanent, secure registry. Running git commit locks staged files in as a permanent snapshot.
The Flow: Working Directory —(add)—> Staging Area —(commit)—> Local Repo.
• What does the add command do? It copies modifications from your Working Directory to the Staging Area, telling Git: "I want this change in my next save point."
• What does commit mean? It permanently records the staged files as a checkpoint, along with a message describing the work.
Initialize, Stage, and Commit changes:
# 1. Initialize a new local Git repository git init # 2. Check the current status of your workspace git status # 3. Add a file to the Staging Area (checkout cart) git add app.js # 4. Commit the staged changes with a descriptive message git commit -m "feat: setup initial web application server"
Concept Checkpoint
Which command moves files from the Working Directory into the Staging Area, preparing them to be committed?
Core Concept
To navigate Git, you must speak its language. Here is your developer glossary explained simply with examples and analogies:
• Repository (Repo): A project folder containing all code files and the hidden .git database. It is a folder with a built-in time machine.
• Server (Remote): A computer in the cloud (like GitHub) hosting a copy of your repo. It acts as the shared central hub for your team.
• Working Directory / Workspace: Your local sandbox folder on your PC where you edit, save, and delete code files.
• Commit: A permanent snapshot of your staged files in the local repository's history. Think of it as a "Save Game" state.
• Commit ID: A unique 40-character fingerprint (e.g., 5a1f28b...) created via cryptographic hashing (SHA-1). It guarantees security; if a single line of code is altered, its ID changes entirely!
• Snapshots (vs. Diffs): Unlike older systems that store line-by-line differences, Git takes a lightweight "snapshot" of all your files at that moment. If a file hasn't changed, Git doesn't duplicate it; it simply points to the previous version to keep things incredibly fast and compact.
• Tags: Human-readable bookmarks pointing to specific commits, usually release versions (e.g., v1.0.0). Think of them as sticky notes on a specific page of your history book.
• Push & Pull: push uploads local commits to the Server (GitHub). pull downloads remote commits from the Server and merges them into your local work.
• Branch: A parallel timeline. You can build a feature on a new branch without affecting the main timeline.
The Key Advantages of Git:
1. Distributed Nature: Work and view history fully offline.
2. High Performance: Operations are local and near-instant.
3. Data Integrity: Cryptographic verification protects history from silent corruption.
Log Commits, Create Tags, and Sync:
# View short Commit IDs (hashes) and messages git log --oneline # Create a release bookmark (Tag) pointing to the current commit git tag v1.0.0 # Push local commits and tags to the remote server git push origin main --tags # Download and merge latest changes from teammates git pull origin main
Concept Checkpoint
What is a Commit ID in Git and why is it important?
Core Concept
Before we start creating repositories, we need to install and configure Git on our machine. On a Linux system (like Ubuntu/Debian), we can install Git using the package manager. After installation, we must configure our identity (name and email) so that every commit we make is signed with our information. This is critical for collaboration as it shows who made what changes.
Key commands used during setup:
• sudo apt update && sudo apt install git -y: Updates package lists and installs the Git tool.
• git --version: Verifies that Git is installed and displays the active version.
• git config --global user.name "Your Name": Configures your commit author name.
• git config --global user.email "your.email@example.com": Configures your commit author email.
• git config --list: Lists all Git configurations active in the current environment.
Run installation and initial configuration commands:
# 1. Update and install Git on Linux (Ubuntu/Debian) sudo apt update && sudo apt install git -y # 2. Verify the installed Git version git --version # 3. Configure global identity git config --global user.name "Keem" git config --global user.email "keem@example.com" # 4. Inspect your configuration settings git config --list
Concept Checkpoint
Why do we need to configure our user.name and user.email after installing Git?
Core Concept
A branch in Git is simply a lightweight, movable pointer to one of your commits. Think of it as a parallel universe or timeline. When you initialize a repository, Git creates a default branch (typically named main or master). If you want to build a new feature (like a 'dark mode' toggle) or fix a bug without breaking the stable code on main, you create a new branch. Changes made on this new branch are completely isolated. Once you test and verify your new code, you can bring it back into the main branch.
Key commands:
• git branch: Lists all local branches. The active branch has an asterisk (*).
• git branch <branch-name>: Creates a new branch at your current commit.
• git checkout <branch-name>: Switches your working directory to the target branch.
• git checkout -b <branch-name>: A shortcut command that creates a new branch and immediately switches to it.
View, create, and navigate branches:
# 1. List all local branches (and see which one is active) git branch # 2. Create a new branch named 'feature-auth' git branch feature-auth # 3. Switch to the new branch git checkout feature-auth # 4. Shortcut: Create and switch to 'feature-payment' in one command git checkout -b feature-payment
Concept Checkpoint
What is a Git branch and why is it useful?
Core Concept
Once Git is initialized, you begin the cycle of editing files, staging changes, committing snapshots locally, and syncing them to a remote server like GitHub. Here is a step-by-step breakdown of the full developer flow:
1. git init: Initializes a new local Git repository (creates the .git database).
2. Write / edit code in your files.
3. git status: Shows what files are modified, untracked, or staged.
4. git add .: Stages all new and modified files (prepares them for the commit).
5. git commit: Saves your staged snapshot permanently into local history (using -m to supply a message).
6. git status: Verifies that your working tree is clean.
7. git log: Shows a chronological list of commits.
8. git show <commitID>: Explores the exact diff and details of a single commit.
9. git remote add origin <url>: Connects your local repository to a remote repository hosted on GitHub.
10. git push -u origin main: Uploads your local commits on the main branch to the remote server, setting it as the default tracking branch.
Stage, commit, and link to GitHub:
# 1. Initialize repository and check status git init git status # 2. Stage all modifications and commit git add . git commit -m "feat: add user login API endpoint" # 3. View commit history and inspect latest commit git log --oneline # Replace 'a1b2c3d' with a real commit ID from your git log git show a1b2c3d # 4. Link to remote repository and push changes git remote add origin https://github.com/user/demo-repo.git git push -u origin main
Concept Checkpoint
Which command is used to link your local Git repository to a remote repository on GitHub?
Core Concept
When feature branches are finished, they must be merged back into the main timeline. If changes do not conflict, Git performs a merge automatically. However, if two branches modify the same line of the same file, Git pauses and signals a Merge Conflict, inserting conflict markers (<<<<<<<, =======, >>>>>>>) into the file. You must open the file, select the correct code, remove the markers, stage, and commit.
Additionally, two vital tools help manage daily workflow:
• git stash: Shelves your local changes temporarily, letting you switch branches or pull updates without committing half-finished code. You restore stashed work using git stash pop.
• git reset: Rolls back local branch states. --soft keeps your files changed and staged. --mixed (default) keeps files changed but unstaged. --hard completely deletes all modifications since the target commit.
Branch, Commits, and Merge Workflow:
Merge a branch, resolve conflicts, or stash changes:
# 1. Merge feature branch into main git checkout main git merge feature-auth # 2. Stash temporary changes to clean your directory git stash # Do other tasks, then bring back your stashed work git stash pop # 3. Reset local repository state to last commit (Discarding changes) git reset --hard HEAD
Concept Checkpoint
What happens during a Merge Conflict in Git?
Core Concept
Undoing changes in Git requires understanding the difference between history-altering commands and history-preserving commands:
• git reset vs. git revert:
- git reset <commitID> rewrites history by moving the branch pointer backward. It effectively erases commits that came after the target commit. Warning: Never use reset on public, shared branches as it disrupts your teammates' history.
- git revert <commitID> creates a new commit that applies the exact opposite changes of the specified commit. This preserves history, making it completely safe for shared branches.
• Rollbacks & Tags: Release tags like v1.0.0 act as immutable bookmarks. If a release breaks, you can use tags or reverts to restore a stable state.
• git clone <url>: Downloads an existing remote repository from GitHub to your local machine, setting up all tracking branches and repository history automatically.
Clone a repo and revert a commit:
# 1. Clone a remote project to your local computer git clone https://github.com/user/project.git # 2. Revert a specific commit (creates a new undo commit) # Replace 'c7d8e9f' with the commit hash you want to undo git revert c7d8e9f # 3. Create a release tag and push it to GitHub git tag v1.1.0 git push origin v1.1.0
Concept Checkpoint
When should you use git revert instead of git reset to undo a change?
Core Concept
In professional settings, teams use collaborative workflows on GitHub to review and test code before merging it:
• Forking: Creating your own personal copy of someone else's GitHub repository on your GitHub account. This allows you to experiment freely without affecting the original project.
• Pull Requests (PRs): A GitHub feature that allows you to propose changes from your fork or branch back to the main repository. Teammates can comment, review, and run tests on your changes before they are merged.
• Synced Workflows (Fetch vs. Pull):
- git fetch downloads the latest information from the remote server but does not modify your local working files.
- git pull is a shortcut that runs git fetch followed immediately by git merge, updating your local files with remote changes.
Synchronize changes with the team:
# 1. Fetch latest remote branches and changes (safe, no merge) git fetch origin # 2. Pull latest commits from remote main branch (fetches + merges) git pull origin main # 3. Create a branch, push it to GitHub, and prepare for PR git checkout -b feature-payment-gateway # (make changes and commit them) git push -u origin feature-payment-gateway
Concept Checkpoint
What is the difference between git fetch and git pull?
Core Concept
Need a quick reference? Here is a comprehensive Cheat Sheet of essential Git commands to keep in your toolbelt:
1. Setup & Configuration
• git config --global user.name "Name": Configures your global author name.
• git config --global user.email "email@example.com": Configures your global author email.
• git init: Initializes a new local Git repository in the current folder.
• git clone <url>: Clones (downloads) a remote repository to your computer.
2. Local Changes
• git status: Lists modified, untracked, or staged files.
• git add <file>: Stages a specific file for the next commit.
• git add .: Stages all modified and new files.
• git commit -m "message": Commits staged changes with a descriptive message.
• git diff: Shows changes between your working directory and the staging area.
3. Branching & Merging
• git branch: Lists all local branches (active branch is marked with *).
• git branch <name>: Creates a new branch.
• git checkout <name>: Switches your working directory to the target branch.
• git checkout -b <name>: Creates a new branch and switches to it immediately.
• git merge <branch>: Merges the target branch into your active branch.
• git stash: Temporarily shelves (saves) uncommitted local changes to clean your directory.
• git stash pop: Restores stashed changes and deletes them from the stash list.
4. History & Logs
• git log: Shows a detailed commit history.
• git log --oneline: Shows a simplified, single-line list of commits.
• git show <commitID>: Explores details and line changes of a specific commit.
• git tag <name>: Marks a specific commit with a human-readable bookmark (e.g. v1.0.0).
5. Sharing & Collaborating
• git remote add origin <url>: Links your local repository to a remote server.
• git push -u origin <branch>: Uploads local commits and sets up tracking.
• git pull: Downloads remote commits and automatically merges them.
• git fetch: Downloads remote updates without merging them.
6. Undoing & Resetting
• git revert <commitID>: Creates a new commit that undoes the changes of a specified commit (safe for shared branches).
• git reset --hard <commitID>: Discards all commits and uncommitted changes back to a specific commit (destructive!).
• git reset --soft <commitID>: Undoes commits, but keeps your files changed and staged in the staging area.
Quick Reference Commands:
# Setup git config --global user.name "Keem" git config --global user.email "keem@example.com" git init git clone https://github.com/user/project.git # Local Changes git status git add . git commit -m "feat: add registration flow" git diff # Branching & Merging git branch git checkout -b feature-dark-mode git merge feature-dark-mode git stash git stash pop # Sharing & Syncing git push origin main git pull origin main git fetch # Undoing git revert a1b2c3d git reset --hard HEAD
Concept Checkpoint
Which sequence of commands would you run to temporarily shelve your uncommitted work, pull the latest changes from the remote server, and then re-apply your shelved work?