Git is a version control system. It records snapshots of your project over time so you can go back to an earlier version, see what changed, and work alongside other people without overwriting each other’s work.
The central idea: Git stores snapshots, not diffs. Each time you commit, Git records what every file looks like at that moment. Unchanged files aren’t copied again; Git just points to the version it already has. Your project’s history is a chain of these snapshots, and each one points back to its parent:
A ← B ← C ← D (main)
↑
HEAD ("you are here")
Each commit has a unique ID, a hash such as a1b2c3d. HEAD is the commit you’re currently on.
Moving between area
Work moves through three areas:
Working Directory ──git add──▶ Staging Area ──git commit──▶ Repository
(files you edit) (the "index": (permanent
next commit's history in
contents) .git/)
The staging area exists so you can pick what goes into a commit. If you edited five files but only three belong to one logical change, you stage just those three.
When you add a remote (such as GitHub), there’s a fourth place: a copy of the repository on another machine. You sync with it using push, fetch and pull.
The workflow
The commands below are in the order you’d normally use them.
Step 0: One-time setup
Tell Git who you are. Every commit is stamped with this name and email.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
Step 1: Get a repository
There are two ways to start:
# Option A: copy an existing project from a remote
git clone https://github.com/someone/project.git
cd project
# Option B: turn a local folder into a new repository
cd my-project
git init
clone downloads the whole history, not just the latest files, and automatically sets up a remote called origin that points back to where you cloned from.
Step 2: Check where things stand
git status
Run this constantly. It shows which files are modified, which are staged, and which Git isn’t tracking yet. It also often suggests the next command to run.
Step 3: Create a branch for your work
A branch is just a movable label pointing to a commit. Creating one is instant and cheap, so it’s good practice to do each piece of work on its own branch:
git switch -c add-login-page # create a new branch and move to it
(The older equivalent is git checkout -b add-login-page.)
A ← B ← C (main)
↖
(add-login-page) ← HEAD
Step 4: Edit files, then review your changes
git diff # what changed but is NOT staged yet
git diff --staged # what IS staged and will go in the next commit
Step 5: Stage changes with add
git add login.html # stage one file
git add src/ # stage a whole directory
git add -p # go through changes piece by piece (very useful)
git add doesn’t save anything permanently. It only puts changes into the box you’re packing.
Step 6: Record a snapshot with commit
git commit -m "Add login page with email/password form"
This takes everything in the staging area and saves it as a new commit whose parent is the current HEAD. The branch label moves forward to the new commit:
A ← B ← C ← E (add-login-page) ← HEAD
↑
(main)
Writing good commit messages: a short summary line (about 50 characters, in the imperative, e.g. “Fix”, “Add”, “Remove”) that says what the commit does. Add a longer body after a blank line if you need to explain why.
Step 7: Look at history
git log # full history
git log --oneline --graph # compact view that draws the branches
git show <commit-hash> # what one particular commit changed
Step 8: Share your work with push
git push -u origin add-login-page
This uploads your commits to the remote. -u makes your local branch “track” the remote one, so next time a plain git push is enough.
Step 9: Get other people’s work with fetch and pull
git fetch # download new commits from the remote without changing your files
git pull # fetch, then merge them into your current branch
pull is fetch followed by merge. When you want to look before you merge, run fetch on its own first.
Step 10: Combine branches with merge
When your feature is finished, bring it into main:
git switch main
git pull # make sure main is up to date
git merge add-login-page
If both branches changed the same lines, Git stops and reports a merge conflict. The file will contain markers like these:
<<<<<<< HEAD
the version on main
=======
the version on your branch
>>>>>>> add-login-page
Edit the file so it contains what you actually want, delete the markers, then run:
git add <file>
git commit
(On GitHub and similar sites, this step usually happens through a pull request rather than a local merge.)
Step 11: Clean up
git branch -d add-login-page # delete the local branch once it's merged
Undoing things
Pick the undo command based on where the mistake is.
Undo changes that aren’t staged yet
git restore file.txt # throw away your edits and go back to the last commit
⚠️ This permanently discards those edits.
Un-stage something (but keep the edits)
git restore --staged file.txt # take it out of the box; the file on disk is unchanged
Fix the last commit (before you’ve pushed)
git add forgotten-file.txt
git commit --amend # replace the last commit with a corrected one
⭐ Revert the previous commit
git revert HEAD
What it does: creates a new commit that does the exact opposite of the last one. If the last commit added a line, the revert commit removes it. Git opens an editor so you can adjust the commit message.
Before: A ← B ← C (C was a mistake)
After: A ← B ← C ← C' (C' undoes C)
Why it’s the safe choice: nothing in the history is erased. C is still there, and C’ cancels it out. That makes revert the right way to undo commits that have already been pushed, because other people’s copies of the history stay valid.
Related forms:
git revert HEAD~1 # undo the commit before the last one; later commits are kept
git revert <hash> # undo any particular commit
git revert --no-edit HEAD # skip the message editor
If later commits touched the same lines, you may have to resolve a conflict, just as with a merge.
Throw away commits (local only)
git reset --soft HEAD~1 # undo the commit; its changes stay staged
git reset HEAD~1 # undo the commit; its changes stay in your files, unstaged
git reset --hard HEAD~1 # ⚠️ undo the commit AND delete its changes
reset moves the branch label backwards, which rewrites history. Only use it on commits you haven’t pushed.
revert vs. reset: the rule to remember
git revert | git reset | |
|---|---|---|
| How it undoes | Adds a new “opposite” commit | Moves the branch back and discards commits |
| History | Kept | Rewritten |
| Safe after pushing? | ✅ Yes | ❌ No |
Already pushed? Revert. Still only local? Reset is fine.
Safety net: reflog
git reflog
Git keeps a log of every position HEAD has been at, including commits you “lost” with reset --hard. Find the hash you want and run git reset --hard <hash> to get back to it. Committed work is very rarely gone for good.
Part 5: Quick reference, in workflow order
| Stage | Command | Purpose |
|---|---|---|
| Setup | git config --global user.name "…" | Identify yourself |
| Start | git clone <url> / git init | Get a repository |
| Branch | git switch -c <name> | Start isolated work |
| Inspect | git status, git diff | See what changed |
| Stage | git add <file> | Choose what goes in the commit |
| Commit | git commit -m "…" | Save a snapshot |
| History | git log --oneline --graph | Review past commits |
| Share | git push | Upload commits |
| Sync | git fetch / git pull | Download commits |
| Combine | git merge <branch> | Join branches |
| Discard edits | git restore <file> | Drop unstaged changes |
| Unstage | git restore --staged <file> | Take a file out of staging |
| Fix last commit | git commit --amend | Correct an unpushed commit |
| Undo last commit | git revert HEAD | Safe undo via a new commit |
| Rewind | git reset [--soft|--hard] HEAD~1 | Local-only undo |
| Rescue | git reflog | Find “lost” commits |
The mental model in one paragraph
You edit files in your working directory, choose changes with add into the staging area, and save them as a snapshot with commit into the repository. Commits link back to their parents to form a history. Branches are labels that move forward as you commit, and HEAD marks where you are. push and pull sync your repository with a remote. To undo, find where the mistake is: restore for files, reset for local commits, and revert for commits that are already shared.