Python · Module 13 · Lesson 41 min read

Version Control with Git

What Git does

Git records snapshots of your project over time, so you can go back, compare versions, and work with others without overwriting each other's code. GitHub and GitLab host Git projects online.

Core ideas

  • Repository (repo): a project tracked by Git.
  • Commit: a saved snapshot with a message.
  • Branch: a separate line of work, e.g. feature/login.
  • Merge / Pull Request: bringing a branch's changes back into main.
  • Remote: the online copy (origin).

Daily workflow

  1. git switch -c feature/login (create a branch)
  2. Edit files
  3. git status and git diff (see what changed)
  4. git add . (stage changes)
  5. git commit -m "Add login form" (save a snapshot)
  6. git push -u origin feature/login
  7. Open a Pull Request and merge after review

Good habits

  • Small commits with clear messages in the imperative mood ("Fix crash on empty list").
  • Add a .gitignore for .venv/, __pycache__/, .env.
  • Never commit passwords or API keys. If you do, change the key immediately.

💻 Terminal commands: run them on a computer.

Example

Terminal
# One-time setup
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Start a project
git init
printf ".venv/\n__pycache__/\n.env\n" > .gitignore
git add .
git commit -m "Initial commit"

# Work on a feature branch
git switch -c feature/login
git add login.py
git commit -m "Add login form validation"

# Share it
git remote add origin https://github.com/you/project.git
git push -u origin feature/login

# Useful commands
git status
git log --oneline --graph
git diff
git pull