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
🌐
Squash
squash.io › how-to-force-overwrite-during-git-merge
How to Force Overwrite During Git Merge - Squash Labs
October 28, 2023 - Here's an example of how to use ... to overwrite changes. The --strategy-option=theirs tells git to take the changes from other_branch and overwrite any conflicting changes in the current branch....
🌐
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 ...
🌐
Delft Stack
delftstack.com › home › howto › git › git merge with force overwrite
How to Merge With Force Overwrite in Git | Delft Stack
February 2, 2024 - We found it much easier to use git merge --ours to merge the files and then use git rebase -i to manually re-apply the changes from the branch I was merging from. The above commands would not work on files that had conflicts, but we found the following would work to resolve the conflict.
🌐
Colddata
colddata.com › how-git-rebase-overwriting-with-your-own-changes
How to git rebase overwriting conflicts with your own changes – colddata
# see current branch $ git branch --- * branch-a ... # rebase preferring current branch changes merge during conflicts $ git rebase -Xtheirs branch-b · -Xtheirs will favor your current branch-a code when overwriting merge conflicts, and vice versa -Xours will overwrite merge conflicts with with the code in branch-b.
Find elsewhere
🌐
GitProtect.io
gitprotect.io › strona główna › how to use git pull force to overwrite local files
How to Use Git Pull Force to Overwrite Local Files - Blog | GitProtect.io
September 17, 2024 - Most often it results in the fact that we have to locally download the latest version of the code, perform the merge on our machine, and only then try to send our changes to the remote repository. The git merge command integrates changes from one branch into another, resolving any conflicts that may arise.
🌐
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.
🌐
GitHub
github.com › ingydotnet › git-subrepo › issues › 276
How to force pull to overwrite merge conflicts? · Issue #276 · ingydotnet/git-subrepo
June 22, 2017 - The recommended approach to this on stackoverflow is git fetch --all Then, you have two options: git reset --hard origin/master OR If you are on some other branch: git reset --hard origin/ I can git subrepo fetch --all but I...
Author   ingydotnet
🌐
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
Top answer
1 of 8
225

Not really related to this answer, but I'd ditch git pull, which just runs git fetch followed by git merge. You are doing three merges, which is going to make your Git run three fetch operations, when one fetch is all you will need. Hence:

git fetch origin   # update all our origin/* remote-tracking branches

git checkout demo         # if needed -- your example assumes you're on it
git merge origin/demo     # if needed -- see below

git checkout master
git merge origin/master

git merge -X theirs demo   # but see below

git push origin master     # again, see below

Controlling the trickiest merge

The most interesting part here is git merge -X theirs. As root545 noted, the -X options are passed on to the merge strategy, and both the default recursive strategy and the alternative resolve strategy take -X ours or -X theirs (one or the other, but not both). To understand what they do, though, you need to know how Git finds, and treats, merge conflicts.

A merge conflict can occur within some file1 when the base version differs from both the current (also called local, HEAD, or --ours) version and the other (also called remote or --theirs) version of that same file. That is, the merge has identified three revisions (three commits): base, ours, and theirs. The "base" version is from the merge base between our commit and their commit, as found in the commit graph (for much more on this, see other StackOverflow postings). Git has then found two sets of changes: "what we did" and "what they did". These changes are (in general) found on a line-by-line, purely textual basis. Git has no real understanding of file contents; it is merely comparing each line of text.

These changes are what you see in git diff output, and as always, they have context as well. It's possible that things we changed are on different lines from things they changed, so that the changes seem like they would not collide, but the context has also changed (e.g., due to our change being close to the top or bottom of the file, so that the file runs out in our version, but in theirs, they have also added more text at the top or bottom).

If the changes happen on different lines—for instance, we change color to colour on line 17 and they change fred to barney on line 71—then there is no conflict: Git simply takes both changes. If the changes happen on the same lines, but are identical changes, Git takes one copy of the change. Only if the changes are on the same lines, but are different changes, or that special case of interfering context, do you get a modify/modify conflict.

The -X ours and -X theirs options tell Git how to resolve this conflict, by picking just one of the two changes: ours, or theirs. Since you said you are merging demo (theirs) into master (ours) and want the changes from demo, you would want -X theirs.

Blindly applying -X, however, is dangerous. Just because our changes did not conflict on a line-by-line basis does not mean our changes do not actually conflict! One classic example occurs in languages with variable declarations. The base version might declare an unused variable:

int i;

In our version, we delete the unused variable to make a compiler warning go away—and in their version, they add a loop some lines later, using i as the loop counter. If we combine the two changes, the resulting code no longer compiles. The -X option is no help here since the changes are on different lines.

If you have an automated test suite, the most important thing to do is to run the tests after merging. You can do this after committing, and fix things up later if needed; or you can do it before committing, by adding --no-commit to the git merge command. We'll leave the details for all of this to other postings.


