Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: force a pull to overwrite local changes [duplicate]

Tags:

git

I would like my local branch to be identical to the remote one. When I pull from the remote one, I'm getting conflicts, and in this case I would like not to resolve them and just get the latest version from the remote branch.

To hard pull I use in my local branch:

git reset -- hard

git pull

Nevertheless, when pulling I'm getting the error:

Automatic merge failed; fix conflicts and then commit the result.

Why? How can I pull the remote branch with overwriting? I thought of a workaround to just delete my local branch and create a new one and then pull, but is there a better way?

like image 577
sir-haver Avatar asked May 29 '20 06:05

sir-haver


People also ask

How do I force git overwrite local changes?

Just like git push --force allows overwriting remote branches, git fetch --force (or git pull --force ) allows overwriting local branches.

Does git pull overwrite committed changes?

The reason for error messages like these is rather simple: you have local changes that would be overwritten by the incoming new changes that a "git pull" would bring in. For obvious safety reasons, Git will never simply overwrite your changes.

Will git checkout overwrite local changes?

Checkout old commits Since this has the potential to overwrite local changes, Git forces you to commit or stash any changes in the working directory that will be lost during the checkout operation. Unlike git reset , git checkout doesn't move any branches around.

How do I discard local changes in git and pull?

There are two Git commands a developer must use in order to discard all local changes in Git, remove all uncommited changes and revert their Git working tree back to the state it was in when the last commit took place. The commands to discard all local changes in Git are: git reset –hard. git clean -fxd.


1 Answers

Try doing a git fetch to bring the (local) remote tracking branch up to date with the remote version, then hard reset your local branch to that:

# from local
git fetch origin
git reset --hard origin/local

As to why you are still getting merge conflicts even after a hard reset, this could be explained by a few things. The general explanation would be that your local branch has commits which are not present in the remote version. In that case, Git cannot simply fast-forward your local branch, and must resort to doing a merge instead, which can lead to conflicts.

like image 91
Tim Biegeleisen Avatar answered Nov 15 '22 03:11

Tim Biegeleisen