Add -X ours argument to your git merge command.
Say you are working in your local branch. Then you want to merge in what went in the master:
git merge -X ours master
On the other hand if you are in master and want to merge your local branch into master then @elhadi rightly says you should use theirs:
git merge -X theirs somebranch
Answer from kmkaplan on Stack OverflowAdd -X ours argument to your git merge command.
Say you are working in your local branch. Then you want to merge in what went in the master:
git merge -X ours master
On the other hand if you are in master and want to merge your local branch into master then @elhadi rightly says you should use theirs:
git merge -X theirs somebranch
To overwrite your stuff in your branch and take their work, you should make
git merge -X theirs {remote/branch} --> example:origin/master
git - Resolve merge conflicts: Force overwrite all files - Stack Overflow
Git merge from UiPath fully overwriting files
version control - How do I force "git pull" to overwrite local files? - Stack Overflow
Aargh! Every Git file update generates a merge conflict
While attempting to merge two branches, I am expecting to see conflicts on trying to edit an existing line. The merge statement simply overwrites the target branch with details from the source branch.
Can anyone help me to set up the repo in a way that merging branches will generate conflicts when trying to edit existing lines of code. I am also open to other ways of generating conflicts while merging or pulling instead of simply rewriting which is currently the case.
Not sure if this is the "blessed" way to do it, but here's what I did to resolve the same problem without having to "force push" or anything gross like that.
Let's assume your history looks something like this (and M is the flubbed merge):
-A--B--C--M (master points here)
\ /
D----E
Running git checkout -b merge_fix <commit ID E> creates a branch before we made any mistakes:
-A--B--C--M (master points here)
\ /
D----E (HEAD and merge_fix point here)
Now, let's re-do the merge on our new branch. We can't just merge in master, so we need to manually pick the commit before our bad merge: git merge <commit ID C> Don't make the same mistakes you did last time!
-A--B--C--M (master points here)
\ X
D----E-G (HEAD and merge_fix point here)
Assuming that commit G looks good, now we want to sync up with the top of the master branch. This command tells git to ignore the changes that were made to master, and force our changes to become the merge result: git merge -s ours master
-A--B--C--M (master points here)
\ X \
D----E-G--H (HEAD and merge_fix point here)
Finally, (again assuming that commit H looks good, we want to fast-forward master to include our fixed merge:
git checkout master
git merge merge_fix
This really just moves the master branch pointer to H, but I'll take the opportunity to clean up my ASCII art a bit:
-A--B--C--M--H (HEAD, master, and merge_fix all point here)
\ X /
D----E--G
And there you have it! you've successfully re-done the merge without invalidating any history!
You can do it, like this:
- Reset to the commit before your merge.
- Perform the merge again
- Force push
That is:
git reset --hard SHA1
git merge branchname
git commit
git push --force remotename branchname
Just keep in mind that git push --force will rewrite whatever you had at the remote branch, and other people using that branch may be affected by this too. (Normally you should not do this.)
There are three simple solutions to copy the last version that is in you remote repository, discarding all changes that you have made locally:
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 re
configureing, etc.Discard the local changes with
git reset --hard <hash>and then do agit 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 agit pull.Do a
git fetchto bring the updates to your local reference of the remote branch (usually origin/master) and then do agit reset --hardpassing this reference, ie,git reset --hard origin/master.
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
If you want all changes from master in dev_branch, then:
git switch dev_branch
git reset --hard master
If you have dev_branch pushed to a remote already, you have to do:
git push --force
To force-push the branch to the remote. Warning: This will break the history of the branch for people who cloned it before! Then, other people will have to do a git pull --rebase on the dev_branch to get the changes.
You can also rename the dev branch to something old and then make a new branch from master with the same name:
git branch -m dev_branch old_dev_branch
git branch -m master dev_branch
Or, use the ours strategy — not sure why it wouldn't work for you:
git checkout master
git merge -s ours dev_branch
git checkout dev_branch
git merge master
Why do that? You can delete the branch if it isn't needed anymore (But why? Branches cost next to nothing.). Or you can rename it:
git branch -m dev_branch obsolete_dev
Or you could do this to delete it:
git branch -D dev_branch
Now create a new branch off master (assuming you are on it):
git branch dev_branch
See git branch --help for further options (setting up remotes and all that jazz).
If you now have new branches, you'll have to synchronize with any peer repositories.
Best way to avoid hassle: Have an "active" development branch, if it goes stale, abandon it and create a new one. No history lost that way (could prove crucial sometime).
Have e.g. a branch for each major version, develop on branches off those to fix version bugs, master forges ahead. Use cherry-pick and perhaps merges to port fixes to older versions.
Reset the index and the head to origin/master, but do not reset the working tree:
git reset origin/master
Despite the original question, the top answers can cause problems for people who have a similar problem, but don't want to lose their local files. For example, see Al-Punk and crizCraig's comments.
The following version commits your local changes to a temporary branch (tmp), checks out the original branch (which I'm assuming is master) and merges the updates. You could do this with stash, but I've found it's usually easier to simply use the branch / merge approach.
git checkout -b tmp
git add *; git commit -am "my temporary files"
git checkout master
git fetch origin master
git merge -s recursive -X theirs origin master
where we assume the other repository is origin master.