30 Days of DevOps — Day 01 Complete
day_01 / git-and-github.md
Git & GitHub
Complete guide — version control from zero to remote workflow. Everything covered in one place, no external reference needed.
8
Topics
30+
Commands
2
Merge types
6
Diagrams
1
Day done ✓
01
What is Version Control?
The problem it solves
Imagine writing code, making changes, something breaks — and you can't remember what you changed. Version control records every change you make to your files over time. You can go back to any previous state — like an undo button that never expires, even days or months later.
Video game save point analogy — you can keep playing, make risky moves, and always reload from the last save. Git is your save-point system for code.
Before Git — the dark ages
Before Git, developers used tools like SCCS, CVS, and SVN (Subversion). They were expensive, slow, and painful to use in teams. Git was created in 2005 by Linus Torvalds (creator of Linux) to replace all of these — free, fast, and distributed.
02
Git vs GitHub — They're Different!
Git
Software installed on your own computer. Tracks file changes locally. Works completely offline. Free and open-source. Available for Windows, macOS, Linux.
GitHub
A website and cloud platform to store your Git repos online. Used for backup, sharing code, and team collaboration. GitLab, Bitbucket, Azure Repos are alternatives.
Golden rule: Git can exist without GitHub. GitHub cannot exist without Git. Git is the engine — GitHub is the cloud garage where you park and share your work.
03
Core Concepts
Repository (Repo)
Your project folder that Git tracks. Looks like a normal folder, but contains a hidden .git folder storing the entire history. Created with git init. Not all folders are repos — only the ones you initialize.
Commit — the snapshot
A commit is a permanent save point. Each commit has a unique hash ID (like a3f8c2d), your name, timestamp, and a message. You can jump to any commit at any time. Think of your project history as an album of photos — each commit is one photo capturing the exact state of your code at that moment.
Commits are like photos in an album. Each photo captures exactly what your code looked like at that moment. Your album (repo) holds all photos in order, forever.
Staging Area — the buffer zone
Before committing, files go to the staging area (also called the index). This lets you choose exactly which changes to include — you might edit 5 files but stage only 2 for this commit. Gives you precise control over what gets saved.
Like packing a box before shipping. You gather items on a table (staging), review what you want, then seal and send the box (commit).
git workflow — working dir → staging → local repo → github
Working dir your edits git add Staging area ready to commit git commit Local repo saved history git push GitHub (remote) cloud backup
04
Setup & Essential Commands
  first-time setup
$git config --global user.name "Your Name"
$git config --global user.email "you@example.com"
$git config --global core.editor "code --wait" # use VS Code
$git config --list # verify your settings
$git --version # check git is installed
 daily workflow
$git init # start tracking this folder as a repo
$git status # see what changed / what's staged
$git add . # stage all changed files
$git add <filename> # stage a specific file only
$git commit -m "describe what you changed"
$git log # full history
$git log --oneline # compact 1-line per commit
 .gitignore — files git should never track
node_modules/ # heavy dependency folders
.env # secret keys, API tokens, passwords
.vscode/ # editor-specific settings
*.log # all log files
dist/ # build output folders
Atomic commits: One commit = one logical change. Never bundle "fix login bug + redesign header + update database" into one commit. Keep them small, focused, and descriptive. This makes history readable and rollbacks easy.
05
Branches & Merging
What is a branch?
A branch is an independent copy of your codebase where you can work freely without touching the stable main branch. Multiple developers can work on different branches at the same time. When a feature is ready, you merge it back. The default branch was called master but is now called main by convention.
Parallel universes analogy — each branch is a separate timeline. You can experiment in any timeline, and decide which ones to merge back into the main reality. HEAD is a pointer that always tells Git: "this is where you are right now."
branch diagram — feature branch created, worked on, then merged back
c1 c2 c3 f1 f2 merge c4 main feature-x branch
 branch commands
