How And When To Use Git Reset

Most of us avoid dreaded git reset command — but actually it can be really useful, as long as you know how it works!

Charlotte Bone
Nov 26, 2019 · 4 min read

What does git reset do?

To put it simply, git reset will take your branch back to a certain point in the commit history, but there are 3 different levels to this:

  • mixed: Moves the head and index back to the commit specified but leaves the files in the working directory — all of the files after the commit are in your working file as untracked folder. If you add them all now, you’ll be at the same stage as a soft reset above.
  • hard: Moves the head, index and working directory back to the commit specified — all of the updated files after the commit specified are now GONE!!! (uncommitted files are unrecoverable at this point)

Why would you need this?

Here are a few examples so that you will have a better idea of when to use each. You should only ever do this on your own branch, or when you’re sure no one has already pulled any of the commits that you will be removing.

Soft

Let’s say you have done a few tiny commits and you want them to all be put into 1 more descriptive commit.

git reset — soft A
git commit -m “my new merged commit”
git push origin branch --force-with-lease

Mixed

You’ve just pushed a few commits, but you want to go back and remove a couple of files in a previous commit.

git reset --mixed A
git status
git add <files>
git commit -m "updated commit"
git push origin branch --force-with-lease

Hard

When would you possibly want this? Usually when things have gone wrong because it’s VERY RISKY. I’ll talk you through a scenario where I’ve needed it.

git reset --hard A
git reset --hard A
git push origin branchname --force-with-lease

Hopefully now you will see that git reset is very powerful and can help you in some situations, used with care!

Charlotte’s Digital Web

Tutorials and insights from a full stack web developer.

Charlotte Bone

Written by

I am a creative, passionate, full stack developer. I love technology & I really want more females to not be afraid to pursue this career / Engineer @stacker.app

Charlotte’s Digital Web

Tutorials and insights from a full stack web developer.

Charlotte Bone

Written by

I am a creative, passionate, full stack developer. I love technology & I really want more females to not be afraid to pursue this career / Engineer @stacker.app

Charlotte’s Digital Web

Tutorials and insights from a full stack web developer.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store