Skip to content

11.4 Git Version Control Basics

Managing Code, Protecting Your Work


Why Git?

  • Track code changes
  • Revert to previous versions
  • Multi-person collaboration
  • Backup code

Basic Operations

Initialize Repository

bash
# Initialize
git init

# Configure user information
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Basic Workflow

bash
# 1. Check status
git status

# 2. Add files
git add analysis.py
git add .  # Add all files

# 3. Commit
git commit -m "Add data analysis script"

# 4. View history
git log
git log --oneline

Common Commands

bash
# View differences
git diff

# Revert changes
git checkout -- file.py  # Discard unstaged changes
git reset HEAD file.py   # Unstage

# View commit history
git log --oneline
git show <commit-id>

# Revert to specific version
git reset --hard <commit-id>

GitHub Collaboration

bash
# Clone repository
git clone https://github.com/username/repo.git

# Push to remote
git push origin main

# Pull updates
git pull origin main

Best Practices

1. .gitignore

# .gitignore file
*.pyc
__pycache__/
.ipynb_checkpoints/
data/
.env
*.log

2. Commit Message Standards

bash
# Good commit messages
git commit -m "Add income data cleaning function"
git commit -m "Fix age filtering bug"

# Poor commit messages
git commit -m "update"
git commit -m "fix"

3. Common Workflow

bash
# Daily workflow
git status          # Check status
git add .           # Add all changes
git commit -m "Describe changes"  # Commit
git push            # Push to remote (if applicable)

Social Science Research Project Example

research_project/
├── .git/
├── .gitignore
├── README.md
├── data/              # Don't upload (.gitignore)
│   ├── raw/
│   └── processed/
├── code/
│   ├── 01_data_cleaning.py
│   ├── 02_analysis.py
│   └── 03_visualization.py
├── notebooks/
│   └── exploratory_analysis.ipynb
├── outputs/
│   ├── figures/
│   └── tables/
└── requirements.txt

Congratulations on Completing All 40 Lessons!

You Have Mastered:

  • Python basic syntax
  • Data structures and functions
  • File operations and exception handling
  • Pandas data analysis
  • Data visualization
  • Machine learning and LLM APIs
  • Best practices

Next Steps:

  1. Practice projects (analyze real data)
  2. Read others' code (GitHub)
  3. Contribute to open-source projects
  4. Continuous learning

Begin your Python data analysis journey!


Happy Learning!

Released under the MIT License. Content © Author.