Advanced Git Mastery – Second edition 2025


Git helps you work smarter and safer. With git reflog
, you can recover lost commits, and tools like git clean
and git gc
help you remove extra files and speed up your project. Git aliases let you create shortcuts for commands, saving you time every day. Exploring the .git
folder gives you insight into how Git manages your project internally.
For team projects, choosing the right Git workflow is key. Git Flow, GitHub Flow, and Trunk-Based Development each support different types of teams and release styles. Writing clear commit messages, squashing fixes, and using pull requests are all best practices that keep your code clean and easy to review.
1. Recovering with Reflog (git reflog
)
What is git reflog
?
It tracks every move of your HEAD, even those Git doesn't normally show.
If you accidentally delete a commit, reset a branch, or mess something up, reflog
can save your day.
Real-World Example: Time Machine for Your Code
Imagine you’re writing a book. Every time you print a copy (commit), you store it.
One day, you get confused and:
Rip out a few pages
Or throw away the last version
Or accidentally go back to an older version
You think: “Oh no! I lost my best work!”
That’s where git reflog
comes in!
git reflog
It shows a history like:
a1b2c3d HEAD@{0}: reset: moving to previous version
f4e5f6g HEAD@{1}: commit: Add contact form
c7d8e9h HEAD@{2}: commit: Fix login issue
Each line = a step you took (checkout, commit, reset, merge, etc.)
How to Recover
1. See the history:
git reflog
2. Choose the commit you want to go back to (copy the hash)
3. Restore it:
- Just look at the old state:
git checkout <commit-id>
- Or move your branch back completely:
git reset --hard <commit-id>
Be careful: --hard
will overwrite current working files.
When to Use git reflog
You did
git reset --hard
and lost workYou force-pushed or deleted a branch accidentally
You want to go back to any previous state, even if it’s not in
git log
Sooooo, Now you better understand the magic of GIT and reflog 😉
2. Cleaning Up with git clean
, git gc
1. git clean
– Remove Untracked Junk
When you're working on a project, sometimes you create extra files or folders that aren’t tracked by Git, like:
Temporary files (
.log
,.tmp
, etc.)Build output (e.g.,
dist/
,node_modules/
, etc.)Mistakenly added files
These don’t show up in git status
as staged or committed, but they clutter your folder.
Real-World Analogy:
Imagine your desk has:
Files you've already filed in drawers (committed files)
Files you’ve decided to file next (staged files)
Sticky notes, wrappers, and paper scraps all over (untracked files)
You use git clean
to throw away those scraps.
Commands:
git clean -f # Clean untracked files only
git clean -fd # Clean untracked files AND folders
Be careful – this deletes files permanently.
To preview before deleting, use:
git clean -n
2. git gc
– Optimize Your Repository
Over time, Git stores:
Deleted commits
Unreachable branches
Old pack files
These pile up and make your repo heavier.
Think of It Like:
Defragmenting your hard drive or cleaning up cache, it keeps things fast and tidy.
Command:
git gc
This stands for Garbage Collection.
It will:
Compress files
Remove unnecessary objects
Optimize Git's internal storage
3. Git Aliases to Boost Productivity
Speed up typing with aliases.
Example:
git config --global alias.st status
git config --global alias.co checkout
Now use:
git st
git co main
Save hours over time!
4. Exploring the .git
Folder – The Brain of Your Git Repo
What is .git
?
When you run git init
, Git creates a hidden folder called .git
inside your project.
This folder is like the brain and memory of your Git project.
It contains everything Git needs to track changes, branches, commits, and settings.
Real-World Analogy:
Think of your Git project like a house, and the .git
folder is the control room in the basement.
You don’t see it during daily use
But it runs everything behind the scenes
If you delete it, you lose all history
Let’s Explore It
Run:
ls .git
You’ll see something like this:
HEAD
config
objects/
refs/
logs/
index
Important Parts Explained:
Folder/File | What It Does |
objects/ | Stores all Git data (commits, files, trees) |
refs/ | Contains branches and tags |
HEAD | Points to your current branch |
config | Local Git settings for this repo |
logs/ | Stores reflog history – all HEAD movements |
index | The staging area snapshot |
Tip:
Do NOT edit files in .git
manually, unless:
You really know what you're doing
You're debugging or recovering a repo
One wrong change can corrupt your Git repo.
5. Best Practices for Teams
Writing Good Commit Messages
Start with a capital letter
Keep the first line under 50 characters
Describe what and why
git commit -m "Fix login redirect on mobile"
Reviewing and Squashing Commits
Use Pull Requests to review code. Use:
git rebase -i
to squash “fix” commits before merging.
Handling Large Files
Git isn't built for binary files or large assets. Use:
git lfs install
git lfs track "*.psd"
Git LFS (Large File Storage) stores them efficiently outside your repo.
Final Thoughts
Git is more than just version control, it's a toolbox for collaboration, automation, and code safety. Mastering these advanced tools can drastically improve your efficiency, team collaboration, and confidence during critical development tasks.
Missed the basics? Git for Beginners
Or continue from intermediate level: Git Intermediate Guide
Or continue from advanced level Part 1: Git Advanced Guide Part 1
Or continue from advanced level Part 2: Git Advanced Guide Part 2
Subscribe to my newsletter
Read articles from Laravel Daily tips directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Laravel Daily tips
Laravel Daily tips
As a FULL-Stack, TALL Stack developer, and owner of laraveldailytips.com, I am from Pakistan. My passion is to write short and useful tips and tricks that can assist other people who are trying to learn something new and helpful. Since the beginning, I have loved PHP, Laravel, VueJS, JavaScript, jQuery, and Bootstrap. I believe in hard work combined with consistency.