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
New to git. I'm attempting to merge two branches. Git reports merge conflicts for some of the files, but for some files it simply takes the file as it is in the other branch without reporting a conflict even though there are differences in the file contents.
Is this intended behaviour for git in certain circumstances? It certainly isn't desired. Is it possible that my history is somehow messed up?
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
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.
Hi,
I'm a bit confused as to when exactly I get merge conflicts? What causes git to throw a merge conflict rather than overwrite changes?
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).
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 newbranchchecks out the branch you want to keepgit merge -s ours oldbranchmerges in the old branch, but keeps all of our files.git checkout oldbranchchecks out the branch that you want to overwriteget merge newbranchmerges in the new branch, overwriting the old branch
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
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?
Sure you can overwrite the file with merge markers with content of new file. Just do git add <file> and git commit after that.
Edit the file in any editor you wish.
How to set the desired git editor?
# Set the default git editor
git config --global core.editor <your editor>
Once the conflict is resolved simply save the file then add it to the index and commit

Tip:
If you wish to add some of the changes and not all of them at once use the git add -p
These are the options you can do within add -p:
y - stage this hunk
n - do not stage this hunk
q - quit, do not stage this hunk nor any of the remaining ones
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
Once you use the s it will pick the chunk of code which can be considered as a standalone change. If you want to split it, even more, you will have to use the e to edit the hunk and then add it back to the stage area.
