Git Fundamentals: Build a Clean, Recoverable History
You will initialize a repository, stage and commit changes, branch, and push to a remote. By the end you will have a clean, recoverable history and understand the staging-area model that makes Git powerful.
Learning Objectives
- Track changes with the staging area and meaningful commits.
- Branch and switch context safely.
- Push to and pull from a remote.
- Time: ~2 hours · Difficulty: Beginner · Prereqs: Git installed and a GitHub account.
Architecture Overview
graph LR
WT[Working tree] -->|git add| Stage[Staging area]
Stage -->|git commit| Local[Local repo]
Local -->|git push| Remote[(Remote: GitHub)]
Remote -->|git pull| Local
Environment Setup
You will need: Git 2.4x and a remote (GitHub) repository.
Before you begin: set your identity with git config --global user.name/email.
Step-by-Step Execution
01
Initialize and make the first commit
The staging area lets you craft exactly what goes into each commit.
git init && git add . && git commit -m "initial commit"02
Branch for a change
git switch -c feature/login && git commit -am "add login"03
Connect a remote and push
$ git remote add origin [email protected]:tyfsadik/app.git && git push -u origin feature/login
branch 'feature/login' set up to track 'origin/feature/login'.
Progress So Far
graph LR
A[01 init + commit] -->|done| B[02 branch]
B -->|done| C[03 remote + push]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
git log --oneline --graph --all && git statusYou should see your commits on the branch and a clean working tree. If so, your history is tracked and pushed.
Troubleshooting
- Author unknown: set
user.name/user.emailbefore committing. - Push rejected: pull/rebase first; the remote has commits you lack.
- Committed a secret: remove it and rewrite history with git filter-repo, then rotate the secret.
Extension Ideas
- Adopt a workflow in Branching & Merging.
- Collaborate via Pull Requests.
- Automate checks with GitHub Actions.
Key Results
- Built a clean, recoverable commit history.
- Used branches to isolate a change from main.
- Pushed work to a tracked remote branch.
- Verified state with log and status.