Git Basics
Learn the core Git workflow — how changes flow from your files to a commit to a remote like GitHub.
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:
- Working directory — your actual files as you edit them.
- Staging area (index) — a holding zone where you assemble the next snapshot
with
git add. - 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
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 mainAfter this, your daily loop is simply: edit → git add → git commit →
git push.
- Initialize a new repo and make your first commit containing a single file.
- Edit that file, run
git diff, and read what changed before staging. - Make two unrelated changes, but stage and commit them separately so each commit tells one story.
- Run
git log --onelineto view your history compactly.
Cheat Sheet▾
| Task | Command |
|---|---|
| Start a repo | git init |
| Clone an existing repo | git clone <url> |
| See what changed | git status / git diff |
| Stage changes | git add <file> / git add . |
| Commit | git commit -m "message" |
| View history | git log --oneline --graph |
| Connect a remote | git remote add origin <url> |
| Upload commits | git push |
| Download commits | git pull |
| Undo unstaged changes | git restore <file> |
| Unstage a file | git 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.