Reputation: 5957
I did a git pull
and got an error:
The following working tree files would be overwritten by merge... Please move or remove them before you can merge.
To resolve this I did the following:
git fetch
git reset --hard origin/master
Now when I do git pull
, it says everything up to date. I want to know what exactly happens when I run these commands. I know git fetch
fetches the changes from the remote repo without merging them into my local repo.
What is the meaning of git reset --hard origin/master
? How does it work?
Upvotes: 399
Views: 688267
Reputation: 1603
In newer version of git (2.23+) you can use:
git switch --force-create master origin/master
Related Reference Docs
Upvotes: 12
Reputation: 39
As answered above, you use this when you want to discard your changes and forcefully move the HEAD back to origin/master
.
If you did reset hard and discard your changes by mistake, you can always do git reflog
to view a local repo history list of every time the HEAD changed and checkout the desired commit sha.
You can:
git checkout <commit_sha>
to checkout a new branch pointing to the desired commit.git reset HEAD --hard <commit_sha>
to move your HEAD back to the desired commit.Upvotes: 3
Reputation: 9512
This is just for the exception of resetting to the master when you already are in the master and worked on that but want to get back to the origin/master without any traces of your work.
Imagine you have not checked out a new branch since you did not want to decide on its name. Then you need to remove the repository folder and clone again, like in good old newcomer times.
Why is that?
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. To get them removed, you would have to remove the full repo folder and clone again if you do not want to remove them by hand. These empty folders stay even after the "reset hard", and this is not dependent on the .gitignore file.
Upvotes: 1
Reputation: 791421
git reset --hard origin/master
says: throw away all my staged and unstaged changes, forget everything on my current local branch and make it exactly the same as origin/master
.
You probably wanted to ask this before you ran the command. The destructive nature is hinted at by using the same words as in "hard reset".
Upvotes: 764