$git branch # list all branches (* = current)
$git branch feature-x # create new branch
$git switch -c feature-x # create AND switch in one step
$git switch main # go back to main
$git merge feature-x # merge feature into current branch
$git branch -d feature-x # delete branch after merge
$git branch -m old-name new-name # rename branch
Fast-forward merge
Happens when main has no new commits since branch was created. Git just moves the pointer forward. No conflicts possible. Clean and simple.
3-way merge
Happens when both main and branch have new commits. Git looks at 3 points — common ancestor + tips of both branches — and creates a merge commit.
Merge conflicts: When both branches edited the same line differently, Git can't decide. You open the file, read both versions, choose manually, then git add + git commit. VS Code has a built-in merge conflict tool — use it. It's not scary, it's just a decision.
06
Diff, Stash & Tags
git diff
Shows exactly what changed line by line. Lines with + were added. Lines with were removed. Use before committing to review your work.
git stash
A temporary clipboard for uncommitted work. Need to switch branches mid-task? Stash your work, switch, come back and pop it to restore.
 diff / stash / tags
$git diff # unstaged changes vs working dir
$git diff --staged # staged changes vs last commit
$git diff branch-a branch-b # compare two branches
$git stash # save work temporarily
$git stash save "work in progress on login"
$git stash list # see all stashes
$git stash pop # restore latest + remove from stash
$git stash clear # remove all stashes
$git tag v1.0 # lightweight tag on current commit
$git tag -a v1.0 -m "First release" # annotated tag
$git push origin v1.0 # push tag to GitHub
07
GitHub — Remote Workflow
SSH key — why it's mandatory
Since 2021, GitHub disabled password authentication for pushing code. You need an SSH key pair — a private key (stays on your machine, never share) and a public key (upload to GitHub). They match like a lock and key, proving your identity securely without any password.
 ssh setup
$ssh-keygen -t ed25519 -C "you@example.com"
# Press Enter to save to default path (~/.ssh/id_ed25519)
# Optional: add a passphrase for extra security
# Then copy ~/.ssh/id_ed25519.pub → GitHub Settings → SSH Keys
 push + pull workflow
$git remote add origin git@github.com:user/repo.git
$git remote -v # verify remote URL
$git push -u origin main # first push (sets upstream)
$git push # all future pushes (short form)
$git pull origin main # fetch + merge from remote
$git fetch # download only, don't merge yet
fetch vs pull: git fetch downloads changes but does NOT apply them — you review first. git pull = fetch + merge in one go. Use fetch when you want to inspect changes before applying. Use pull when you trust the remote and want to sync immediately.
08
History Management — Rebase & Reflog
git rebase
Replays your commits on top of another branch instead of creating a merge commit. Result: clean, linear history that looks like one straight line of development. Preferred by teams that value clean history.
git reflog
Records every HEAD movement — every checkout, commit, reset, rebase. Your ultimate safety net. Even if you delete a branch or do a bad reset, reflog shows the full trail so you can recover anything.
 rebase + reflog
$git checkout feature-branch
$git rebase main # replay commits on top of main
$git add . # after resolving any conflicts
$git rebase --continue # continue after conflict fix
$git reflog # see full history of HEAD movements
$git reset --hard HEAD@{2} # go back 2 steps
$git reset --hard <commit-hash> # go to specific commit
⚠ Rebase warning: Never rebase branches that other people are using. Rebase rewrites history — if someone else based work on your old commits, it breaks their repo. Golden rule: rebase local, merge public.
Complete Cheatsheet — All Commands
git initcreate repo
git statussee state
git add .stage all
git commit -m ""save snapshot
git log --onelinecompact history
git diffwhat changed
git diff --stagedstaged changes
git config --listsee all settings
git branchlist branches
git switch -c namecreate + switch
git switch mainchange branch
git merge branchmerge in
git branch -d namedelete branch
git stashsave WIP
git stash poprestore WIP
git stash listsee all stashes
git tag v1.0mark release
git remote addlink GitHub
git push -u originfirst push
git pushpush changes
git pullsync + merge
git fetchdownload only
git rebase mainclean history
git reflogsafety net
Key Takeaways
Git = local software. GitHub = online platform. They are not the same thing. You can use Git without GitHub, but not GitHub without Git.
SSH key setup is mandatory — password auth has been disabled on GitHub since 2021. Generate your ed25519 key and add the public key to your GitHub settings.
Atomic commits = one task per commit. Keep them small and focused. A clean commit history makes debugging, reviewing, and reverting much easier.
git reflog is your safety net — you can recover almost anything with it, even deleted branches or bad resets. It's local only, not synced to GitHub.
Never force-push or rebase shared branches. Rewrite history only on your local feature branches, never on main or branches others are using.