M38A - Git Basics: Version Control from CLI
Git Basics: Version Control from CLI
Master the foundational CLI commands of Git to track changes, create distinct save states (commits), and review the history of a codebase.
- Master the foundational CLI commands of Git to track changes, create distinct save states (commits), and review the history of a codebase.
The Time Machine
When writing complex scripts or configuration files, you will inevitably break something that worked perfectly yesterday.
If you do not use version control, you will have a folder full of files named script_v1.sh, script_v2_FINAL.sh, script_v2_FINAL_REALLY.sh. This is unmanageable.
Git is a time machine. It takes cryptographic snapshots of your entire project folder. If you destroy your code today, you can instantly revert the entire folder back to exactly how it looked last Tuesday.
(Git is completely platform-independent. The commands are identical on Windows, macOS, and Linux).
1. Initializing the Time Machine (git init)
You must tell Git to start watching a folder.
Create a new project folder and enter it
mkdir my_project cd my_project
Tell Git to turn this normal folder into a Repository
git init
(Optional but required once) Tell Git who you are!
git config —global user.name “Your Name” git config —global user.email “you@example.com”
When you run git init, Git creates a hidden folder called .git inside your project. This hidden folder contains the entire history of every file. If you delete the .git folder, the history is destroyed, and the folder becomes normal again.
2. The Two-Step Save (git add & git commit)
Saving a file in Git is not like pressing Ctrl+S in Notepad. It is a two-step process: Staging and Committing.
Think of “Staging” (git add) as putting items into a shipping box.
Think of “Committing” (git commit) as sealing the box and slapping a permanent label on it.
1. Create a quick file to test
echo “Version 1 of my script” > script.sh
2. Check the status of the time machine
git status
Output: Untracked files: script.sh
3. Step One: Add the file to the “shipping box” (Staging Area)
git add script.sh
4. Step Two: Seal the box with a permanent, descriptive label
git commit -m “Created the initial script file”
🧠 Best Practice: Meaningful Messages
Never write git commit -m “saving” or git commit -m “fixed stuff”.
If your server crashes at 2 AM on a Sunday, and you look at the Git history hoping to find out what changed, “fixed stuff” tells you nothing. Use imperative sentences: git commit -m “Add firewall rule to block port 80”.
3. Reviewing History (git log & git diff)
Once you have made several commits over a week, you can visualize the time machine.
Show the chronological list of every commit ever made
git log
(Press ‘q’ to quit the log view)
The log output looks something like this:
commit 5f3a09b (HEAD -> main)
Author: Admin <admin@company.com>
Date: Mon Oct 24 10:00:00 2024 -0400
Update the database password
That massive string (5f3a09b...) is the Commit Hash. It is a unique cryptographic ID (SHA-1) for that exact moment in time.
Seeing What Changed (Diff)
If you modify a file but haven’t committed it yet, you can ask Git to show you exactly what changed line-by-line compared to the last saved state.
echo “Added a new dangerous line” >> script.sh
Show me the exact lines added or removed
git diff
What You Just Learned
git initturns a normal folder into a time-traveling Repository.git statustells you what files are changed but not yet saved.git add <file>stages the file (puts it in the box).git commit -m ""commits the stage (seals the box with a time stamp).git logandgit difflet you review the cryptographic history of the project.
Git is the foundation of modern infrastructure. You don’t configure servers manually anymore; you write a script, commit it to Git, and an automated system deploys it.
In the next section, we finally learn how to write those automation scripts.