Ned’s Declassified Git Survival Guide ๐Ÿ“š

Index ๐Ÿ“‹

Introduction ๐Ÿ”

Episode 1: First Day of Work & Bugs ๐Ÿž

Using git checkout to restore files from a previous commit.
Saving uncommitted changes with git restore.

Episode 2: We’ve Got a New Priority, Houston ๐Ÿš€

How to st…


This content originally appeared on DEV Community and was authored by The Eagle ๐Ÿฆ…

Index ๐Ÿ“‹

  1. Introduction ๐Ÿ”

  2. Episode 1: First Day of Work & Bugs ๐Ÿž

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

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

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

    • Recovering deleted commits with git reflog.
  6. Conclusion ๐ŸŽ“

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 temporarily save uncommitted work ๐Ÿ—‚๏ธ, git cherry-pick to pull specific commits from one branch to another ๐Ÿ’, and git reflog to recover seemingly lost commits ๐Ÿ—ƒ๏ธ. Whether you're fixing a bug ๐Ÿž, juggling priorities, 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! ๐ŸŒŸ

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

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

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

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

File stashed

But if you write:

git stash list

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}

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

Then, cherry-pick the specific commit:

git cherry-pick 714cf87

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

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 ๐Ÿ‘‡


This content originally appeared on DEV Community and was authored by The Eagle ๐Ÿฆ…


Print Share Comment Cite Upload Translate Updates
APA

The Eagle ๐Ÿฆ… | Sciencx (2024-09-13T22:20:21+00:00) Ned’s Declassified Git Survival Guide ๐Ÿ“š. Retrieved from https://www.scien.cx/2024/09/13/neds-declassified-git-survival-guide-%f0%9f%93%9a/

MLA
" » Ned’s Declassified Git Survival Guide ๐Ÿ“š." The Eagle ๐Ÿฆ… | Sciencx - Friday September 13, 2024, https://www.scien.cx/2024/09/13/neds-declassified-git-survival-guide-%f0%9f%93%9a/
HARVARD
The Eagle ๐Ÿฆ… | Sciencx Friday September 13, 2024 » Ned’s Declassified Git Survival Guide ๐Ÿ“š., viewed ,<https://www.scien.cx/2024/09/13/neds-declassified-git-survival-guide-%f0%9f%93%9a/>
VANCOUVER
The Eagle ๐Ÿฆ… | Sciencx - » Ned’s Declassified Git Survival Guide ๐Ÿ“š. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/13/neds-declassified-git-survival-guide-%f0%9f%93%9a/
CHICAGO
" » Ned’s Declassified Git Survival Guide ๐Ÿ“š." The Eagle ๐Ÿฆ… | Sciencx - Accessed . https://www.scien.cx/2024/09/13/neds-declassified-git-survival-guide-%f0%9f%93%9a/
IEEE
" » Ned’s Declassified Git Survival Guide ๐Ÿ“š." The Eagle ๐Ÿฆ… | Sciencx [Online]. Available: https://www.scien.cx/2024/09/13/neds-declassified-git-survival-guide-%f0%9f%93%9a/. [Accessed: ]
rf:citation
» Ned’s Declassified Git Survival Guide ๐Ÿ“š | The Eagle ๐Ÿฆ… | Sciencx | https://www.scien.cx/2024/09/13/neds-declassified-git-survival-guide-%f0%9f%93%9a/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.