I tried a lot of links on Stackoverflow/elsewhere to properly understand behaviour of
git reset --hard option
I know that:
origin
, reset is done on most recent commit on origin
What I don't understand are the following values:
origin
HEAD
origin/master
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
.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With