
No Comments Yet
Be the first to share your thoughts and start the conversation.
GSAP Course launch discount ends soon!🎉
Claim DiscountBe the first to share your thoughts and start the conversation.
How did you manage to remove the blur property and reach here?
Upgrading gives you access to quizzes so you can test your knowledge, track progress, and improve your skills.
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
In this lesson, you'll discover essential Git commands that serve as lifesavers when things go wrong during development. Understanding how to effectively use these commands not only helps in managing your code history but also in recovering from mistakes and keeping your workflow seamless.
git checkout
: This command allows you to view history but does not delete anything from your commits.git reset
: This command helps to reset the commit history, allowing you to remove specific commits while deciding what to do with the associated changes.git revert
: Ideal for situations where you want to undo changes while keeping the commit history intact.git stash
: Temporarily saves uncommitted changes, allowing you to switch to another task without losing your work.00:00:00Â So far, you've learned the basics and some intermediate Git skills that you'll use in your day-to-day work.
00:00:06Â But let's be honest, sometimes things can go wrong and you mistakenly break production.
00:00:13Â That's when Git becomes your best friend, only if you know how to use it to its full potential, of course.
00:00:19Â And this is where what I call Git Savior commands come in handy.
00:00:24Â These commands let you manage changes, undo mistakes, and tweak your commit history.
00:00:30Â Think of them as your backup plan when things get messy.
00:00:33Â You already tried and tested one such command, which allows you to check out to a past commit.
00:00:39Â Do you know which one is it?
00:00:41Â Yeah, it's git checkout and then a commit hash.
00:00:45Â But as I've told you, that command doesn't discard or delete anything.
00:00:50Â It's just a way to view the history of specific commits.
00:00:53Â There might be the case where you want to check out to a particular commit and then delete everything that comes after it.
00:01:00Â That's the job for git reset.
00:01:03Â Imagine you want to remove some commits and revert to a previous commit with the possibility of choosing whether you want to keep or discard the changes
00:01:12Â in the working directory.
00:01:15Â That's a bit hard to understand, right?
00:01:17Â So let me say that again.
00:01:19Â Suppose you made 10 commits and want to check out the third commit in history.
00:01:23Â You want to delete all commits after the third, like fourth, fifth, sixth, tenth, and so on.
00:01:30Â Maybe you even wrote some bad code in those commits.
00:01:33Â And to keep things simple, you want to remove these bad commits, but still want to keep the changes that you implemented.
00:01:40Â I hope that makes sense.
00:01:41Â So, delete these commits, but keep all the changes you made from the fourth to the tenth commit.
00:01:48Â This is where we use git reset.
00:01:50Â It allows us to remove the traces of commits in history, but gives us the changes we made in those to be discarded commits so we can decide what to do.
00:02:00Â We can keep those changes, form a better commit out of them, or delete them entirely.
00:02:05Â It's up to you.
00:02:06Â Let me quickly add a console log to this hello.js file.
00:02:10Â On top of the first one, I'll add another one that says something like console log hello GitHub.
00:02:17Â There we go.
00:02:18Â Now we have two.
00:02:19Â Then make a commit by running git add dot git commit dash M add GitHub console log.
00:02:29Â to hello.js.
00:02:31Â Next, to properly illustrate how we'll use git reset, I'll add one more console log.
00:02:37Â This console log will say something like hello from, and then put the name of your branch.
00:02:44Â Dev Adrian in my case, since we haven't switched from before, we're still in this branch.
00:02:49Â And I also want to commit this.
00:02:51Â Imagine this is a bigger feature that you implemented.
00:02:54Â So I'll run git add dot git commit dash M and we'll say something like add hello console dot log to hello dot JS.
00:03:07Â And commit it.
00:03:09Â Now you commit it, you stand up from your desk, go for a walk or go about your day.
00:03:13Â Then you come back and then you start focusing on some additional functionalities within the same branch.
00:03:19Â Let's say that additional functionality is another console log, but this time this console log will actually break things.
00:03:26Â So we can say this is bad code.
00:03:30Â Okay.
00:03:30Â And now we can do another commit, of course, without knowing that this commit breaks the code.
00:03:35Â So we can say git add dot git commit-m, and we can say add another console log.
00:03:44Â Now, if you run git log.
00:03:47Â You'll see many commits.
00:03:48Â Keep in mind that git log here includes all commits that have happened before as well.
00:03:53Â Everything from the latest console logs, to previous ones, to resolving our merge conflicts, even Johnny's changes right here.
00:04:00Â There's a lot of stuff that's here.
00:04:01Â Everything from when we first started working on this project.
00:04:05Â Now, before I tell you how to remove this bad code using git reset, you must understand that there are a couple of different ways to run the git reset command.
00:04:14Â Soft reset simply moves the specified commit in history, but keeps changes staged in a working directory.
00:04:21Â That means that whatever changes were made after that commit will be in stage mode.
00:04:26Â And stage changes are those that we add to git tracking system by running the git add dot command.
00:04:34Â So before running that, they're untracked.
00:04:36Â And then once you run git add, then you can consider those changes tracked or staged.
00:04:42Â So if you want to do that, you can run git reset dash dash soft, and then add a commit hash.
00:04:49Â The second way to use git reset is called a mixed reset.
00:04:53Â And for that one, you don't have to pass any flag as it's the default one.
00:04:59Â MixedReset moves to the specified commit in history, unstages the changes, and keeps them in the working directory.
00:05:06Â Thus, all changes made after the specified commit will be in your working directory, but they won't be staged.
00:05:13Â You have to manually stage them if you want to by using git add dot.
00:05:17Â And finally, there's a hard reset.
00:05:19Â It moves to the specified commit in history and discards all changes in the working directory and staging area.
00:05:27Â All those changes made after the selected commit will be discarded entirely and you won't see any trace.
00:05:34Â You can run that command by running git reset, dash dash hard, and then add a commit hash.
00:05:41Â Now let's actually go with a mixed reset by copying one of the commit hashes from here.
00:05:46Â But which one do we want to copy?
00:05:48Â We can scroll to the top to see the newest ones.
00:05:51Â And we have this add another console log, which we know breaks our code.
00:05:55Â So we want to avoid this.
00:05:57Â So instead we can go all the way to add GitHub console log to HelloJS.
00:06:02Â So let me copy this commit hash and press Q to exit.
00:06:08Â Git log.
00:06:10Â Next, we can run git reset and then paste this commit hash and press enter.
00:06:17Â Unstaged changes after reset, hello.js.
00:06:20Â So take a quick look at the file explorer.
00:06:23Â You'll see that the hello.js file is blue, which means that we have some changes in that file.
00:06:29Â And if you pay attention to this green line here, this means that these are the two additional changes which are currently unstaged.
00:06:36Â So let me put that in other words.
00:06:38Â The changes for the two commits that we have added are right now here and are unstaged.
00:06:44Â And we can verify that by running git log.
00:06:47Â And you can see that there are no additional commits after the add GitHub console log.
00:06:52Â There's no add hello dev Adrian, and there's no add bad code.
00:06:56Â We can exit that for a second and we can also run git status.
00:07:00Â And you'll see that hello.js has been modified and specifically it has been modified with the file changes that came from the two commits that we have reset.
00:07:11Â So now it's completely up to you whether you want to keep those changes, modify them, and then again, stage and commit them.
00:07:18Â In this case, I will simply remove them from the code like they never happened.
00:07:24Â And we can go to git log and see that there's no more add buggy code or add console log and see that those two additional commits happening after the GitHub
00:07:34Â console log are completely gone.
00:07:35Â All of your mistakes completely deleted.
00:07:38Â I hope this makes sense.
00:07:39Â So you can take a moment and try the other two variations of the git reset on your own and see how they differ.
00:07:46Â Once you do that, we can move to the next advanced git command.
00:07:50Â Git revert.
00:07:51Â Let's say you've deployed a feature that broke production and you want to undo its effects without losing the commit history.
00:07:58Â You want the logs to be there, but you want to revert to an old commit.
00:08:03Â This is the exact situation where you want to use git revert.
00:08:07Â It's ideal when you have nothing to hide and you want to maintain a clear record of changes that you did.
00:08:13Â It's almost the opposite of reset.
00:08:15Â Let me show you how it works.
00:08:17Â I'll add another console log that says something like trying out revert.
00:08:23Â Okay.
00:08:24Â And I will commit it by running git commit-m add revert console log.
00:08:31Â Oops, I forgot to do a git add before that.
00:08:33Â My mistake.
00:08:35Â Let me do git add dot, and I'll use the up arrow two times to get back to the previous command and commit it.
00:08:42Â Now, let's say you want to revert to the previous version of your code base that didn't contain this console log, but you want to keep it in the logs.
00:08:51Â You can run git log.
00:08:53Â You can find the commit hash of the previous commit without containing the revert console log.
00:08:58Â Copy it.
00:09:00Â and then run git revert, paste the commit hash, and then we get something that looks like this, which is somewhat familiar to us.
00:09:09Â It's like a mini merge conflict.
00:09:12Â It's trying to figure out what we want to keep and what we want to remove.
00:09:16Â In this case, I'll manually remove lines of code that we don't need.
00:09:20Â So we don't need these ones and we don't need the trying out revert because we want to abort or revert that one.
00:09:28Â You can save it.
00:09:30Â And once you save it, we want to add those changes to staging by saying git add dot.
00:09:36Â And then you can run git revert continue, and this command will successfully finalize the revert.
00:09:43Â As you can see, we're getting there.
00:09:44Â This message is saying that we will revert this commit, but we just need to provide it a commit message.
00:09:50Â Or we can exit this window by pressing colon, QA, and then exclamation mark, and then press enter.
00:09:57Â There we go.
00:09:58Â So one file has changed, there's one insertion and two deletions, and we are back to where we were.
00:10:03Â Similar to git reset, right?
00:10:05Â But now, if you check out git log, You can see that a new commit revert add GitHub console log to hello has been added to our Git history.
00:10:17Â As I said, similar to reset, but depends whether you want to hide your tracks or you want to show everybody that you messed up and you fixed it later on.
00:10:26Â Both have their own use cases.
00:10:27Â Now, let me show you git stash, another super useful git command.
00:10:32Â Let's say you're in the middle of developing a feature, but an urgent bug caused by your teammate comes up and your boss wants you to work on it first.
00:10:40Â You haven't finished your feature and it's not ready to commit yet.
00:10:45Â But you still have to keep your active changes somewhere so you can get back to them later on and you can start working on this bug soon.
00:10:52Â And that's exactly what git stash does.
00:10:54Â It lets you temporarily save your uncommitted changes, both staged and unstaged, without actually committing them.
00:11:02Â Let's say that I am in the process of implementing an important feature.
00:11:09Â And then I have multiple lines of code, which I don't want to lose.
00:11:14Â That's going to look something like this.
00:11:17Â You can also write some of your own very important code here.
00:11:20Â So now how do we save this code so we can use it later on so we can focus on something else for the time being?
00:11:26Â It's super useful.
00:11:28Â You just run git stash.
00:11:31Â As you can see, your important code is now completely gone.
00:11:35Â And we got a message here saying that save the working directory on devadrian.
00:11:40Â So now you can go ahead and implement this urgent fix that your boss requested you to do.
00:11:47Â You can then run git add, git commit, and say save the day.
00:11:53Â Of course, this is not a realistic commit message, but in this case, since you implemented the urgent fix, you indeed did save the day.
00:12:00Â And you can even push that code if you want to.
00:12:02Â So it can run git push.
00:12:04Â Great.
00:12:05Â The day is saved, but now you want to get back to implementing that other feature you were working on before your day was ruined by a bug.
00:12:12Â To get our code back, simply run git stash, apply, and then enter a stash name.
00:12:19Â But how do we get to a stash name?
00:12:22Â You can run a command git stash list to see a list of all stashes.
00:12:27Â Currently, we have stash at curly braces 0. So let's do just that.
00:12:33Â Get stash, apply stash at curly braces 0. And press Enter.
00:12:41Â And you can see that our code got back right here.
00:12:44Â Since the implemented fix was on similar lines as our existing code, we have yet another merge conflict.
00:12:50Â But by now, you should be an expert in resolving those.
00:12:53Â So simply remove the lines around it and keep both the urgent fix and your work in progress.
00:13:01Â And that's it.
00:13:01Â The day is saved and you can continue working on your feature.
00:13:05Â I don't want this to happen, but I'm sure that someday soon you'll encounter an issue in your code that requires you to use one of these three very handy
00:13:15Â Git commands.
00:13:16Â Git stash will definitely be the first one.
00:13:18Â So when the time comes, you'll know how to use it.
00:13:21Â Great work.