← Lesson 10

Lesson 11 · Python

Git & GitHub

~35 min

1. Version Control and Git

Version control is a system that records changes to files over time so you can recall specific versions later. **Git** is a command-line tool that manages your project history. Unlike sending file copies like project_v2_final.py, Git tracks lines of edits incrementally. **GitHub** is a cloud hosting platform for your Git repositories, allowing you to share code, showcase your portfolio, and collaborate.

2. The Three Stages of Git

Git works in a cycle of three distinct environments:

  • Working Directory: Your local folders on your disk where you are currently editing files.
  • Staging Area: An intermediate buffer space where you select and group specific changes to prepare them for a commit. You stage edits using git add.
  • Repository (Commit History): The permanent database containing all committed snapshots. You commit changes using git commit.

3. Essential Git Commands

# 1. Initialize a new local Git repository
git init

# 2. Check the status of files (untracked, modified, or staged)
git status

# 3. Add a file to the Staging Area
git add main.py
git add .  # Adds all modified files in the directory

# 4. Commit staged files with a descriptive message
git commit -m "Add user auth logic to entry script"

# 5. Review commit history log
git log --oneline

4. Ignoring Files: The .gitignore File

You should never commit virtual environments (venv/), dependencies folders (node_modules/), or files containing API keys and database passwords (.env). You create a text file named exactly .gitignore in the root of your project to specify which files and directories Git should ignore.

# .gitignore content example:
.env
__pycache__/
*.log
venv/

5. Common Beginner Mistakes

⚠️ Watch out for these mistakes:

  • Useless Commit Messages: Writing messages like git commit -m "stuff" or "fix". Write descriptive, imperative messages: "Refactor loops for faster execution".
  • Forgetting to Add before Commit: Modifying files and running git commit without running git add. Git will tell you "nothing to commit, working tree clean".
  • Committing Massive Folders: Committing virtual environments or dependency directories, swelling your repository size. Always configure your .gitignore first.
Interactive Practice

Mini Practice: Git Commits Flow

Plan out a mock Git workspace commands sequence. You create a new file named main.py, stage it, configure a .gitignore to ignore log files, and commit all changes. Write the commands in sequence below.

0% complete
Check when done
Quiz

Test Your Understanding

1. Which command prepares modified files for a commit by moving them to the staging area?

2. What is the purpose of the `.gitignore` file in a project?

3. Which git command shows the history of all commits made in a repository?

🚀 Progression

Git version control is completed. You are ready to explore the final module: **Career Direction & Portfolios** in Lesson 12.