What's the difference between `git reset --hard master` and `git reset --hard origin/master`?

vashishatashu picture vashishatashu · Apr 25, 2015 · Viewed 18.1k times · Source

I tried a lot of links on Stackoverflow/elsewhere to properly understand behaviour of

git reset --hard option

I know that:

  • If it is omitted or if it is origin, reset is done on most recent commit on origin
  • If a SHA1 hash is provided, reset is done on the corresponding commit.

What I don't understand are the following values:

  1. origin
  2. HEAD
  3. origin/master
  4. origin/branch

All seem to have same behavior i.e. they a point to latest commit on master.

Please explain what is the significance of all 4 value option provided above.

I would also like to know if I am on a specific branch how can I reset to the last commit on that very branch? If for example I am on v1.2, origin/v1.2 still takes me to latest commit on master.

Answer

Mohammad AbuShady picture Mohammad AbuShady · Apr 25, 2015

First of all you need to understand that branch and tag names are just pointers to hash values, which represent a single commit, if you say that there's 4 options that have the same behaviour, then the first logical answer is because they all point to the same commit

  • origin I'm not sure about this but i think origin by it self will resolve to origin/HEAD which i think will be dependent on your github settings, in github you set a 'default branch', origin/head will resolve to origin/[default_branch], in your case im assuming it's master, so this is why it has the same effect as origin/master.

  • HEAD always points to the current commit, the one you are standing on, so a git reset --hard HEAD will permenantly remove all changes in tracked files and staged files changes, but not change the commit hash.

  • origin/master is last commit in the remote master branch since your last fetch/pull, each time you commit to master, your local master is updated and your origin/master is updated too, if someone else pushes to master your repo will have no idea that there is an update, unless you do a git fetch then your origin/master will move ahead of your master, or maybe even diverge.
    running a git reset --hard origin/master will have the same effect if you are currently are on the master branch and the master is in sync with origin/master

  • origin/branch I'm not sure what this represents, because there's no origin/branch by default, so I'm guessing you created a branch called branch and happens to be in the same commit as your master, to confirm you can try doing a git branch to see all your branches, i'm guessing you'll find one called branch

To see all this in a visual way, you could try running git log --graph --decorate --all or I prefer a visual tool like gitk, if you have the binary installed you would run gitk --all to see all branches relative to each other