🧠 Git Basics – Just What You Must Know
Let’s walk through Git using a simple real-world scenario:
Imagine you’re writing a small program and want to track your changes efficiently, experiment safely, and roll back when needed. Git helps you do all of that, locally, without any remote server.
📌 What is Git?
Git is a free and open-source distributed version control system, created by Linus Torvalds in 2005 to manage the Linux kernel codebase.
Why It Matters
- It tracks changes to your codebase over time.
- It helps teams collaborate efficiently without overwriting each other’s work.
- Every developer has a complete local copy of the entire project history.
Key Concepts
- Snapshots, not differences: Git saves a snapshot of the entire project at each commit.
- Branching and merging: Work on multiple features or bug fixes in isolation.
- Local-first: Make all changes locally and push when you’re ready to share.
🧪 Practice:
- Try using a text editor to make a small change in a file and manually save versions with different names.
- Now imagine Git doing that automatically, but much smarter and more efficient.
⚙️ First-Time Git Setup
Before using Git, you need to configure your personal information and preferred editor (VS Code in this example):
|
|
user.name
anduser.email
will be attached to every commit you make.core.editor
sets VS Code as your default Git editor.git config --list
confirms your global settings.
🧪 Practice:
- Run the config commands and check with
git config --list
. - Try changing your name and verify the update.
🔧 Basic Git Commands
📁 Initialize a Repository
Start tracking a folder with Git:
This creates a hidden .git
folder — Git’s database for tracking your project. You can now begin version controlling this folder.
🧪 Practice:
- Create a test folder on your machine and initialize it with
git init
. - Use
ls -a
to view the hidden.git
folder.
Clone an Existing Repo
Instead of starting from scratch, clone a remote repository:
|
|
This downloads the project, initializes Git, and sets the remote origin.
🧪 Practice:
- Try cloning any public GitHub repo, like
https://github.com/octocat/Hello-World.git
.
More Examples:
|
|
➕ Stage & Commit Changes
One of the key steps in using Git is saving your work in stages. First, you “stage” the files — this tells Git which changes you want to include in the next snapshot (commit). Then you “commit” those staged changes, creating a permanent record in your project’s history.
Think of it like this:
- You’re editing a document (working directory).
- You select what you want to save (staging area).
- Then you save it (commit) to your project history.
git add .
stages all modified and new files.git commit
records the current snapshot of staged changes.
More Useful Variants:
When to Use Them:
- Use
git add file.txt
to add a specific file instead of everything. - Use
git commit -am
for a quick save if you’re working with tracked files. - Use
git add -p
when you only want to commit part of your changes in a file — great for splitting up work.
🧪 Practice:
- Create a
hello.txt
file, write something, and usegit add
andgit commit
to save it. - Edit the file again, then use
git add -p
to stage only a portion of your changes. - Try
git commit --amend
to edit the last commit message or add forgotten files. changes and record them:
git add .
stages all modified and new files.git commit
records the current snapshot of staged changes.
Other Useful Variants:
🧪 Practice:
- Create a
hello.txt
file, write something, and usegit add
andgit commit
to save it. - Run
git log
to see your commit.
🔍 Monitor Your Work
Check status and view your commit history:
git status
: Shows what’s staged, unstaged, or untracked.git log
: Shows commit history with author, date, and message.
More Examples:
|
|
🧪 Practice:
- Modify a tracked file and use
git status
to see the change. - Use
git log
after several commits and note the difference between full and one-line views.
🔀 Branching
Why Use Branches?
Imagine working on a new feature like “dark mode” while your main branch is stable. Branches let you experiment safely.
Or with the modern command:
|
|
Switch back to main
with:
|
|
More Commands:
🧪 Practice:
- Create a branch called
feature-test
, switch to it, and make a commit. - Switch back to
main
and confirm your changes aren’t visible there.
⇂ Merging Changes
After testing in a branch, merge it into the main codebase:
Git will try to automatically merge changes. If there are conflicts, Git will prompt you to resolve them manually.
More Examples:
🧪 Practice:
- Edit the same file on both
main
and your feature branch. Merge to observe a conflict. - Use a code editor or
git status
to resolve the conflict and finish the merge.
⏺ Undo / Revert
Undoing in Git has layers. Here’s how to handle different situations.
Discard Local Changes (Unstaged)
|
|
Reverts file.txt
to the last committed version.
Unstage a File
|
|
Removes file.txt
from staging, but keeps your working copy.
Soft Reset (Keep Changes Staged)
|
|
Moves back one commit, but keeps your changes staged.
Mixed Reset (Unstage Changes)
|
|
Moves back one commit, keeps changes but unstaged.
Hard Reset (Remove Everything)
|
|
Moves back one commit and discards all local changes.
Revert a Commit
|
|
Creates a new commit that undoes the effect of a previous one.
More Useful Examples:
🧪 Practice:
- Commit a dummy file. Try
--soft
and--hard
resets. Observe staged vs. removed content. - Use
git revert
on an earlier commit and note the new reversal commit.
⏲ Navigate to Previous Commits
Sometimes you need to explore or revert to an earlier state of your project — for debugging, testing, or recovering lost progress. Git allows you to move through the history of commits with ease.
To view or test older versions of your project without affecting current progress, use:
|
|
- This puts you in detached HEAD mode, meaning you’re viewing an old snapshot of your code.
- You can explore, run, and even make changes (though these changes won’t be saved to a branch unless explicitly committed to one).
To return to your working branch, simply run:
|
|
To rewind your current branch to a previous state:
|
|
- This resets the branch to a specific commit and deletes all changes after that commit.
More Examples:
|
|
|
|
|
|
|
|
🧪 Practice:
- Use
git log --oneline
to list recent commits. - Pick one hash and run
git checkout <hash>
to view that version. - Try switching back using
git switch main
. - Experiment with
git reset
variants and note how each behaves with your files and staging.
You can go back in time to explore or test past versions:
|
|
- Detached HEAD mode allows inspection without altering history.
Return to latest:
|
|
Reset back to a specific commit:
|
|
More Examples:
🧪 Practice:
- Use
git log
to find an earlier commit and checkout that commit. - Return to your latest commit with
git switch main
.
🧩 Real-World Use Cases
- New Feature: Branch off, build, test, and merge.
- Oops Moment: Use
git reset
,restore
, orrevert
to undo. - Explore Past: Use
git log
,checkout
, andreflog
to navigate. - Track Progress:
git status
,diff
, andlog
keep you informed.
✅ Next Steps
Now that you’re comfortable with Git basics, you’re ready for more advanced workflows like branching strategies, rebasing, cherry-picking, and using tags.