1You can also get conflicts with respect to "file-wide" operations, e.g., perhaps we fix the spelling of a word in a file (so that we have a change), and they delete the entire file (so that they have a delete). Git will not resolve these conflicts on its own, regardless of -X arguments.


Doing fewer merges and/or smarter merges and/or using rebase

There are three merges in both of our command sequences. The first is to bring origin/demo into the local demo (yours uses git pull which, if your Git is very old, will fail to update origin/demo but will produce the same end result). The second is to bring origin/master into master.

It's not clear to me who is updating demo and/or master. If you write your own code on your own demo branch, and others are writing code and pushing it to the demo branch on origin, then this first-step merge can have conflicts, or produce a real merge. More often than not, it's better to use rebase, rather than merge, to combine work (admittedly, this is a matter of taste and opinion). If so, you might want to use git rebase instead. On the other hand, if you never do any of your own commits on demo, you don't even need a demo branch. Alternatively, if you want to automate a lot of this, but be able to check carefully when there are commits that both you and others, made, you might want to use git merge --ff-only origin/demo: this will fast-forward your demo to match the updated origin/demo if possible, and simply outright fail if not (at which point you can inspect the two sets of changes, and choose a real merge or a rebase as appropriate).

This same logic applies to master, although you are doing the merge on master, so you definitely do need a master. It is, however, even likelier that you would want the merge to fail if it cannot be done as a fast-forward non-merge, so this probably also should be git merge --ff-only origin/master.

Let's say that you never do your own commits on demo. In this case we can ditch the name demo entirely:

git fetch origin   # update origin/*

git checkout master
git merge --ff-only origin/master || die "cannot fast-forward our master"

git merge -X theirs origin/demo || die "complex merge conflict"

git push origin master

If you are doing your own demo branch commits, this is not helpful; you might as well keep the existing merge (but maybe add --ff-only depending on what behavior you want), or switch it to doing a rebase. Note that all three methods may fail: merge may fail with a conflict, merge with --ff-only may not be able to fast-forward, and rebase may fail with a conflict (rebase works by, in essence, cherry-picking commits, which uses the merge machinery and hence can get a merge conflict).

2 of 8
95

I had a similar issue, where I needed to effectively replace any file that had changes / conflicts with a different branch.

The solution I found was to use git merge -s ours branch.

Note that the option is -s and not -X. -s denotes the use of ours as a top level merge strategy, -X would be applying the ours option to the recursive merge strategy, which is not what I (or we) want in this case.

Steps, where oldbranch is the branch you want to overwrite with newbranch.

  • git checkout newbranch checks out the branch you want to keep
  • git merge -s ours oldbranch merges in the old branch, but keeps all of our files.
  • git checkout oldbranch checks out the branch that you want to overwrite
  • get merge newbranch merges in the new branch, overwriting the old branch
Top answer
1 of 4
5

Here's a daily routine we've been using in a multi-developer, multi-team environment that's simple enough and works well.

Say you have a dev branch that stores the current in-development version of your product. A master branch that stores your current production version. Every developer has his own branch for a feature being implemented or a bug fix.

Every morning, all devs do the following:
Checkout dev.
Pull.
Checkout dev's working branch.
Rebase onto dev

Throughout the day, the above may repeat. Developers make merge requests to another developer designated as the maintainer of the dev branch.

Developer:
Commit changes.
Push

Dev maintainer:
Checkout branch from developer to merge.
Pull.
Checout dev.
Merge from branch from developer to merge.
Push

2 of 4
4

What you probably want to do is use rebase. A rebase places commits in the destination branch after the commits on the source branch.

So locally, if I'm on my feature branch, I will use git rebase master - this places the commits I have on my feature branch on top of the newest commits in master.

For a remote branch, I typically use git pull --rebase, which stashes your changes, pulls the changes from the server, places your changes on top of the newest changes from the server.

The best visual guide to how rebasing works, that I've come across is this one by Atlassian.

You can find out more about rebase at these resources:

  • man git-rebase
  • git-scm guide on rebasing
  • When do you use git rebase instead of git merge?
🌐
Stack Overflow
stackoverflow.com › questions › 49711578 › what-are-the-circumstances-when-an-overwrite-happens-in-a-git-merge-instead-of
What are the circumstances when an overwrite happens in a git merge, instead of a conflict? - Stack Overflow
When you merge a branch into another, if both sides modify the same file(s), there will has merge conflicts. So what do you want to do, resolve the conflicts by keeping one side version or use stash, rebase etc to replace git merge?
🌐
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.
🌐
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 ...
🌐
Built In
builtin.com › articles › git-merge-theirs
Git Merge Theirs Explained | Built In
July 15, 2024 - The Git merge ‘theirs’ command -Xtheirs overrides changes in the master branch with changes in your feature branch, resolving git merge conflicts. Here’s how.