Lesson 52 of 55 · Python
Version Control with Git
Duration: 20 minutes
Version Control with Git
Git is the most widely‑used distributed version‑control system. It lets you track changes, collaborate with teammates, and safely experiment with new features.
1️⃣ Install Git
- Windows: Download the installer from https://git‑scm.com/download/win.
- macOS:
brew install git(Homebrew) or use the Xcode command‑line tools (xcode-select --install). - Linux:
sudo apt-get install git(Debian/Ubuntu) orsudo dnf install git(Fedora).
2️⃣ Basic Workflow
# 1. Initialise a new repository (run inside your project folder)
git init
# 2. Stage files for the first commit
git add .
# 3. Commit with a message
git commit -m "Initial commit – project skeleton"
3️⃣ Branching & Merging
# Create a new branch for a feature
git checkout -b feature/login
# After working, stage & commit changes
git add login.py
git commit -m "Add login view"
# Switch back to main and merge
git checkout main
git merge feature/login
4️⃣ Remote Repositories (GitHub example)
# Create a repo on GitHub, then link it locally
git remote add origin https://github.com/<username>/<repo>.git
# Push your local main branch
git push -u origin main
5️⃣ Common Commands Cheat‑Sheet
| Action | Command |
|---|---|
| View status | git status |
| See commit history | git log --oneline --graph --decorate |
| Discard local changes | git checkout -- <file> |
| Undo last commit (keep changes) | git reset --soft HEAD~1 |
| Stash work in progress | git stash / git stash pop |
Once you’re comfortable with these basics you’ll be ready to collaborate on any Python project.