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).

Answer from torek on Stack Overflow
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
🌐
Reddit
reddit.com › r/git › how do i "force merge"?
r/git on Reddit: How do I "force merge"?
January 10, 2017 -

I have two branches (A and B), each of which is comprised of one working directory (parent/A and parent/B, respectively).

The idea is then to merge it into "test" (branch) in such a way that "test" will have both parent/A and parent/B.

The issue is that when I'm in staging and do a git merge A, it doesn't copy all files. Not that it doesn't override them, but it's those files just don't copy into "test".

What can I do to "import"/merge the all files?

Note, git (on branch A) reports that on branch A there is nothing to commit. working tree clean.

EDIT

git ls-files in branch A lists all the files.

git ls-files in branch "test" doesn't list any.

EDIT AGAIN

"Staging" was a branch name. To avoid complicating things, I renamed it (here) to branch "test"

EDIT AGAIN

I think I figured out what happened: "testing" used to contain parent/A and parent/B. Then I split it into two branches: A and B. In branch A, I deleted parent/B, and in branch B, I deleted parent/A. Now, whenever I merge, say "A", it deletes parent/B. The question now becomes how to do an incremental merge?

Discussions

How to force pull to overwrite merge conflicts?
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... More on github.com
🌐 github.com
6
June 22, 2017
version control - How do I force "git pull" to overwrite local files? - Stack Overflow
How do I force an overwrite of local files on a git pull? My local repository contains a file of the same filename as on the server. error: Untracked working tree file 'example.txt' would be overwritten by merge More on stackoverflow.com
🌐 stackoverflow.com
Git merge overwrites instead of reporting a conflict
Differences in files does not produce conflicts and overrides if commits for that file is in your branch prior your changes. Conflicts appear for the commits that are for the same place but in different branches that are being merged. Could you clarified that this is the case - there are new changes in target branch that appeared since you have created your branch ? More on reddit.com
🌐 r/learnprogramming
6
1
June 2, 2022
How do I "force merge"?

The issue is that when I'm in staging and do a git merge A, it doesn't copy all files. Not that it doesn't override them, but it's those files just don't copy into staging.

What do you mean by "when I'm in staging"? How does one get "in" the index?

More on reddit.com
🌐 r/git
12
6
January 10, 2017
🌐
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 - Force merge in Git 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. GitHub: We Are Save Main Repository …
🌐
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 - Here the matter is a bit different because git push force will overwrite changes in the remote repository, not our local one. Which is potentially much more dangerous! By default, Git will only push if it succeeds in doing the aforementioned ...
🌐
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 › git-pull-force-how-to-overwrite-local-changes-with-git
Git Pull Force – How to Overwrite Local Changes With Git
July 20, 2020 - Just like git push --force allows overwriting remote branches, git fetch --force (or git pull --force) allows overwriting local branches. It is always used with source and destination branches mentioned as parameters.
🌐
JanBask Training
janbasktraining.com › community › data-science › force-merge-git
Git merge with force overwrite -git force merge | JanBask Training Community
July 12, 2021 - This will perform the merge, but if there are conflicts, it will resolve them by favoring the changes from the branch being merged in, effectively overwriting changes from the current branch. Remember, using forceful merge strategies like ours or theirs can lead to loss of changes if not used carefully, so make sure to review the changes and conflicts before proceeding.
Find elsewhere
🌐
ITsyndicate
itsyndicate.org › blog › how-to-use-git-force-pull-properly
Git Pull Force to Overwrite Local Changes - Right Way
You can use the above method to make sure all of your versioned files are force synced with your remote origin. Also, if you want to get rid of untracked files, you can use the following git command: ... Warning: The above command will delete all untracked files from your workspace. Using the above technique to force overwrite your current workspace can save you time.
🌐
Quora
quora.com › How-do-you-force-a-merge-with-Git
How to force a merge with Git - Quora
Answer (1 of 3): Ouch. You really don’t want to - if you are asking the question, most likely you are having some conflicts or such and forcing the merge will make the codebase universe explode. Unless you know what you are doing I recommend to just look through conflicts.
🌐
Squash
squash.io › how-to-force-overwrite-during-git-merge
How to Force Overwrite During Git Merge
October 28, 2023 - Related Article: How To Handle Git Refusing To Merge Unrelated Histories On Rebase · Another way to force overwrite during a git merge is by using the git reset and git checkout commands.
🌐
Google Groups
groups.google.com › g › okapi-devel › c › yd8WDJ5p13g
Git how to force server changes (overwrite local changes) and vice versa...
But it does demand the files be committed in order to perform the merge. If you don't want to commit your local changes, one possibility is to use "git stash save", which takes all your uncommitted changes and hides them, restoring your tree to the original HEAD state.
🌐
Built In
builtin.com › articles › git-merge-theirs
Git Merge Theirs Explained | Built In
July 15, 2024 - More on GitHow to Fix the GitHub Error ‘Remote Host Identification Has Changed’ · In situations where you want to override changes from one branch to another, you can use two merge strategy options: -Xtheirs and -Xours.
🌐
Graphite
graphite.com › guides › git-overwrite-local-remote
How to overwrite a local git local branch with a remote
This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs. Overwriting your local branch with the remote version is important because it removes local divergences or unintended changes, providing a clean and accurate reflection of the current state of the shared codebase. This prevents merge conflicts and ensures consistency, enabling smoother collaboration with your team.
🌐
Git Tower
git-tower.com › learn › git faq › how do i force git pull to overwrite local files?
How do I force git pull to overwrite local files? | Learn Version Control with Git
1 week ago - You might expect that git pull --force would force Git to overwrite your local changes. It does not. The --force flag is passed down to git fetch, not git merge.
🌐
Codecademy
codecademy.com › article › force-git-pull
How to Force Git Pull to Overwrite Local Changes in Git | Codecademy
Learn how to force `git pull` in Git to overwrite local changes safely using `git reset --hard` and `git stash`. Understand use cases, risks, and best practices.
🌐
OpenReplay
blog.openreplay.com › openreplay blog › git force pull: how to safely overwrite local changes and sync with remote
Git Force Pull: How to Safely Overwrite Local Changes and Sync with Remote
February 10, 2025 - While git pull --force may sound like the right command, it’s better to use git fetch and git reset --hard to overwrite local changes and sync with the remote repo. Just be careful, as reset --hard and clean -fd are irreversible!
🌐
Continuously Merging
articles.mergify.com › git-overwrite-local-branch-with-remote
Git Overwrite Local Branch with Remote Safely
August 26, 2025 - The git fetch and git reset --hard origin/branch-name workflow is a 100% local operation. You are simply pulling down the latest information from the remote server and forcing your local copy to match it.
🌐
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