Git for Python Developers: A Complete Practical Guide
If you write Python code, learning Git is one of the highest-leverage skills you can pick up.
Git lets you track changes, collaborate with others, and recover from mistakes without fear. In this guide,
we'll walk through Git concepts and commands using real Python project examples — from your very first
git init to managing branches
and pushing to GitHub.
1. What is Git?
Git is a distributed version control system (VCS). It records snapshots of your project over time, so you can:
- Track every change made to your Python files
- Revert back to an earlier working version if something breaks
- Work on new features in isolated branches
- Collaborate with other developers without overwriting each other's work
Features of Git
Version tracking: Records every change made to files, allowing you to review or restore previous versions. Distributed architecture: Every developer has a complete copy of the repository, including its history, enabling offline work. Branching and merging: Developers can create branches to work on new features or bug fixes independently, then merge those changes back into the main codebase. Collaboration: Multiple developers can work on the same project simultaneously with minimal conflicts. Fast and efficient: Git is designed to handle projects of all sizes with high performance.
Common Git conceptsRepository (Repo): A project and its complete version history. Commit: A snapshot of changes saved to the repository. Branch: An independent line of development. Merge: Combines changes from one branch into another. Clone: Creates a local copy of a remote repository. Pull: Downloads and integrates changes from a remote repository. Push: Uploads local commits to a remote repository.
Git is different from platforms like GitHub or GitLab. Git is the tool; GitHub is a hosting service that stores your Git repositories online.2. Installing Git
Check if Git is already installed:
git --version
If not installed:
- Windows: Download from git-scm.com
- Mac:
brew install git - Linux (Debian/Ubuntu):
sudo apt install git
3. Setting Up Git
Before your first commit, tell Git who you are:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
4. Creating a Python Project with Git
Let's start a simple Python project and turn it into a Git repository.
# Create a project folder
mkdir python-calculator
cd python-calculator
# Initialize Git
git init
# Create a Python file
echo "def add(a, b):
return a + b" > calculator.py
Now check the status of your repository:
git status
You'll see calculator.py listed as an untracked file.
5. The Core Git Workflow
Git's basic workflow revolves around three areas:
| Area | Description |
|---|---|
| Working Directory | Your actual files on disk (e.g., calculator.py) |
| Staging Area (Index) | Files marked to be included in the next commit |
| Repository (.git) | Permanent history of committed snapshots |
# Stage the file
git add calculator.py
# Commit the change
git commit -m "Add basic calculator with add function"
6. Ignoring Python-Specific Files with .gitignore
Python projects generate a lot of files you shouldn't commit — compiled bytecode, virtual environments,
caches, etc. Create a .gitignore file:
__pycache__/
*.pyc
*.pyo
venv/
.env
.vscode/
*.egg-info/
dist/
build/
Then commit it:
git add .gitignore
git commit -m "Add .gitignore for Python project"
7. Viewing History
git log
git log --oneline
git log --oneline --graph --all
8. Branching for New Features
Suppose you want to add a subtract() function without touching the working
main branch:
# Create and switch to a new branch
git checkout -b feature/subtract-function
# Edit calculator.py to add:
# def subtract(a, b):
# return a - b
git add calculator.py
git commit -m "Add subtract function"
Switch back to main and merge:
git checkout main
git merge feature/subtract-function
9. Connecting to GitHub
# Link your local repo to a remote GitHub repo
git remote add origin https://github.com/yourusername/python-calculator.git
# Push your code
git branch -M main
git push -u origin main
10. Cloning an Existing Python Repository
git clone https://github.com/someuser/some-python-project.git
cd some-python-project
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
11. Undoing Mistakes
| Situation | Command |
|---|---|
| Unstage a file | git restore --staged file.py |
| Discard changes in a file | git restore file.py |
| Undo last commit (keep changes) | git reset --soft HEAD~1 |
| Undo last commit (discard changes) | git reset --hard HEAD~1 |
12. Using Git with a Python Virtual Environment
Best practice: keep venv/ out of version control, but track dependencies:
pip freeze > requirements.txt
git add requirements.txt
git commit -m "Add project dependencies"
13. Automating Git with Python (GitPython)
You can even control Git programmatically using the GitPython library — useful for build
scripts, automation tools, or CI pipelines.
pip install gitpython
from git import Repo
# Open an existing repo
repo = Repo(".")
# Check current branch
print(repo.active_branch)
# Stage and commit changes via Python
repo.git.add(A=True)
repo.index.commit("Automated commit from Python script")
# Push changes
origin = repo.remote(name="origin")
origin.push()
14. Quick Reference Cheat Sheet
git init # Start a new repo
git clone <url> # Copy a repo
git status # See changed files
git add <file> # Stage a file
git commit -m "message" # Save a snapshot
git push # Upload to remote
git pull # Download latest changes
git branch # List branches
git checkout -b <name> # Create + switch branch
git merge <branch> # Combine branches
git log --oneline # View history
Conclusion
Git might feel intimidating at first, but once you get comfortable with
add, commit, push, and branch, it becomes second nature.
Combine it with Python's own automation power (like GitPython), and you'll have a rock-solid workflow for
any personal or team project.
Have questions about Git or Python workflows? Drop them in the comments below!

0 Comments