Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve merge conflicts: Force overwrite all files

I am working on a git repository by myself (so yes, I know the implications and the warnings of doing this) and somehow one of the trees got a commit after being pushed when it shouldn't have.

Now I'm trying to pull back and it's complaining about hundreds of merge conflicts.

Is there a way to tell git to forcefully overwrite any and all files locally that are coming from the remote server? Is there a faster way than doing git reset --hard HEAD~1 and then doing the pull?

On that same note, is there a way to do the same with with a simple merge? Everything I've seen suggests to check out each and every file during the merge conflict resolution stage, but with hundreds of files it's just not possible to do so manually.

like image 820
Qix - MONICA WAS MISTREATED Avatar asked Jan 10 '13 03:01

Qix - MONICA WAS MISTREATED


People also ask

How do I force git overwrite?

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

Will git merge overwrite my changes?

Usually git does not overwrite anything during merge.


2 Answers

There are three simple solutions to copy the last version that is in you remote repository, discarding all changes that you have made locally:

  1. Discard your repository and clone again. This is the most simple solution, but if your repository is big, it can take a long time, and may require extra effort like reconfigureing, etc.

  2. Discard the local changes with git reset --hard <hash> and then do a git pull. The problem is you need to first find a commit that precedes whatever change history you are trying to avoid. After resetting to that commit hash, do a git pull.

  3. Do a git fetch to bring the updates to your local reference of the remote branch (usually origin/master) and then do a git reset --hard passing this reference, ie, git reset --hard origin/master.

like image 172
William Seiti Mizuta Avatar answered Oct 01 '22 15:10

William Seiti Mizuta


git reset --hard {remote_repository_name}/{branch_name}

Example:

git reset --hard upstream/branch1

If you are working with a branch you can use the above code.But before that, you have to set (upstream or origin) to your local repository,

git remote add upstream https://github.com/lakini/example.git

here,https://github.com/lakini/example.git is the remote upstream repository.

Same as like this we can work in the remote repository(origin) as well.

git reset --hard origin/branch1
like image 41
Sithara Avatar answered Oct 01 '22 15:10

Sithara