DEV Community

📚 Git & GitHub Series
➡️ You're reading Part 3
⬅️ Part 2: Setting Up Git and GitHub
Getting Started with Git Tracking
Create a folder named Git-Learning
and add a file (e.g. hello.txt
) with some text.
At this point, Git is not tracking this folder or file. Git doesn’t track every folder on your machine by default — you need to initialize it.
Initializing Git
To allow Git to start tracking this project, run:
git init
Git now tracks this folder.
You'll see a U next to the file name, meaning Untracked.
How Does Git Track?
Once git init
is run, Git creates a hidden .git
folder — the brain of Git. It contains everything about your project's history.
Staging Files
When a file needs to be tracked it is moved over to the Staging Area.
To track a specific file:
git add hello.txt
To track all files:
git add .
Now files will show A, meaning Added to staging.
What’s Inside the .git Folder?
Some key components:
-
index
— tracks files in the staging area. -
objects
— where Git stores commit data. -
HEAD
— points to the current branch or commit.
Let's try reading the index file:
Use this to view what's staged or untracked:
git status
Git reads from the index file to show you what's staged, modified, or untracked.
Creating a Commit
A commit is like a snapshot of your code at a specific moment:
git commit -m "Initial commit"
Each commit generates a unique hash ID and stores:
- Author name and email
- Date and time
- Commit message Reference to the previous commit (if any)
To view the commit history:
git log
Making Changes
Edit hello.txt
, then check what changed:
git diff
Git tracks the difference between the working directory and staging area using metadata inside .git.
Diving into the Objects Folder
Inside .git/objects
, Git stores:
- Blobs (file content)
- Trees (directories)
- Commits (snapshots)
The folder names are based on the first 2 characters of the SHA-1 hash of the file or commit.
Viewing Commit Data
You can use the commit hash to view commit details:
Notice, I can now see the who committed details, using the commit Hash ID.
Now I want to see the content in the file;
I will now use the blob ID to see the content in “file1.txt” that was committed.
Git stores everything in the .git folder — and you rarely need to manually look inside, because Git gives us tools like:
- git stat
- git diff
- git log
Committing
Create another file, say file2.txt, add some content, then:
git add file2.txt
git commit -m "Added file2.txt"
Check the commit log again:
git log
You'll notice the HEAD is now pointing to the latest commit (Commit 2).
How Git Stores Commits
Every commit contains a SHA-1 hash, generated based on:
- The content of the files
- Previous commit
- Commit message
- Metadata
This ensures every commit is unique, even if you commit the same code again, you’ll likely get a different hash.
🧠 Git = Content-addressable storage.
Fun Fact: Git Meets Algorithms
Spotify once used an algorithm named MAYA (Most Advanced Yet Acceptable) to recommend music.
While it was supposed to suggest songs related to your taste, a bug caused it to occasionally show completely unrelated tracks, and surprisingly, users loved the variety!
Similarly, Git’s algorithms may look simple, but the power lies in how uniquely and efficiently it stores and suggests changes.
Traveling Between Commits
To navigate through your commit history, first run:
git log --oneline
To compare differences between two commits:
git diff <commit1> <commit2>
Moving HEAD to a Previous Commit
Want to move back in time?
You can checkout an earlier commit using its hash:
git reset <commit-hash>
As we move the head to commit 1, the files that were in the latest commit (Commit 2) are in Changes now.
If you don't want the changes then;
git reset --hard <commit-hash>
Notice because file 2, was added after the second commit, it appears as a new Untracked file, even though it was committed.
To verify:
git log
GitHub: The Remote Repository
Sharing the .git
folder allows others to see your commits. Instead, use GitHub!
Initially both developers will have the same content in “file1.txt”. Now when they both begin to work on it simultaneously there will be conflicts as both as using the same file. Now which file is the Source Of Truth ? For that pen drive won’t be needed, we can use Github.
Create a GitHub repo (e.g. Git-Masterclass
)
Then, link your local project:
Use the push an existing repository from the command line commands
git remote add origin https://github.com/yourname/Git-Masterclass.git
Verify the remote was added by viewing the config file within the .git folder
To see the URLS of the remotes connected to the local git repository, both for fetching(pull) and pushing.
Follow the remaining commands on GitHub:
git branch -M main git push -u origin main
Now visit Github will be able to see “Commit 1”, which was on our local machine is now on github.
Wrapping Up
In this post, you learned how:
- How Git tracks and stages files
- How commits and hashes work
- What lives inside the
.git
folder - How to view, reset, and compare commits
- How to push changes to GitHub
📚 More from the Series
- Part 1: Why Git & GitHub Matter
- Part 2: Setting Up Git and GitHub
- ✅ Part 3: You're here!
- Part 4: Branching, Merging, Squashing & Rebasing
- Part 5: Working with Pull Requests & Conflict Resolution
Enjoyed this?
Let me know in the comments or follow me for the next post in this Git & GitHub series!
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)