Question

I want to revert all the uncommited changes in current branch say 'master'. Whats the best one to use amoung the following and WHY??:

  1. git checkout -f master
  2. git reset --hard <last commitID>
Was it helpful?

Solution

They both do what you need : discard all the uncommitted changes, but checkout is designed to switch to a new branch discarding the uncommitted changes on the current branch, so for example :

git branch 
*master
#Do some changes 
git checkout -f new_feature #It switch to the other branch(new_feature), discarding all the current branch changes, in one single command.

With git reset --hard you achieve the same goal, but in the case you want also to switch branch you have to do one more command:

git branch 
*master
#Do some changes 
git reset --hard
git checkout new_feature

I think that in the case you remain in the same branch the two command are equivalent ...

OTHER TIPS

git checkout -f master

What it does is very simple:

If you will just do checkout your local changes will not be lost.

If you want to override your changes you adding the -f flag to make sure that all your files are back to their "original" state. It also allow you to switch between branches.

If you are working on your current branch you can simply use reset and save the need to switch branches.

git reset --hard <last commitID>

reset have several options: soft, mixed & hard. each does different things. more info:

https://www.kernel.org/pub/software/scm/git/docs/git-reset.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top