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
git switch -c feature/login(create a branch)- Edit files
git statusandgit diff(see what changed)git add .(stage changes)git commit -m "Add login form"(save a snapshot)git push -u origin feature/login- 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
.gitignorefor.venv/,__pycache__/,.env. - Never commit passwords or API keys. If you do, change the key immediately.
💻 Terminal commands: run them on a computer.
Example
# 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