Git Gud: Learning Git to survive your first month 🚀
5 min read
Table of Contents
- Basic Workflow: init, clone, status, add, commit, push, pull
- Going Deeper: log, reflog, merge, rebase, fetch, reset, cherry-pick, aliases
- 1. git log: Looking Back in Time
- 2. git reflog: Recover from the Unrecoverable
- 3. git merge & git rebase: Navigating the Big Leagues
- 4. git fetch: See Without Touching
- 5. git push: Beyond the Basics
- 6. git reset: Correcting Mistakes
- 7. git cherry-pick: Selective Commit Application
- 8. Git Aliases: Work Smarter, Not Harder
- Final Thoughts
Getting started with Git?
It's pretty easy, tbh. It's literally as simple as git clone
, git commit
, git push
... Wait, I need to pull before I push? Google says I should rebase or something. Wait, where did my changes go? Oh god I was supposed to ship this today! OH SH*T WHAT IS HAPPENING?!?!?
I got your back.
This post covers the basics, and some 🍑 saving measires.
Basic Workflow: init
, clone
, status
, add
, commit
, push
, pull
Let’s get these out of the way fast. These are the bread and butter of Git. You’ll use them in almost every project:
git init
: Start version control in a new project.git clone
: Copy an existing project to your machine.git status
: See what’s changed in your working directory.git add
: Stage changes to be committed.git commit
: Save those changes with a helpful message 📝.git push
: Send your changes to the remote repo (like GitHub) 🚀.git pull
: Fetch and merge updates from the remote repo into your local one.
This is your life for the next few weeks. Add a meme here of a person looking at their terminal, overwhelmed by all the commands 😅. Now, let’s kick things up a notch.
Going Deeper: log
, reflog
, merge
, rebase
, fetch
, reset
, cherry-pick
, aliases
1. git log
: Looking Back in Time
git log
is more than just a history view—it's a window into the soul of your code 👀. Want to narrow things down?
Find by author:
git log --author="Your Name"
Check only specific files:
git log -- file.js
Check changes within a time frame:
git log --since="2 weeks ago"
Combine it all for targeted queries:
git log --author="Your Name" --since="2 weeks ago" --oneline --graph
For complex histories, the --graph
option is the best thing since sliced bread 🍞—it shows how branches and merges evolved. Insert a meme of a messy Git graph here for a laugh!
2. git reflog
: Recover from the Unrecoverable
If Git were a video game, git reflog
would be your “undo” cheat code 🎮. It tracks every change you make, even when git log
can't help. Accidentally wiped out a commit with reset --hard
? No worries.
git reflog
git reset --hard HEAD@{2} # Go back to the state of the repo before the mistake
3. git merge
& git rebase
: Navigating the Big Leagues
These are the strategies for bringing together different branches. Both are super powerful, but they serve different purposes:
git merge
: Combines two branches and creates a merge commit. If you want to keep the full history intact and visible, merge is your friend.git merge feature-branch
Use
--no-ff
if you want to always create a merge commit, even when Git could fast-forward:git merge --no-ff feature-branch
git rebase
: Moves your branch on top of another, making the history cleaner.git rebase main
Want to polish your commits before merging? Try an interactive rebase:
git rebase -i HEAD~3
For the unaware, the internet feels pretty... strongly about merges and rebases. In case you didn't know, you will, soon. :D
Do share what's your preference in the comments. 👇
4. git fetch
: See Without Touching
git fetch
is like peeking into the fridge to see what's inside before deciding what to eat 🍕. It updates your local copy with changes from the remote, but doesn’t mess with your working branch yet.
git fetch origin
Use it with git diff
to see how your local branch compares:
git diff origin/main
5. git push
: Beyond the Basics
While git push origin main
is the standard, here’s how you can take it further:
Push specific branches:
git push origin feature-branch
Push and set upstream (perfect for new branches):
git push -u origin feature-branch
Force pushing when you absolutely need to:
git push --force
Push specific tags:
git push origin v1.0.0
Need to delete a branch from the remote? Easy:
git push origin --delete feature-branch
6. git reset
: Correcting Mistakes
Time to fix those “oops” moments! git reset
lets you move backward in time—without a time machine. Here are the different flavors:
Soft reset keeps your changes but removes the commit:
git reset --soft HEAD~1
Mixed reset uncommits and unstages the changes:
git reset HEAD~1
Hard reset wipes out everything—gone, poof 💥:
git reset --hard HEAD~1
Pro tip: Run git reflog
if you accidentally reset something important.
7. git cherry-pick
: Selective Commit Application
Need just one commit from another branch? git cherry-pick
is your secret weapon 🍒. Grab the exact commit you need without merging the whole branch.
git cherry-pick <commit-hash>
This is especially handy when you need a quick bug fix from another branch without bringing in all the extra work.
8. Git Aliases: Work Smarter, Not Harder
Why type out long commands when you can use aliases? Here's how to cut down your keystrokes:
gwip
: Stash all changes with a "Work in Progress" commit:git config --global alias.gwip "!git add -A && git commit -m 'WIP'"
gunwip
: Undo your last WIP commit and restore your working state:git config --global alias.gunwip "reset HEAD~1 --soft"
And the classics:
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.lg "log --oneline --graph --all"
You, when you realized you didn't need to type those long-damn commands a thousand times repeatedly.
Final Thoughts
Git isn’t scary once you get the hang of it, and these advanced commands will help you control your projects like a boss 💪. Whether you’re rewinding time with reflog
, surgically applying commits with cherry-pick
, or streamlining your workflow with aliases, you’ve got the tools to not just survive, but master Git.
Happy coding! 😎
Hope this helps you Git Gud faster—and don’t forget to share your favorite Git memes along the way!