DEV Community

Cover image for Ned's Declassified Git Survival Guide πŸ“š
The Eagle πŸ¦…
The Eagle πŸ¦…

Posted on

Ned's Declassified Git Survival Guide πŸ“š

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 πŸ“‹

  1. Episode 1: First Day of Work & Bugs

    • Using git checkout to restore files from a previous commit.
    • Saving uncommitted changes with git restore.
  2. Episode 2: We've Got a New Priority, Houston

    • How to stash untracked files with git stash.
    • Managing stashes and retrieving them when needed.
  3. Episode 3: The CoolFeature Crisis!

    • Using git cherry-pick to select and apply specific commits.
  4. Episode 4: I Changed My Mind Drama

    • Recovering deleted commits with git reflog.
  5. 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:

Folder structure of React in Vite

We refactor our main.tsx file because we want MapsApp.tsx to be our main application file, but we accidentally introduce a bug.

Commit with a bug in main.tsx

We don't realize it and proceed to commit and push it to the remote repository:

Doing the commit process

(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:

Git log

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:

Selecting the commit hash

Then, proceed to write:

git checkout 4d7490e src/main.tsx
Enter fullscreen mode Exit fullscreen mode

And you've restored itβ€”tadaa!

Restored file

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:

Uncommitted changes

You can confirm they are uncommitted with a git status:

Git status

And your git log only has the first commit, where you created your project:

Git log

You have two options, one easy and one hard:

# Easy one
git restore src/main.tsx
# Hard one
git checkout -- src/main.tsx
Enter fullscreen mode Exit fullscreen mode

I'll go with restore:

Clean file

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:

Creating 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
Enter fullscreen mode Exit fullscreen mode

Now, you can see the file is no longer with us:

File stashed

But if you write:

git stash list
Enter fullscreen mode Exit fullscreen mode

It's still here:

Stash list

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}
Enter fullscreen mode Exit fullscreen mode

I'll go with pop, and you can see it's back:

File popped from stash

When I try to use git stash list, nothing shows up because it ultimatewas popped from the list:

Empty stash 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!

Urgent request

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
Enter fullscreen mode Exit fullscreen mode

Then, cherry-pick the specific commit:

git cherry-pick 714cf87
Enter fullscreen mode Exit fullscreen mode

Cherry-picking the commit

And Linus Torvalds (creator of Git & Linux) saves the day again! And if you use git log, you can see your changes are there:

Git log of main


Episode 4: The β€œI Changed My Mind” Drama

Let's say you created a new feature:

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.

Git reset

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
Enter fullscreen mode Exit fullscreen mode

Reflog

Now you've got the hash to restore your commit:

Reset

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)