Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 940
vashishatashu Avatar asked Apr 25 '15 07:04

vashishatashu


People also ask

What is git reset -- hard origin master?

git reset --hard origin/master works only as a full wipe out if you are in a local branch. If you are in the master branch instead, and if you have made changes, you can only drop all of the files that you made or changed. You cannot drop the folders that you added.

What is the difference between master and origin master in git?

Master: This is a branch name where we first initiate git and then we use to make commits. And the changes in the master can pull/push into a remote. origin/master: This is a remote branch, which has a local branch named master on a remote named origin.

What is the difference between git reset and git reset -- hard?

git reset --soft , which will keep your files, and stage all changes back automatically. git reset --hard , which will completely destroy any changes and remove them from the local directory. Only use this if you know what you're doing.

What is git reset types of reset?

reset --soft : History changed, HEAD changed, Working directory is not changed. reset --mixed : History changed, HEAD changed, Working directory changed with unstaged data. reset --hard : History changed, HEAD changed, Working directory is changed with lost data. It is always safe to go with Git --soft.


1 Answers

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

like image 196
Mohammad AbuShady Avatar answered Oct 05 '22 14:10

Mohammad AbuShady