← Back to the Build Your Homepage series
πŸš€
EPISODE 03
Commit and push from the terminal β€” for real

Work Like a Real Developer with the Git CLI

Stop uploading files through the GitHub web UI. Install git, set up your identity, and learn the daily workflow: clone, add, commit, push, pull, branch, and merge.

GitTerminalCommitBranchMerge
Duration
⏱ About 2.5 hours
Level
πŸ“Š Beginner (after Lesson 2)
Prerequisite
🎯 Lessons 1–2
OUTCOME
Fluently commit and push code from your terminal, with branches

What you'll learn

  • 1Install git and configure user.name / user.email
  • 2Clone a remote repository to your laptop
  • 3Use the add β†’ commit β†’ push cycle confidently
  • 4Create and switch branches with switch / checkout
  • 5Merge branches and resolve simple conflicts

1. Why the CLI?

The GitHub web UI is fine for one file, but breaks down quickly when:

  • You change ten files at once
  • You need to switch between branches
  • You collaborate with teammates
  • You want to undo a commit cleanly

Every developer eventually uses git from the terminal. Better to learn it now.

2. Install & Configure Git

Install

OSHow
macOSxcode-select --install (or brew install git)
Windowshttps://git-scm.com/download/win β†’ install with defaults
Linuxsudo apt install git (or your distro equivalent)
bash
git --version   # should print "git version 2.x.x"

git config --global user.name  "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
πŸ’‘

Use the email tied to your GitHub account β€” it makes commits show up under your profile.

3. Clone a Repository

Clone copies a remote repo to your laptop. Find the green "Code" button on GitHub and copy the HTTPS URL.

bash
git clone https://github.com/your-name/my-homepage.git
cd my-homepage
ls   # files appear locally

4. The Daily Cycle: add β†’ commit β†’ push

Every change you make follows the same three-step rhythm.

bash
# 1. Edit files in your editor

# 2. See what changed
git status
git diff

# 3. Stage the files you want to record
git add index.html style.css
# or stage everything: git add .

# 4. Commit with a clear message
git commit -m "Style the navbar with flexbox"

# 5. Push to GitHub
git push

What each step does

  • add: tell git "include these changes in the next snapshot" (staging area)
  • commit: save the snapshot with a message (still local)
  • push: upload your local commits to GitHub

Pull before push

If teammates pushed changes, run git pull first to merge them, then push your work.

bash
git pull --rebase
git push

5. Branches & Merging

Branches let you work on a feature without breaking the main version. You can have several in flight at once.

bash
git switch -c feature/dark-mode   # create + switch
# ... edit and commit ...
git switch main                    # back to main
git merge feature/dark-mode        # bring the work in

git branch -d feature/dark-mode    # delete the merged branch

Handling conflicts

When two branches change the same lines, git asks you to resolve. Open the marked file, choose the right version, then:

bash
git add <resolved-file>
git commit
# (git auto-fills the merge commit message)

6. Reading & Undoing History

bash
git log --oneline -10           # last 10 commits
git show HEAD                   # latest commit details
git diff HEAD~1 HEAD            # diff between two commits

# Undo unstaged changes in a file
git restore index.html

# Unstage a file (keep edits)
git restore --staged index.html

# Revert a pushed commit safely
git revert <commit-hash>
⚠️

Avoid git reset --hard unless you really mean to throw away work. Prefer git revert for anything already pushed.

7. .gitignore

Tell git to ignore noise: build outputs, dependency folders, OS files.

text
# .gitignore
node_modules/
.DS_Store
.env
build/
.vscode/

Commit the .gitignore itself so the whole team shares the same rules.

8. Quick Reference

ActionCommand
See what changedgit status / git diff
Stagegit add <files>
Commitgit commit -m "..."
Pushgit push
Pullgit pull --rebase
New branchgit switch -c <name>
Switch branchgit switch <name>
Mergegit merge <branch>
Historygit log --oneline
Undo unstaged editsgit restore <file>

πŸ›  Mini Projects

Replace your web-UI edits with a full add→commit→push terminal workflow
Build a feature branch for a new section, merge it into main, then delete the branch
Set up a .gitignore that excludes .DS_Store and node_modules
Example code / lecture materials

All lecture materials and example code are openly available on GitHub.

View on GitHub β†—