The Problem: "final_version_v2_APPROVED_real_final.zip"
Have you ever found yourself saving multiple versions of the same file just to keep track of changes? It's a chaotic system that quickly becomes unmanageable, especially when working in a team. What if you make a mistake and need to go back to a version from three days ago? What if a teammate accidentally deletes a critical feature? This is the problem that Version Control solves.
What is Version Control? What is Git?
- A Version Control System (VCS) is software that records every change made to your project files over time. Think of it as a "save point" in a video game for your entire project. It allows you to revert to any previous version, compare changes, and see who changed what and when.
- Git is the world's most popular, modern, distributed version control system. "Distributed" means that every developer has a full copy of the project's history on their local machine, allowing for powerful offline work and collaboration.
Core Git Concepts (The Vocabulary)
To understand Git, you need to know four key terms:
- Repository (Repo): The project's "database" that contains all your files and the entire history of their changes. This is stored in a hidden
.git
folder in your project root. - Commit: A single "snapshot" or "save point" of your project at a specific moment in time. Every commit has a unique ID and a message describing the changes made.
- Branch: A parallel line of development. The main branch is usually called
main
. You create new branches to work on new features without disrupting the stable, main version of your code. - Merge: The action of combining the changes from one branch back into another (e.g., merging your completed "new-feature" branch back into the
main
branch).
The Basic Git Workflow (The Three Stages)
Making a save (a commit) in Git is a three-step process. This is the most important workflow to understand.
- Working Directory: This is simply your project folder where you edit, add, and delete files.
- Staging Area (Index): Before you commit, you must add your changes to a "staging area." This lets you choose exactly which changes you want to include in the next commit. It's like putting items in a box before you seal it.
- Command:
git add <file-name>
- Command:
- The Repository: Once your changes are staged, you "commit" them. This takes the snapshot from the staging area and saves it permanently to your project's history in the repository.
- Command:
git commit -m "A descriptive message about your changes"
- Command:
Your Mission: Your First Commits
Let's practice this workflow in our mvc-project
folder from the previous module.
Step 1: Initialize a Repository
Open your terminal, navigate to the mvc-project
directory, and run this command. It turns your project into a Git repository.
git init
Step 2: Check the Status
This is your most-used command. It tells you the current state of your repository.
git status
You'll see a list of "untracked files." Git sees your project files but isn't tracking their history yet.
Step 3: Make Your First Commit
Let's stage all our project files and make our first commit.
# The '.' means "add all files in the current directory and subdirectories"
git add .
# Now, commit the staged files with a message
git commit -m "Initial commit: Setup basic MVC project structure"
Run git status
again. It will now say "nothing to commit, working tree clean."
Step 4: Make a Change and a New Commit
Open public/index.php
and add a comment, like // Our awesome MVC app
. Save the file. Now run git status
again. Git will tell you that index.php
has been modified!
Let's commit this new change.
# Stage the specific file
git add public/index.php
# Commit the change
git commit -m "Docs: Add a comment to the entry point file"
Step 5: View the History
To see the log of all your commits, run:
git log
You will see your two commits, each with its unique ID, author, date, and commit message.
Conclusion
You've just learned the fundamental workflow of Git! This process of add
and commit
is something developers do many times every day. It provides a safety net for your work and is the first step towards collaborating with other developers on platforms like GitHub.