Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The difference between git reset --mixed, --soft and --hard. From http://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard

When you modify a file in your repository, the change is initially unstaged. In order to commit it, you must stage it—that is, add it to the index—using git add. When you make a commit, the changes that are committed are those that have been added to the index.

git reset changes, at minimum, where your current branch is pointing. The difference between --mixed and --soft is whether or not your index is also modified. So, if we're on branch master with this series of commits:

- A - B - C (master)

HEADpoints to C and the index matches C.

--soft

When we run git reset --soft B, master (and thus HEAD) now points to B, but the index still has the changes from C; git status will show them as staged. So if we run git commit at this point, we'll get a new commit with the same changes as C.


--mixed

Okay, so starting from here again:

- A - B - C (master)

Now let's do git reset --mixed B. Once again, master and HEAD point to B, but this time the index is also modified to match B. If we run git commit at this point, nothing will happen since the index matches HEAD. We still have the changes in the working directory, but since they're not in the index, git status shows them as unstaged. To commit them, you would git add and then commit as usual.


--hard

And finally, --hard is the same as --mixed (it changes your HEAD and index), except that --hard also modifies your working directory. If we're at C and run git reset --hard B, then the changes added in C, as well as any uncommitted changes you have, will be removed, and the files in your working copy will match commit B. Since you can permanently lose changes this way, you should always run git status before doing a hard reset to make sure your working directory is clean or that you're okay with losing your uncommitted changes.

@murphywuwu

This comment has been minimized.

Copy link

commented Aug 16, 2016

thanks~,this is very good

@ysinjab

This comment has been minimized.

Copy link

commented Feb 22, 2017

That's awesome ! thank you !

@lgilliam

This comment has been minimized.

Copy link

commented Nov 1, 2017

Best write-up I've seen of the differences and consequences. It's concise and easy to follow.

@itzrahulsoni

This comment has been minimized.

Copy link

commented Jan 17, 2018

I am adding this link because it tells you how to do it using the UI easily.

https://www.attosol.com/undo-in-git-using-sourcetree/

@MJjainam

This comment has been minimized.

Copy link

commented Feb 28, 2018

Hey, nice writeup. I have a doubt though:
Does the working directory change when we do git reset --soft B ?

@shaunthomas999

This comment has been minimized.

Copy link

commented May 31, 2018

@itzrahulsoni - Thanks for adding the reference to the link here.

@renganathan-m

This comment has been minimized.

Copy link

commented Oct 6, 2018

This is great explanation, Thanks!

@vinayak-thbs

This comment has been minimized.

Copy link

commented Nov 7, 2018

Great explaination

@willielassiter

This comment has been minimized.

Copy link

commented Dec 4, 2018

beautifully explained... i can stop looking for a better reset explanation now..

thanks...

@geeklp

This comment has been minimized.

Copy link

commented Sep 6, 2019

awesome

@kartikpodugu

This comment has been minimized.

Copy link

commented Sep 20, 2019

what will the following command do?
git reset --soft HEAD~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.