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 Overflow
🌐
GitHub
docs.github.com › articles › resolving-a-merge-conflict-using-the-command-line
Resolving a merge conflict using the command line - GitHub Docs
Delete the conflict markers <<<<<<<, =======, >>>>>>> and make the changes you want in the final merge. For example, you can keep both changes: If you have questions, please open an issue or ask in our IRC channel if it's more urgent.
Discussions

git - Resolve merge conflicts: Force overwrite all files - Stack Overflow
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... More on stackoverflow.com
🌐 stackoverflow.com
October 4, 2017
Git merge from UiPath fully overwriting files
So, I’m trying to use the git ... using a git lab repository that I created, and trying to merge 2 different versions of the same code. Usually when conflicts are detected, a message appears so you can solve these conflicts, but when I try to merge the codes one simply overwrite the ... More on forum.uipath.com
🌐 forum.uipath.com
16
1
October 15, 2020
version control - How do I force "git pull" to overwrite local files? - Stack Overflow
You could try git pull --force ... running git pull. ... This doesn't work. fatal: refusing to merge unrelated histories 2019-08-23T15:49:29.25Z+00:00 ... Automatic merge failed; fix conflicts and then commit the result. 2020-01-10T16:32:39.55Z+00:00 ... No: error: The following untracked working tree files would be overwritten by merge: ... More on stackoverflow.com
🌐 stackoverflow.com
Aargh! Every Git file update generates a merge conflict
Every time I change a Git file, I get a merge conflict. I thought the idea was for Git to take a sort of snapshot of the file and apply any changes it. No one has edited these files since my last commit. So why doesn’t Git assume that the new file supersedes the old? More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
16
0
September 10, 2019
🌐
Reddit
reddit.com › r/git › git merge overwriting changes.
r/git on Reddit: Git Merge overwriting changes.
February 11, 2022 -

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.

🌐
Atlassian
atlassian.com › git › tutorials › using branches › merge conflicts
How to Resolve Merge Conflicts in Git? | Atlassian Git Tutorial
This indicates a conflict with another developers code. Git will do its best to merge the files but will leave things for you to resolve manually in the conflicted files. A mid-merge failure will output the following error message: 1error: Entry ...
🌐
Git
git-scm.com › docs › git-merge
Git - git-merge Documentation
This is the default behavior. Use --no-overwrite-ignore to abort. ... Abort the current conflict resolution process, and try to reconstruct the pre-merge state.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-fix-merge-conflicts-in-git
How to Fix Merge Conflicts in Git
March 28, 2023 - When these conflicts occur and you attempt to resolve them, Git will automatically annotate the conflicting lines for you with the lesser than symbol (<), equals sign (=), and greater than symbol (>) like this: ... Everything between the lesser than (<) and equals signs (=) is the change in the current branch (the branch you're merging into).
Top answer
1 of 6
22

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!

2 of 6
7

You can do it, like this:

  1. Reset to the commit before your merge.
  2. Perform the merge again
  3. 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.)

Find elsewhere
🌐
Quora
quora.com › Git-is-overwriting-my-code-on-merge-If-someone-else-has-made-any-change-to-the-same-line-instead-of-asking-me-to-choose-how-do-I-resolve-it
Git is overwriting my code on merge. If someone else has made any change to the same line instead of asking me to choose, how do I resolve it? - Quora
Answer (1 of 2): A2A by Sachin Karve If I understand it correctly you should now be allowed to overwrite already existing code, unless you are forcing it to. If those changes are occasional, before you start writing code every time, Pull the code changes. If those changes are happening every ...
🌐
Medium
medium.com › @lada496 › force-merge-in-git-796a1cb997e8
Force merge in Git. This is the last way to deal with merge… | by Lada496 | Medium
October 26, 2022 - This is the last way to deal with merge conflicts. Please read the story below as well if you are interested in how I could avoid this problem. ... Assume we want to merge feature to main but that is impossible. // go to feature branch git switch(or checkout) feature// create new branch git branch feature
🌐
Sean C Davis
seancdavis.com › posts › git-accept-merge-all-changes
Git Merge: Accept All Changes | Sean C Davis
Then, restart the merge using a strategy option. In my case, I wanted to accept all current changes and ignore any incoming changes, which I could accomplish like this: ... If, instead, you know you want to overwrite any current changes and accept all conflicts from incoming changes, you can use the theirs strategy instead: ... It seems like a silly question, given that GitHub is not typically thought about as a CMS.
🌐
Microsoft Learn
learn.microsoft.com › en-us › visualstudio › version-control › git-resolve-conflicts
Resolve merge conflicts in Visual Studio | Microsoft Learn
When you merge one branch into another, file changes from commits in one branch can conflict with the changes in the other. Git attempts to resolve these changes by using the history in your repo to determine what the merged files should look like.
🌐
UiPath Community
forum.uipath.com › feedback › studio
Git merge from UiPath fully overwriting files - Studio - UiPath Community Forum
October 15, 2020 - So, I’m trying to use the git ... using a git lab repository that I created, and trying to merge 2 different versions of the same code. Usually when conflicts are detected, a message appears so you can solve these conflicts, but when I try to merge the codes one simply overwrite the ...
Top answer
1 of 2
99

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
2 of 2
1

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.

🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › devops › repos › git › merging
Resolve Git merge conflicts - Azure Repos | Microsoft Learn
Learn how to resolve merge conflicts stemming from a Git merge or a Git rebase, by using Visual Studio or the command line.
🌐
DEV Community
dev.to › github › how-to-prevent-merge-conflicts-or-at-least-have-less-of-them-109p
How to Prevent Merge Conflicts (or at least have less of them) - DEV Community
June 16, 2022 - You will experience more than one ... you can reduce the number of merge conflicts you encounter. Let's discuss how we can do that! I mentioned this in an earlier DEV post, but I think it's worth repeating. Version control systems, like git, auto-magically manage code ...
🌐
Google Groups
groups.google.com › g › okapi-devel › c › yd8WDJ5p13g
Git how to force server changes (overwrite local changes) and vice versa...
Now if you have uncommitted changes on your local machine I guess you are stuck doing a merge since the above will overwrite your changes :-( Is there a way to do the opposite? Tell git to prefer your local changes - like an svn update?
🌐
freeCodeCamp
forum.freecodecamp.org › t › aargh-every-git-file-update-generates-a-merge-conflict › 313546
Aargh! Every Git file update generates a merge conflict - The freeCodeCamp Forum
September 10, 2019 - Every time I change a Git file, I get a merge conflict. I thought the idea was for Git to take a sort of snapshot of the file and apply any changes it. No one has edited these files since my last commit. So why doesn’t G…
🌐
Sentry
sentry.io › sentry answers › git › resolve merge conflicts in git
Resolve merge conflicts in Git | Sentry
March 15, 2023 - To resolve the conflict, we need to delete the lines containing the branch names and the equals signs. Usually, this means choosing the content of one branch or the other to retain, but Git does not force us to do this.