Git Basics

💤0
Lv 10 XP
← 🧱 Foundations · Git & GitHub

Git Basics

Beginner ⭐ 50 XP ⏱ 22 min #git#version-control#github

Learn the core Git workflow — how changes flow from your files to a commit to a remote like GitHub.

📖Theory

Git is a version control system: it takes snapshots of your project over time so you can review history, undo mistakes, and collaborate without emailing zip files around.

The key idea is that a change moves through three areas:

  1. Working directory — your actual files as you edit them.
  2. Staging area (index) — a holding zone where you assemble the next snapshot with git add.
  3. Repository — permanent history, created when you git commit.

A remote (like GitHub) is just another copy of the repository hosted elsewhere. You sync with it using git push (send your commits) and git pull (get others’ commits).

graph LR
  W["Working
Directory"] -->|git add| S["Staging
Area"] S -->|git commit| L["Local
Repository"] L -->|git push| R["Remote
(GitHub)"] R -->|git pull| L
How a change flows from your files to GitHub
🌍Real-World Example

A complete first-project workflow, start to finish:

# One-time identity setup
git config --global user.name "Alex"
git config --global user.email "alex@example.com"

# Start a repo
mkdir myapp && cd myapp
git init

# Make a change, then stage and commit it
echo "# My App" > README.md
git status                 # see what changed
git add README.md          # stage it
git commit -m "Add README" # snapshot it

# Connect to GitHub and publish
git remote add origin https://github.com/alex/myapp.git
git push -u origin main

After this, your daily loop is simply: edit → git addgit commitgit push.

✍️Hands-On Exercise
  1. Initialize a new repo and make your first commit containing a single file.
  2. Edit that file, run git diff, and read what changed before staging.
  3. Make two unrelated changes, but stage and commit them separately so each commit tells one story.
  4. Run git log --oneline to view your history compactly.
🧾Cheat Sheet
TaskCommand
Start a repogit init
Clone an existing repogit clone <url>
See what changedgit status / git diff
Stage changesgit add <file> / git add .
Commitgit commit -m "message"
View historygit log --oneline --graph
Connect a remotegit remote add origin <url>
Upload commitsgit push
Download commitsgit pull
Undo unstaged changesgit restore <file>
Unstage a filegit restore --staged <file>
💬Common Interview Questions
What's the difference between `git add` and `git commit`?

git add stages changes — it tells Git which changes you want in your next snapshot. git commit actually records that snapshot into history with a message.

What does `git pull` do under the hood?

It’s git fetch (download new commits from the remote) followed by git merge (integrate them into your current branch).

How is Git different from a backup or Dropbox?

Git records intentional, labeled snapshots (commits) with full history and branching, designed for merging concurrent work — not just mirroring the latest file state.

🚑Troubleshooting Scenarios

“Updates were rejected because the remote contains work you do not have.” Someone pushed before you. Run git pull to integrate their changes, resolve any conflicts, then push again.

You committed to the wrong branch. If you haven’t pushed, you can move the commit: create the right branch, then use git reset to rewind the wrong one. (We’ll cover this in Branching & Merging.)

Accidentally staged a file you didn’t mean to. git restore --staged <file> removes it from staging without touching your edits.

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type