Git Basics: The Big Three Commands
Saving code with Git is a bit different than hitting Ctrl + S in Word. It’s more like a Photography Session.
Imagine you are a photographer:
- The Pose: You arrange your subjects (Stage your files).
- The Click: You take the photo (Commit the snapshot).
- The Gallery: You upload the photo to your online portfolio (Push to GitHub).
The Three Stages of Git
Before we learn the commands, you need to visualize where your code "lives" during the saving process.
- Working Directory: This is your "Work Desk." Any changes you make in VS Code happen here.
- Staging Area: This is your "Shopping Cart." You put the changes here that you are ready to save.
- Local Repository: This is your "Vault." Once you commit, your changes are safely locked in the history of your computer.
The Commands You'll Use 90% of the Time
Open your terminal in VS Code (Press Ctrl + `) and let's walk through a save point.
Step 1: git add (The Selection)
This tells Git: "Hey, look at these specific changes I just made."
git add index.html
Use git add . to add every file you changed at once!*
Step 2: git commit (The Snapshot)
This creates the actual "Save Point" in your timeline. You must add a message so you know what you did.
git commit -m "Added a cool new button to the homepage"
Step 3: git push (The Upload)
This sends your local "Vault" up to the cloud (GitHub).
git push origin main
Visualizing the Flow
Interactive Lab: Git Command Simulator
Think you’ve got it? Let's test your logic. If you were building a website for CodeHarborHub, in what order would you do these tasks?
- Modify the
style.cssto make the background blue. - Type
git add style.cssto put it in the cart. - Type
git commit -m "Changed background to blue". - Type
git pushto show it to your team.
What happens if you skip Step 2? Git will tell you: "Nothing added to commit." You can't take a photo if nobody is standing in front of the camera!
Checking the Status
If you ever feel lost and don't know if you saved your work, use this "GPS" command:
git status
- Red Files: You changed them, but haven't "Added" them yet.
- Green Files: They are in the "Cart" (Staged) and ready to be committed.
- "Nothing to commit": You are all caught up!
Summary Checklist
- I know that
git addputs files in the Staging Area. - I know that
git commitcreates a permanent Save Point. - I understand that
git pushsends my work to GitHub. - I can use
git statusto check my progress.