Introduction
Welcome to Ned's Declassified Git Survival Guide! ๐ In this guide, you'll learn essential Git commands like git checkout
and git restore
to undo changes, git stash
to save uncommitted work temporarily, git cherry-pick
๐ to pull specific commits from one branch to another, and git reflog
to recover "lost commits". Whether you're fixing a bug ๐, changing priorities because your manager decided that, or restoring deleted code, these commands will help you handle common real-case scenarios with confidence ๐ช. So, letโs dive in and master Git together!
This is what we'll learn today:
Index ๐
-
Episode 1: First Day of Work & Bugs
- Using
git checkout
to restore files from a previous commit. - Saving uncommitted changes with
git restore
.
- Using
-
Episode 2: We've Got a New Priority, Houston
- How to stash untracked files with
git stash
. - Managing stashes and retrieving them when needed.
- How to stash untracked files with
-
Episode 3: The CoolFeature Crisis!
- Using
git cherry-pick
to select and apply specific commits.
- Using
-
Episode 4: I Changed My Mind Drama
- Recovering deleted commits with
git reflog
.
- Recovering deleted commits with
Conclusion
Episode 1: First Day of Work & Bugs
Let's say we're building a Map Application for your workplace, and we have the following folder structure:
We refactor our main.tsx
file because we want MapsApp.tsx
to be our main application file, but we accidentally introduce a bug.
We don't realize it and proceed to commit and push it to the remote repository:
(In case you're curious, I'm using gitmoji for cool commit messages.)
Now, you run your project and realize there's a bug in main.tsx
because the console tells you, but for some reason, you can't find itโmaybe you're nervous, so...
What can I do so I don't get yelled at?
Advice #1: Restoring Files Saves Lives
In this scenario, for simplicity, let's say you've already pushed the project before the refactor as a good practice (otherwise, you're in trouble). You use git log
, and it shows you the following:
Since you have a previous good file, you can simply use git log
and select the commit hash that you want to restore. In our case, it's this one:
Then, proceed to write:
git checkout 4d7490e src/main.tsx
And you've restored itโtadaa!
Hey, I've only got one commit in my project, can I reverse that?
Advice #2: Use git restore
for Uncommitted Changes
You have the following uncommitted changes in main.tsx
:
You can confirm they are uncommitted with a git status
:
And your git log
only has the first commit, where you created your project:
You have two options, one easy and one hard:
# Easy one
git restore src/main.tsx
# Hard one
git checkout -- src/main.tsx
I'll go with restore
:
Now our file is clean again! Hooray!
Episode 2: Hey, We've Got a New Priority, Houston
Now, your manager or team leader decides it's a good day to change priorities (because thatโs their thing). Let's say you're creating a new page called MyCoolPage.tsx
:
Now, you have to decide: Should I commit this and then rebase the commits, pick the ones I want, and squash them to make it clean?
I'm Going to Spend Too Much Time Just for That Change Later On, What Can I Do?
Advice #3: Stash It, Bro
Be careful in this case! This special example involves creating a new file that doesn't exist in your repository, so you need to use:
git stash --include-untracked
# Or the shorter flag
git stash -u
Now, you can see the file is no longer with us:
But if you write:
git stash list
It's still here:
The name of the stash is stash@{0}
.
Now, we've got multiple options:
# We can pop the stash and remove it from the list with a simple
git stash pop
# Or apply it and let it stay in the list so we can access it later with
git stash apply stash@{number of your stash}
I'll go with pop
, and you can see it's back:
When I try to use git stash list
, nothing shows up because it ultimatewas popped from the list:
Episode 3: The CoolFeature Crisis!
You're working in a different branch for a feature called feature/myCoolFeature
, which has three requirements. You've already completed two of them, so you have two commits in that branch.
Now, your manager tells you: Hey, we need the CoolFeature
you implemented from your branch, but only the first one, and we need it now!
You could create a branch from your current one, delete the HEAD commit, and merge it into main
, but thatโs too much for such a simple task.
Oh man, I need time for this and they need it now, is there a faster way?
Advice #4: Use Cherry-Pick to Get That Commit
GitSurvivalGuide
Go to your main
branch with git checkout main
and get the log from the other branch with:
git log feature/myCoolFeature
Then, cherry-pick the specific commit:
git cherry-pick 714cf87
And Linus Torvalds (creator of Git & Linux) saves the day again! And if you use git log
, you can see your changes are there:
Episode 4: The โI Changed My Mindโ Drama
Let's say you created a new feature:
You add and commit it, but you change your mind and decide to delete it, so you proceed to Google and search how to delete a commit
. You find that with a git reset --hard HEAD~1
, you proceed to use it.
You're fine until you realize that you want to reuse some code, but now when you use git log
, you can't find it. You think:
It's gone forever, now I'm going to waste time...
Advice #5: If You Can't Find It in git log
, It's Time to reflog
Just check the magic of this command:
git reflog
Now you've got the hash to restore your commit:
Another happy ending!
Conclusion ๐
Congrats dawg, you've just aced your Git survival guide! ๐ Just like Nedโs Declassified School Survival Guide ๐, you now have a nice toolkit to navigate the tricky hallways of version control. From using git checkout
to fix those accidental missteps, stashing away uncommitted changes like a pro, cherry-picking commits when the pressure's on, to rescuing lost work with git reflog
, you're set to tackle any Git challenge with confidence. ๐๐ ๏ธ So, put these tips into practice and keep your projects running smoothly.
Special thanks to Midu without his Git book, I wouldn't have made this article. ๐๐ปโจ
Thank you for reading!
Let me know, dawg, your thoughts or any suggestions about Git in the comments down below ๐
Top comments (0)