What is Git?
Git is a distributed version control system that tracks changes in your code over time. It allows you to save snapshots of your project, collaborate with others, and revert to previous versions if something goes wrong.
Think of Git like a time machine for your code. Every time you save (commit), you create a checkpoint you can return to at any time.
Why Use Git?
- Track changes: See exactly what changed, when, and by whom
- Undo mistakes: Revert to any previous state
- Collaborate: Work with others without overwriting each other's work
- Branches: Experiment safely without affecting main code
- Backup: Your code is stored in multiple places
- Industry standard: Every developer needs to know Git
Getting Started
# Install Git
# Windows: Download from git-scm.com
# Mac: brew install git
# Linux: sudo apt install git
# Configure Git (one-time setup)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Create a new repository
mkdir my-project
cd my-project
git init
# Or clone an existing repository
git clone https://github.com/user/repo.git
Basic Git Workflow
┌─────────────┐ git add ┌─────────────┐ git commit ┌─────────────┐
│ Working │ ────────────> │ Staging │ ─────────────> │ Repository │
│ Directory │ │ Area │ │ (History) │
└─────────────┘ └─────────────┘ └─────────────┘
1. You modify files in your working directory
2. You stage changes you want to commit (git add)
3. You commit staged changes to history (git commit)
4. You push commits to remote (git push)
Essential Commands
# Check status of your files
git status
# Stage files for commit
git add file.py # Add specific file
git add . # Add all changes
git add -p # Add interactively
# Commit changes
git commit -m "Add user authentication"
# View commit history
git log # Full history
git log --oneline # Compact view
git log --graph # Show branch structure
# See what changed
git diff # Unstaged changes
git diff --staged # Staged changes
git diff HEAD~1 # Compare with previous commit
# Undo changes
git checkout -- file.py # Discard changes to file
git reset HEAD file.py # Unstage a file
git reset --hard HEAD~1 # Undo last commit (careful!)
# Work with remote
git remote add origin https://github.com/user/repo.git
git push -u origin main # Push to remote
git pull # Get latest from remote
git fetch # Download without merging
Branching
Branches let you work on features without affecting the main code.
# Create and switch to a new branch
git branch feature-login # Create branch
git checkout feature-login # Switch to branch
# Or in one command:
git checkout -b feature-login
# List branches
git branch # Local branches
git branch -a # All branches (including remote)
# Switch between branches
git checkout main
git checkout feature-login
# Merge a branch into current branch
git checkout main
git merge feature-login
# Delete a branch
git branch -d feature-login # Safe delete (merged only)
git branch -D feature-login # Force delete
# Typical workflow
git checkout -b feature-x # Create feature branch
# ... make changes ...
git add .
git commit -m "Implement feature X"
git push -u origin feature-x # Push branch
# Create Pull Request on GitHub
# After merge, clean up:
git checkout main
git pull
git branch -d feature-x
Merging and Conflicts
# Merge another branch into current
git merge feature-branch
# If there are conflicts, Git will tell you
# Open the file and look for conflict markers:
<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> feature-branch
# Resolve by editing the file, then:
git add resolved-file.py
git commit -m "Resolve merge conflict"
# Alternatively, use a merge tool
git mergetool
# Abort a merge if it's too messy
git merge --abort
Common Scenarios
# Oops, I committed to the wrong branch
git checkout correct-branch
git cherry-pick <commit-hash>
git checkout wrong-branch
git reset --hard HEAD~1
# I need to update my branch with latest main
git checkout my-feature
git rebase main
# Or merge:
git merge main
# I need to save my work but not commit
git stash # Save changes
git stash list # See stashed changes
git stash pop # Apply and remove stash
git stash apply # Apply but keep stash
# I made a typo in my last commit message
git commit --amend -m "Fixed message"
# I forgot to add a file to my last commit
git add forgotten-file.py
git commit --amend --no-edit
# I need to undo a pushed commit
git revert <commit-hash> # Creates new commit that undoes changes
Git with GitHub
# Fork a repository on GitHub, then:
git clone https://github.com/your-username/repo.git
cd repo
# Add upstream remote to get updates from original
git remote add upstream https://github.com/original/repo.git
# Keep your fork updated
git fetch upstream
git checkout main
git merge upstream/main
git push
# Create a pull request workflow
git checkout -b fix-bug
# ... make changes ...
git push -u origin fix-bug
# Go to GitHub and create Pull Request
.gitignore
Tell Git which files to ignore:
# .gitignore for Python projects
__pycache__/
*.py[cod]
*$py.class
.Python
venv/
env/
.env
*.egg-info/
dist/
build/
# IDE
.vscode/
.idea/
*.swp
# Testing
.coverage
htmlcov/
.pytest_cache/
# Local settings
*.local
config.local.py
Best Practices
- Commit often: Small, focused commits are easier to understand and revert
- Write good messages: Explain why, not just what
- Use branches: Never commit directly to main
- Pull before push: Get latest changes first
- Review your changes: Use
git diffbefore committing - Don't commit secrets: Use .gitignore for .env files
# Good commit messages
git commit -m "Add password validation to login form"
git commit -m "Fix #123: Handle empty user input"
# Bad commit messages
git commit -m "Fix bug"
git commit -m "Update"
git commit -m "asdfasdf"
Master Git with Expert Mentorship
Our Full Stack Python program covers Git workflows and collaboration. Learn professional version control practices with personalized guidance.
Explore Full Stack Python Program