• 12 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 8/21/19

Turn back time on your mistakes with git reset

Log in or subscribe for free to enjoy all this course has to offer!

Backtracking: the three types of git reset

Git reset is a powerful tool for backtracking and fixing mistakes; but, with great power comes great responsibility. Before we get into how to use different git reset commands, I want to caution you that this tool can cause a lot of damage. Remember, your code repository holds the main branch of a project. Running git reset will typically delete files and commits.  And without those, you don’t have a project! This is why it's critical to plan ahead when using it, so you don't end up deleting elements that are critical for your project.  

It can get really bad
Git reset can save your life - or become a nightmare

For example, sometimes running some commands can leave your code with orphaned files left for later; in other words, files that supported something that no longer exists.  You might think they're useless, but you never know if another team member might be utilizing them.  Whenever you use reset, communicate with your teammates, and make sure that you are not deleting anything you are not meant to. 

With that warning out of the way, we can get into the three types of git reset: hard, soft, and mixed. Each of these types is used in specific circumstances.  

Let’s take a look at what you can do.

Forgot to add a file to a commit? Git reset soft to the rescue!

If you forgot to add a file to commit, you can undo and reset your files back to the pre-commit phase.:

git reset --soft HEAD~1

This command moves back to the last commit, and your files are still staged. In other words, your original files will go back to being staged.  All you have to do is add the file you need, and then commit again to fix the issue.

This is what it looks like:

git reset soft in action
Git reset soft in action

In the above example, we originally added forest and flowers, but had forgotten birds.  So, we moved back to staged with  git reset soft, added birds, and made a new commit. Problem solved. 🙌

Rewind on committing your files: git reset mixed is your go-to!

Sometimes you might say to yourself, "Gee whiz! I don't need those files until the next commit! How do I rewind?" The mixed version of git reset is perfect for this situation. You don't have to type in the full  git reset --mixed  because it is actually a default setting for git reset.  If you type in the following, it does the same thing:  

git reset

So when would you use it? Let's say you made an accidental commit on your local repository, and just want to go back and get rid of a few files that were in your commit.   Git reset  will unstage the files that were in your commit. This way, you can just add the files you need for your next commit!

Let's see how that looks from the commit in the last example:

I unstaged the three files in the last commit, staged one file and commit it.
Git reset mixed in action

Here, we've unstaged the three files in the last commit, staged one file ("birds"), and then committed it. Pretty slick, right? 😎

Totally sure you're starting over? Use git reset hard!

There might come a time when realize that you screwed everything up and want to start over. 😞 You seriously don't need any of the files. If you are sure, then this is the command: 

git reset --hard

Use git reset --hard with caution. Let me show you what happens: 

Yikes, I just lot a bunch of uncommitted files!
Git reset hard makes its mark

Yikes, I just lost a bunch of uncommitted files: birds, flowers, forest, and mysong have all been wiped out. You can do this, but like I said, use caution. Let me give you a list of warnings of what could happen:

  • ⚠️ You are sharing your branch with someone, and you delete their unstaged files and orphaned branches. 😬

  • ⚠️ You delete your own unstaged files. 😭

  • ⚠️ You kill the main branch, and now everyone has to start over. 😱

  • ⚠️ Wow, that looks clean...its because its all gone. ☠️

So I should just never use it?  

You can, but it's a bit dangerous. Here are some quick guidelines for figuring out if it's worth the risk:

✅ DO use git reset --hard 

  • You have only one branch on your project that you just started, and you are tracking your files pretty consistently.

  • You literally want to start over and get rid of everything.

❌ DON'T use git reset --hard

  • You are working on a project with a team.

  • You share a branch with multiple team members.

Have I scared you enough?  Just keep in mind that there are lots of ways to backtrack and fix your mistakes.  Think hard about which solutions are the most appropriate for your situation.

Let's recap!

  • git reset --soft  can uncommit and keep your files staged.

  • git reset --mixed  can uncommit and unstage your files.

  • git reset --hard  can delete all of your unstaged and staged files!

  • You can backtrack by a specific number of commits.

Example of certificate of achievement
Example of certificate of achievement