What is the meaning of git reset --hard origin/master?

GitVersion ControlGit Reset

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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".

Solution 2 - Git

In newer version of git (2.23+) you can use:

git switch -C master origin/master

-C is same as --force-create. Related Reference Docs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjimcghView Question on Stackoverflow
Solution 1 - GitCB BaileyView Answer on Stackoverflow
Solution 2 - Gitthe_spectatorView Answer on Stackoverflow