How to resolve merge conflicts in Git

4 242

1 484

Is there a good way to explain how to resolve merge conflicts in Git?


Posted 2008-10-02T11:31:05.777

Reputation: 59 912


The following blog post seems to give a very good example on how to handle merge conflict with Git that should get you going in the right direction. Handling and Avoiding Conflicts in Git

– mwilliams – 2008-10-02T11:40:03.117


You can configure a merge tool (kdiff3 and then use git mergetool. When you're working in large developer teams you'll always encounter merge conflicts.

– Grady G Cooper – 2015-04-18T05:37:12.303

Don't forget that you can mitigate most merge conflicts by regularly merging downstream! – Ant P – 2015-07-27T09:50:33.853

This seems to be a detailed tutorial -

– Rajavanya Subramaniyan – 2015-12-27T08:29:36.163

This is not specific to this question, but I had to say somewhere that the term "conflict" in the common language for version control systems (VCS) was very badly chosen. What we call conflict in VCS is completely different than what we normally see as a conflict in real life. Say, we have a car accident. One driver says "Oh I did not see you". The other says "Ok, we can fix it". This is not at all a conflict. The conflict can occur after. In VCS, it's the opposite: the simple collision is called a conflict, a modification that consciously undo a known modification cannot even be a conflict. – None – 2016-04-23T20:51:54.760

A reaction to my previous comment could be "We know that when there is a git conflict, there is no real conflict," but there is more to it. When programmers work in team there are differences of opinions. It may happen that a programmer consciously undo what another did and that would be closer to a real conflict, but git conflicts have nothing to do with this process. Anyway, I think the term "collision" or "accident" instead of "conflict" would have been closer to what it actually is, because it occurs when both sides did not see the other side coming. – None – 2016-04-24T18:55:28.933

how do you ask git which files need merging? – Charlie Parker – 2016-07-16T03:57:34.767

@CharlieParker git status will show you quite clearly which files that are in conflict and needs to be merged manually – Spoike – 2016-07-18T08:59:40.333

A niche, related question on resolving a conflict in just one file, from command line, using three-way merge with given strategy:

– jakub.g – 2016-08-25T08:48:56.797

Fascinating question: Asked in 2008, 100% open ended with no hint at all about what it's really about (is it about GUI? about the git commands? about semantics? about pushing/pulling or just general conflicts?) - completely unanswerable. 30 answers, all of them (as far as a quick glance shows) more or less going on about different diff3 and merge tools, none accepted. Top-voted answer mentions a command that does not even work out of the box with a default git installation. Managed to hit the SE start page for me today, 2017, with 1.3m views and thousands of votes both ways. Fascinating. – AnoE – 2017-07-11T16:48:34.023 has a nice way of visualizing the issue. Make sure you see this answer. (It has ways to also edit and resolve right from the site, but that's just a convenience.)

– Honey – 2017-09-19T15:02:51.543

Please select a correct response to this question so that it gets pushed to the top. I only say this because there are now multiple pages of answers. – Blake Frederick – 2017-11-16T17:34:27.120


2 502

Try: git mergetool

It opens a GUI that steps you through each conflict, and you get to choose how to merge. Sometimes it requires a bit of hand editing afterwards, but usually it's enough by itself. It is much better than doing the whole thing by hand certainly.

As per @JoshGlover comment:

The command doesn't necessarily open a GUI unless you install one. Running git mergetool for me resulted in vimdiff being used. You can install one of the following tools to use it instead: meld, opendiff, kdiff3, tkdiff, xxdiff, tortoisemerge, gvimdiff, diffuse, ecmerge, p4merge, araxis, vimdiff, emerge.

Below is the sample procedure to use vimdiff for resolve merge conflicts. Based on this link

Step 1: Run following commands in your terminal

git config merge.tool vimdiff
git config merge.conflictstyle diff3
git config mergetool.prompt false

This will set vimdiff as the default merge tool.

Step 2: Run following command in terminal

git mergetool

Step 3: You will see a vimdiff display in following format

  |       |      |       |
  |       |      |       |
  |      MERGED          |
  |                      |

These 4 views are

LOCAL – this is file from the current branch

BASE – common ancestor, how file looked before both changes

REMOTE – file you are merging into your branch

MERGED – merge result, this is what gets saved in the repo

You can navigate among these views using ctrl+w. You can directly reach MERGED view using ctrl+w followed by j.

More info about vimdiff navigation here and here

Step 4. You could edit the MERGED view the following way

If you want to get changes from REMOTE

:diffg RE  

If you want to get changes from BASE

:diffg BA  

If you want to get changes from LOCAL

:diffg LO 

Step 5. Save, Exit, Commit and Clean up

:wqa save and exit from vi

git commit -m "message"

git clean Remove extra files (e.g. *.orig) created by diff tool.

Peter Burns

Posted 2008-10-02T11:31:05.777

Reputation: 34 028

5You can also use Beyond Compare 3 now (git mergetool -t bc3). – AzP – 2012-08-30T14:59:24.247

5downloading beyond compare 3 worked great as a windows user didn't take me too long to learn and didn't need to change anything since git is set up to use it from the start. only problem is now i don't know the console command to say the merge conflicts are done. – anon58192932 – 2013-03-05T23:36:19.030

Although it is probably the fault of the sysadmins at my university, I could not run mergetool because of bcomp.exe not existing. – 2rs2ts – 2013-04-09T14:38:10.800

4@advocate Just git add the fixed files. Doing so effectively marks them as resolved. – eikonomega – 2013-09-06T17:26:22.853

5In Windows, Winmerge is a pretty good merge tool. I have been using it for 5 years. – RoboAlex – 2013-09-24T21:49:16.883

51FYI you can use git mergetool -y to save a few keystrokes if you're merging a lot of files at once. – davr – 2010-06-17T23:32:50.860

I'm on Windows and I received this message when running the command git mergetool:

This message is displayed because 'merge.tool' is not configured. See 'git mergetool --tool-help' or 'git help config' for more details. 'git mergetool' will now attempt to use one of the following tools: tortoisemerge emerge vimdiff No known merge tool is available.

I'll try installing Beyond Compare. – Francisco Zarabozo – 2014-02-22T17:29:31.883


Or you can learn how to use vimdiff: It's actually much simpler than it looks.

– Ruslan Osipov – 2014-05-30T19:15:58.257


IMO This answer could be improved by adding detail about setting the tools, and if you describe what happens if you start using the wrong gui and need to switch (does that ruin the merge?)

– geneorama – 2016-03-29T17:27:45.840

Using FileMerge app on Mac, I figured out if you select a diff'd item, you can use the left/right arrow keys to select which one of the two to use – Gene Bo – 2016-05-16T18:59:39.600

Should I use meld to resolve conflicts?

– hellboy – 2016-11-14T09:12:33.530

353Well, it doesn't necessarily open a GUI unless you install one. Running git mergetool for me resulted in vimdiff being used. You can install one of the following tools to use it instead: meld opendiff kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse ecmerge p4merge araxis vimdiff emerge. – Josh Glover – 2011-05-11T14:00:49.067

31Good point Josh. On ubuntu I've had the best luck with meld, its three way merge display isn't bad. On OSX git chose a nice default. – Peter Burns – 2011-05-24T05:08:08.600

17This opened KDiff3. Which I have absolutely no clue how to use. – David Murdoch – 2011-06-10T18:46:25.080


I highly recommend using the 'meld' tool. It's a cross platform diff tool that makes it easier to merge than using vim. If you are a Vim user then there is the fugitive.vim plugin

– icc97 – 2017-06-27T04:45:54.913

This is amazing, thanks so much. I had no idea I even had this installed. For me, meld opened by default, which is identical to the PyCharm confilct manager I used to use. – Anonymous – 2018-02-06T20:51:50.650

On windows with VsCode? – Legends – 2018-06-10T11:13:12.453

For Step 4, if you want to take all the changes instead of just the current buffer from remote/local/base, you can use %diffg instead (% is a range specification,[range]), e.g. :%diffg BA or :%diffg LO or :%diffg RE

– Balraj Bains – 2018-08-02T06:58:31.697

1 622

Here's a probable use-case, from the top:

You're going to pull some changes, but oops, you're not up to date:

git fetch origin
git pull origin master

From ssh://[email protected]:22/projectname
 * branch            master     -> FETCH_HEAD
Updating a030c3a..ee25213
error: Entry 'filename.c' not uptodate. Cannot merge.

So you get up-to-date and try again, but have a conflict:

git add filename.c
git commit -m "made some wild and crazy changes"
git pull origin master

From ssh://[email protected]:22/projectname
 * branch            master     -> FETCH_HEAD
Auto-merging filename.c
CONFLICT (content): Merge conflict in filename.c
Automatic merge failed; fix conflicts and then commit the result.

So you decide to take a look at the changes:

git mergetool

Oh me, oh my, upstream changed some things, but just to use my changes...

git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"

And then we try a final time

git pull origin master

From ssh://[email protected]:22/projectname
 * branch            master     -> FETCH_HEAD
Already up-to-date.



Posted 2008-10-02T11:31:05.777

Reputation: 48 057

2This was indeed what I was looking for! Why, oh why, can't "git checkout -- filename.c" work here, too.. Would be too consistent, I guess. – Zds – 2012-03-27T08:23:07.627

What exactly does the --ours/theirs commands do? – AaronLS – 2013-02-23T06:47:54.197

179Careful! The meaning of --ours and --theirs is reversed. --ours == the remote. --theirs == local. See git merge --help – mmell – 2013-03-04T22:56:21.383

1If you need to do it to many files: git checkout --theirs *.java – Jorge Nunez Newton – 2013-03-13T18:37:54.503

1Thanks so much. I need to confirm that: --theirs == server, remote. --ours == local – Thanh – 2013-05-08T03:51:58.533

55In my case, I confirm that --theirs = remote repository, --ours = my own local repository. It is the opposite of @mmell comments. – Aryo – 2013-06-22T12:59:08.580

5If you git stash pop and run in to a conflict, --ours will be HEAD and --theirs will be the stashed changes. – Matt Briançon – 2013-10-16T21:37:04.337


@mmell Only on a rebase, apparently. See this question

– Navin – 2013-11-10T06:19:14.430

1should we do 'git commit -m "using theirs"'? Last time I only "add" resolved file and it was enough to continue merge operation. – Yura – 2014-04-08T13:45:13.873

160Guys, "ours" and "theirs" is relative to whether or not you are merging or rebasing. If you're merging, then "ours" means the branch you're merging into, and "theirs" is the branch you're merging in. When you're rebasing, then "ours" means the commits you're rebasing onto, while "theirs" refers to the commits that you want to rebase. – None – 2014-05-26T04:27:22.743

If instead of brutally doing git checkout --ours file.txt you want to perform an automatic three-way-merge on that file using --ours strategy, you can use git merge-file for that, see

– jakub.g – 2016-08-25T08:52:15.090

2Why would you fetch before a pull? Doesn't it always fetch when you pull automatically? – TankorSmash – 2016-08-25T20:25:04.763

16This was super helpful because I had a lot of merge errors with binary files (art assets) and merging those seems to always fail, so I need to overwrite it with the new file always and not "merge" – petrocket – 2011-06-08T17:39:40.430


I find merge tools rarely help me understand the conflict or the resolution. I'm usually more successful looking at the conflict markers in a text editor and using git log as a supplement.

Here are a few tips:

Tip One

The best thing I have found is to use the "diff3" merge conflict style:

git config merge.conflictstyle diff3

This produces conflict markers like this:

Changes made on the branch that is being merged into. In most cases,
this is the branch that I have currently checked out (i.e. HEAD).
The common ancestor version.
Changes made on the branch that is being merged in. This is often a 
feature/topic branch.

The middle section is what the common ancestor looked like. This is useful because you can compare it to the top and bottom versions to get a better sense of what was changed on each branch, which gives you a better idea for what the purpose of each change was.

If the conflict is only a few lines, this generally makes the conflict very obvious. (Knowing how to fix a conflict is very different; you need to be aware of what other people are working on. If you're confused, it's probably best to just call that person into your room so they can see what you're looking at.)

If the conflict is longer, then I will cut and paste each of the three sections into three separate files, such as "mine", "common" and "theirs".

Then I can run the following commands to see the two diff hunks that caused the conflict:

diff common mine
diff common theirs

This is not the same as using a merge tool, since a merge tool will include all of the non-conflicting diff hunks too. I find that to be distracting.

Tip Two

Somebody already mentioned this, but understanding the intention behind each diff hunk is generally very helpful for understanding where a conflict came from and how to handle it.

git log --merge -p <name of file>

This shows all of the commits that touched that file in between the common ancestor and the two heads you are merging. (So it doesn't include commits that already exist in both branches before merging.) This helps you ignore diff hunks that clearly are not a factor in your current conflict.

Tip Three

Verify your changes with automated tools.

If you have automated tests, run those. If you have a lint, run that. If it's a buildable project, then build it before you commit, etc. In all cases, you need to do a bit of testing to make sure your changes didn't break anything. (Heck, even a merge without conflicts can break working code.)

Tip Four

Plan ahead; communicate with co-workers.

Planning ahead and being aware of what others are working on can help prevent merge conflicts and/or help resolve them earlier -- while the details are still fresh in mind.

For example, if you know that you and another person are both working on different refactoring that will both affect the same set of files, you should talk to each other ahead of time and get a better sense for what types of changes each of you is making. You might save considerable time and effort if you conduct your planned changes serially rather than in parallel.

For major refactorings that cut across a large swath of code, you should strongly consider working serially: everybody stops working on that area of the code while one person performs the complete refactoring.

If you can't work serially (due to time pressure, maybe), then communicating about expected merge conflicts at least helps you solve the problems sooner while the details are still fresh in mind. For example, if a co-worker is making a disruptive series of commits over the course of a one-week period, you may choose to merge/rebase on that co-workers branch once or twice each day during that week. That way, if you do find merge/rebase conflicts, you can solve them more quickly than if you wait a few weeks to merge everything together in one big lump.

Tip Five

If you're unsure of a merge, don't force it.

Merging can feel overwhelming, especially when there are a lot of conflicting files and the conflict markers cover hundreds of lines. Often times when estimating software projects we don't include enough time for overhead items like handling a gnarly merge, so it feels like a real drag to spend several hours dissecting each conflict.

In the long run, planning ahead and being aware of what others are working on are the best tools for anticipating merge conflicts and prepare yourself to resolve them correctly in less time.

Mark E. Haase

Posted 2008-10-02T11:31:05.777

Reputation: 17 918

5The diff3 option is a great feature to have with merges. The only GUI I've come across that shows it is Perforce's p4merge, which can be installed and used separately from Perforce's other tools (which I've not used, but heard complaints about). – alxndr – 2014-05-01T22:15:41.677

2After a rebase attempt which resulted in a merge conflict: $ git log --merge -p build.xml output: fatal: --merge without MERGE_HEAD? – Ed Randall – 2016-06-17T09:15:07.557

what if I have changes on one file from branch1 and deletion of that file in branch2. How can I solve that merge conflict? Is there any way using git where I can merge them by keeping the changes of one branch? – Honey – 2017-01-21T14:48:16.383

git config merge.conflictstyle diff3 - thank you, sir. This is amazing and has freed me from trying to find (and pay $$) for a good 3 way merge GUI. IMO this is better because it shows the common ancestor as well as local/remote, and shows the last commit log lines which (AFAIK) no GUI does. The commits definitely help you identify what code belongs to what branch. – ffxsam – 2017-04-04T16:00:53.237

I have found that sometimes the diff3 conflictstyle results in enormous diff hunks that are largely identical, whereas the default will produce smaller, more manageable, hunks. Unfortunately, I don't have a reproducer I can use for a bug report. But if you encounter this problem you might consider turning off the option temporarily. – Dave Abrahams – 2017-07-02T03:31:12.137


  1. Identify which files are in conflict (Git should tell you this).

  2. Open each file and examine the diffs; Git demarcates them. Hopefully it will be obvious which version of each block to keep. You may need to discuss it with fellow developers who committed the code.

  3. Once you've resolved the conflict in a file git add the_file.

  4. Once you've resolved all conflicts, do git rebase --continue or whatever command Git said to do when you completed.


Posted 2008-10-02T11:31:05.777

Reputation: 16 125

1Yes, technically, this answers the question which as asked, but is not a usable answer, in my opinion, sorry. What's the point of making one branch the same as another? Of course a merge will have conflicts.. – Thufir – 2012-08-09T05:56:27.290

3Thulfir: who said anything about making one branch the same as another? There are different scenarios where you need to merge, without "making one branch the same as another". One is when you're done with a development branch and want to incorporate its changes into the master branch; after this, the development branch can be deleted. Another one is when you want to rebase your development branch, in order to ease the eventual final merge into the master. – Teemu Leisti – 2012-09-21T08:50:19.667

3@JustinGrant git add stages files in the index; it does not add anything to the repository. git commit adds things to the repository. This usage makes sense for merges -- the merge automatically stages all of the changes that can be merged automatically; it is your responsibility to merge the rest of the changes and add those to the index when you are done. – Mark E. Haase – 2012-10-17T15:13:05.923

36@Justin Think of Git as tracking content rather than tracking files. Then it's easy to see that the content you've updated isn't in the repository and needs to be added. This way of thinking also explains why Git doesn't track empty folders: Although they are technically files, there isn't any content to track. – Gareth – 2010-10-12T09:17:27.903

how do you ask git which files need merging? – Charlie Parker – 2016-07-16T03:57:19.750

1Step 2 amuses me. It sounds so easy. Then one day you have a merge conflict that impacts thousands of files written by people that no longer work for the company. Sigh... – Brian Knoblauch – 2016-08-02T17:08:29.853

5content is there, conflict occurs because there 2 version of content. Therefore "git add" does not sound correct. And it does not work (git add, git commit) if you want commit only that one file after conflict was resolved ("fatal: cannot do a partial commit during a merge.") – Dainius – 2011-09-14T09:19:10.090


Check out the answers in Stack Overflow question Aborting a merge in Git, especially Charles Bailey's answer which shows how to view the different versions of the file with problems, for example,

# Common base version of the file.
git show :1:some_file.cpp

# 'Ours' version of the file.
git show :2:some_file.cpp

# 'Theirs' version of the file.
git show :3:some_file.cpp

Pat Notz

Posted 2008-10-02T11:31:05.777

Reputation: 127 652

Also check out the "-m" option to "git checkout -m" - it allows you to extract the different flies back out into your workspace – qneill – 2015-02-05T22:06:41.840

This saved me. Looking at each file separately allowed me to remember what I was going for in each branch. Then I could make the decision to choose. – Rohmer – 2016-05-14T21:59:15.797


Merge conflicts happens when changes are made to a file at the same time. Here is how to solve it.

git CLI

Here are simple steps what to do when you get into conflicted state:

  1. Note the list of conflicted files with: git status (under Unmerged paths section).
  2. Solve the conflicts separately for each file by one of the following approaches:

    • Use GUI to solve the conflicts: git mergetool (the easiest way).

    • To accept remote/other version, use: git checkout --theirs path/file. This will reject any local changes you did for that file.

    • To accept local/our version, use: git checkout --ours path/file

      However you've to be careful, as remote changes that conflicts were done for some reason.

      Related: What is the precise meaning of "ours" and "theirs" in git?

    • Edit the conflicted files manually and look for the code block between <<<<</>>>>> then choose the version either from above or below =====. See: How conflicts are presented.

    • Path and filename conflicts can be solved by git add/git rm.

  3. Finally, review the files ready for commit using: git status.

    If you still have any files under Unmerged paths, and you did solve the conflict manually, then let Git know that you solved it by: git add path/file.

  4. If all conflicts were solved successfully, commit the changes by: git commit -a and push to remote as usual.

See also: Resolving a merge conflict from the command line at GitHub


I've successfully used DiffMerge which can visually compare and merge files on Windows, macOS and Linux/Unix.

It graphically can show the changes between 3 files and it allows automatic merging (when safe to do so) and full control over editing the resulting file.


Image source: DiffMerge (Linux screenshot)

Simply download it and run in repo as:

git mergetool -t diffmerge .


On macOS you can install via:

brew install caskroom/cask/brew-cask
brew cask install diffmerge

And probably (if not provided) you need the following extra simple wrapper placed in your PATH (e.g. /usr/bin):

exec ${DIFFMERGE_EXE} --nosplash "[email protected]"

Then you can use the following keyboard shortcuts:

  • -Alt-Up/Down to jump to previous/next changes.
  • -Alt-Left/Right to accept change from left or right

Alternatively you can use opendiff (part of Xcode Tools) which lets you merge two files or directories together to create a third file or directory.


Posted 2008-10-02T11:31:05.777

Reputation: 63 134


If you're making frequent small commits, then start by looking at the commit comments with git log --merge. Then git diff will show you the conflicts.

For conflicts that involve more than a few lines, it's easier to see what's going on in an external GUI tool. I like opendiff -- Git also supports vimdiff, gvimdiff, kdiff3, tkdiff, meld, xxdiff, emerge out of the box and you can install others: git config merge.tool "your.tool" will set your chosen tool and then git mergetool after a failed merge will show you the diffs in context.

Each time you edit a file to resolve a conflict, git add filename will update the index and your diff will no longer show it. When all the conflicts are handled and their files have been git add-ed, git commit will complete your merge.


Posted 2008-10-02T11:31:05.777

Reputation: 15 349

7Using "git add" is the real trick here. You may not even want to commit (maybe you want to stash), but you have to do "git add" to complete the merge. I think mergetool does the add for you (although it isn't in the manpage), but if you do the merge manually, you need to use "git add" to complete it (even if you don't want to commit). – nobar – 2010-10-25T09:37:00.633


See How Conflicts Are Presented or, in Git, the git merge documentation to understand what merge conflict markers are.

Also, the How to Resolve Conflicts section explains how to resolve the conflicts:

After seeing a conflict, you can do two things:

  • Decide not to merge. The only clean-ups you need are to reset the index file to the HEAD commit to reverse 2. and to clean up working tree changes made by 2. and 3.; git merge --abort can be used for this.

  • Resolve the conflicts. Git will mark the conflicts in the working tree. Edit the files into shape and git add them to the index. Use git commit to seal the deal.

You can work through the conflict with a number of tools:

  • Use a mergetool. git mergetool to launch a graphical mergetool which will work you through the merge.

  • Look at the diffs. git diff will show a three-way diff, highlighting changes from both the HEAD and MERGE_HEAD versions.

  • Look at the diffs from each branch. git log --merge -p <path> will show diffs first for the HEAD version and then the MERGE_HEAD version.

  • Look at the originals. git show :1:filename shows the common ancestor, git show :2:filename shows the HEAD version, and git show :3:filename shows the MERGE_HEAD version.

You can also read about merge conflict markers and how to resolve them in the Pro Git book section Basic Merge Conflicts.


Posted 2008-10-02T11:31:05.777



For Emacs users which want to resolve merge conflicts semi-manually:

git diff --name-status --diff-filter=U

shows all files which require conflict resolution.

Open each of those files one by one, or all at once by:

emacs $(git diff --name-only --diff-filter=U)

When visiting a buffer requiring edits in Emacs, type

ALT+x vc-resolve-conflicts

This will open three buffers (mine, theirs, and the output buffer). Navigate by pressing 'n' (next region), 'p' (prevision region). Press 'a' and 'b' to copy mine or theirs region to the output buffer, respectively. And/or edit the output buffer directly.

When finished: Press 'q'. Emacs asks you if you want to save this buffer: yes. After finishing a buffer mark it as resolved by running from the teriminal:

git add FILENAME

When finished with all buffers type

git commit

to finish the merge.


Posted 2008-10-02T11:31:05.777

Reputation: 1 420


Please follow the following steps to fix merge conflicts in Git:

  1. Check the Git status: git status

  2. Get the patchset: git fetch (checkout the right patch from your Git commit)

  3. Checkout a local branch (temp1 in my example here): git checkout -b temp1

  4. Pull the recent contents from master: git pull --rebase origin master

  5. Start the mergetool and check the conflicts and fix them...and check the changes in the remote branch with your current branch: git mergetool

  6. Check the status again: git status

  7. Delete the unwanted files locally created by mergetool, usually mergetool creates extra file with *.orig extension. Please delete that file as that is just the duplicate and fix changes locally and add the correct version of your files. git add #your_changed_correct_files

  8. Check the status again: git status

  9. Commit the changes to the same commit id (this avoids a new separate patch set): git commit --amend

  10. Push to the master branch: git push (to your Git repository)


Posted 2008-10-02T11:31:05.777

Reputation: 831


Simply, if you know well that changes in one of the repositories is not important, and want to resolve all changes in favor of the other one, use:

git checkout . --ours

to resolve changes in the favor of your repository, or

git checkout . --theirs

to resolve changes in favor of the other or the main repository.

Or else you will have to use a GUI merge tool to step through files one by one, say the merge tool is p4merge, or write any one's name you've already installed

git mergetool -t p4merge

and after finishing a file, you will have to save and close, so the next one will open.

Mohamed Selim

Posted 2008-10-02T11:31:05.777

Reputation: 1 562


I either want my or their version in full, or want to review individual changes and decide for each of them.

Fully accept my or theirs version:

Accept my version (local, ours):

git checkout --ours -- <filename>
git add <filename>              # Marks conflict as resolved
git commit -m "merged bla bla"  # An "empty" commit

Accept their version (remote, theirs):

git checkout --theirs -- <filename>
git add <filename>
git commit -m "merged bla bla"

If you want to do for all conflict files run:

git merge --strategy-option ours


git merge --strategy-option theirs

Review all changes and accept them individually

  1. git mergetool
  2. Review changes and accept either version for each of them.
  3. git add <filename>
  4. git commit -m "merged bla bla"

Default mergetool works in command line. How to use a command line mergetool should be a separate question.

You can also install visual tool for this, e.g. meld and run

git mergetool -t meld

It will open local version (ours), "base" or "merged" version (the current result of the merge) and remote version (theirs). Save the merged version when you are finished, run git mergetool -t meld again until you get "No files need merging", then go to Steps 3. and 4.


Posted 2008-10-02T11:31:05.777

Reputation: 734


You could fix merge conflicts in a number of ways as other have detailed.

I think the real key is knowing how changes flow with local and remote repositories. The key to this is understanding tracking branches. I have found that I think of the tracking branch as the 'missing piece in the middle' between me my local, actual files directory and the remote defined as origin.

I've personally got into the habit of 2 things to help avoid this.

Instead of:

git add .
git commit -m"some msg"

Which has two drawbacks -

a) All new/changed files get added and that might include some unwanted changes.
b) You don't get to review the file list first.

So instead I do:

git add file,file2,file3...
git commit # Then type the files in the editor and save-quit.

This way you are more deliberate about which files get added and you also get to review the list and think a bit more while using the editor for the message. I find it also improves my commit messages when I use a full screen editor rather than the -m option.

[Update - as time has passed I've switched more to:

git status # Make sure I know whats going on
git add .
git commit # Then use the editor


Also (and more relevant to your situation), I try to avoid:

git pull


git pull origin master.

because pull implies a merge and if you have changes locally that you didn't want merged you can easily end up with merged code and/or merge conflicts for code that shouldn't have been merged.

Instead I try to do

git checkout master
git fetch   
git rebase --hard origin/master # or whatever branch I want.

You may also find this helpful:

git branch, fork, fetch, merge, rebase and clone, what are the differences?

Michael Durrant

Posted 2008-10-02T11:31:05.777

Reputation: 57 996

Hey, I kinda understood your answer. But since i'm new to github merge conflicts, I think there is something missing. What happens to your local modifications when you do git checkout master and git fetch and git rebase --hard origin/master – Suhaib – 2017-08-08T17:43:36.637

I believe you should add more details on what to do. Another example which is confusing me, you mentioned in your answer: we do git add ., will it save our local modifications so we can follow up with git checkout master ? or are they two different scenarios ? – Suhaib – 2017-08-08T17:47:14.410



In speaking of pull/fetch/merge in the above answers, I would like to share an interesting and productive trick,

git pull --rebase

This above command is the most useful command in my git life which saved a lots of time.

Before pushing your newly committed change to remote server, try git pull --rebase rather git pull and manual merge and it will automatically sync latest remote server changes (with a fetch + merge) and will put your local latest commit at the top in git log. No need to worry about manual pull/merge.

In case of conflict, just use

git mergetool
git add conflict_file
git rebase --continue

Find details at:

Sazzad Hissain Khan

Posted 2008-10-02T11:31:05.777

Reputation: 14 113


CoolAJ86's answer sums up pretty much everything. In case you have changes in both branches in the same piece of code you will have to do a manual merge. Open the file in conflict in any text editor and you should see following structure.

(Code not in Conflict)
(first alternative for conflict starts here)
Multiple code lines here
(second alternative for conflict starts here)
Multiple code lines here too    
(Code not in conflict here)

Choose one of the alternatives or a combination of both in a way that you want new code to be, while removing equal signs and angle brackets.

git commit -a -m "commit message"
git push origin master


Posted 2008-10-02T11:31:05.777

Reputation: 4 455


There are 3 steps:

  1. Find which files cause conflicts by command

    git status
  2. Check the files, in which you would find the conflicts marked like

  3. Change it to the way you want it, then commit with commands

    git add solved_conflicts_files
    git commit -m 'merge msg'

Qijun Liu

Posted 2008-10-02T11:31:05.777

Reputation: 711


git log --merge -p [[--] path]

Does not seem to always work for me and usually ends up displaying every commit that was different between the two branches, this happens even when using -- to separate the path from the command.

What I do to work around this issue is open up two command lines and in one run

git log ..$MERGED_IN_BRANCH --pretty=full -p [path]

and in the other

git log $MERGED_IN_BRANCH.. --pretty=full -p [path]

Replacing $MERGED_IN_BRANCH with the branch I merged in and [path] with the file that is conflicting. This command will log all the commits, in patch form, between (..) two commits. If you leave one side empty like in the commands above git will automatically use HEAD (the branch you are merging into in this case).

This will allow you to see what commits went into the file in the two branches after they diverged. It usually makes it much easier to solve conflicts.

Brian Di Palma

Posted 2008-10-02T11:31:05.777

Reputation: 4 494


As of December 12th 2016, you can merge branches and resolve conflicts on

Thus, if you don't want to use the command-line or any 3rd party tools that are offered here from older answers, go with GitHub's native tool.

This blog post explains in detail, but the basics are that upon 'merging' two branches via the UI, you will now see a 'resolve conflicts' option that will take you to an editor allowing you to deal with these merge conflicts.

enter image description here


Posted 2008-10-02T11:31:05.777

Reputation: 1 156


Using patience

I'm surprised no one else spoke about resolving conflict using patience with the merge recursive strategy. For a big merge conflict, using patience provided good results for me. The idea is that it will try to match blocks rather than individual lines.

If you change the indentation of your program for instance, the default Git merge strategy sometimes matches single braces { which belongs to different functions. This is avoided with patience:

git merge -s recursive -X patience other-branch

From the documentation:

With this option, merge-recursive spends a little extra time to avoid 
mismerges that sometimes occur due to unimportant matching lines 
(e.g., braces from distinct functions). Use this when the branches to 
be merged have diverged wildly.

Comparison with the common ancestor

If you have a merge conflict and want to see what others had in mind when modifying their branch, it's sometimes easier to compare their branch directly with the common ancestor (instead of our branch). For that you can use merge-base:

git diff $(git merge-base <our-branch> <their-branch>) <their-branch>

Usually, you only want to see the changes for a particular file:

git diff $(git merge-base <our-branch> <their-branch>) <their-branch> <file>


Posted 2008-10-02T11:31:05.777

Reputation: 1 054


I always follow the below steps to avoid conflicts.

  • git checkout master (Come to the master branch)
  • git pull (Update your master to get the latest code)
  • git checkout -b mybranch (Checkout a new a branch and start working on that branch so that your master always remains top of trunk.)
  • git add . AND git commit AND git push (on your local branch after your changes)
  • git checkout master (Come back to your master.)

Now you can do the same and maintain as many local branches you want and work simultaneous my just doing a git checkout to your branch when ever necessary.


Posted 2008-10-02T11:31:05.777

Reputation: 588


If you want to merge from branch (test) to master, you can follow these steps:

Step 1: Go to the branch

git checkout test

Step 2: git pull --rebase origin master

Step 3: If there are some conflicts, go to these files to modify it.

Step 4: Add these changes

git add #your_changes_files

Step 5: git rebase --continue

Step 6: if there is still conflict, go back to step 3 again. If there is no conflict, do following: git push origin +test

Step 7: And then there is no conflict between test and master. You can use merge directly.


Posted 2008-10-02T11:31:05.777

Reputation: 8 733


Merge conflicts could occur in different situations:

  • When running "git fetch" and then "git merge"
  • When running "git fetch" and then "git rebase"
  • When running "git pull" (which is actually equal to one of the above-mentioned conditions)
  • When running "git stash pop"
  • When you're applying git patches (commits that are exported to files to be transferred, for example, by email)

You need to install a merge tool which is compatible with Git to resolve the conflicts. I personally use KDiff3, and I've found it nice and handy. You can download its Windows version here:

BTW if you install Git Extensions there is an option in its setup wizard to install Kdiff3.

Then setup git configs to use Kdiff as its mergetool:

$ git config --global --add merge.tool kdiff3
$ git config --global --add mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add mergetool.kdiff3.trustExitCode false

$ git config --global --add diff.guitool kdiff3
$ git config --global --add difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add difftool.kdiff3.trustExitCode false

(Remember to replace the path with the actual path of Kdiff exe file.)

Then every time you come across a merge conflict you just need to run this command:

$git mergetool

Then it opens the Kdiff3, and first tries to resolve the merge conflicts automatically. Most of the conflicts would be resolved spontaneously and you need to fix the rest manually.

Here's what Kdiff3 looks like:

Enter image description here

Then once you're done, save the file and it goes to the next file with conflict and you do the same thing again until all the conflicts are resolved.

To check if everything is merged successfully, just run the mergetool command again, you should get this result:

$git mergetool
No files need merging


Posted 2008-10-02T11:31:05.777

Reputation: 11 597


This answers is to add an alternative for those VIM users like I that prefers to do everything within the editor.


enter image description here

Tpope came up with this great plugin for VIM called fugitive. Once installed you can run :Gstatus to check the files that have conflict and :Gdiff to open Git in a 3 ways merge.

Once in the 3-ways merge, fugitive will let you get the changes of any of the branches you are merging in the following fashion:

  • :diffget //2, get changes from original (HEAD) branch:
  • :diffget //3, get changes from merging branch:

Once you are finished merging the file, type :Gwrite in the merged buffer. Vimcasts released a great video explaining in detail this steps.

Vicente Adolfo Bolea Sánchez

Posted 2008-10-02T11:31:05.777

Reputation: 631


git fetch
git checkout your branch
git rebase master

In this step you will try to fix the conflict using your prefer IDE

You can follow this link to check ho to fix the conflict in the file

git add
git rebase --continue
git commit --amend
git push origin HEAD:refs/drafts/master (push like a drafts)

Now every thing is fine and you will find your commit in gerrit

I hope that this will help every one concerning this issue.


Posted 2008-10-02T11:31:05.777

Reputation: 159


Try Visual Studio Code for editing if you aren't already. What it does is after you try merging(and land up in merge conflicts).VS code automatically detects the merge conflicts.

It can help you very well by showing what are the changes made to the original one and should you accept incoming or

current change(meaning original one before merging)'?.

It helped for me and it can work for you too !

PS: It will work only if you've configured git with with your code and Visual Studio Code.

Kailash Bhalaki

Posted 2008-10-02T11:31:05.777

Reputation: 246


For those who are using Visual Studio (2015 in my case)

  1. Close your project in VS. Especially in big projects VS tends to freak out when merging using the UI.

  2. Do the merge in command prompt.

    git checkout target_branch

    git merge source_branch

  3. Then open the project in VS and go to Team Explorer -> Branch. Now there is a message that says Merge is pending and conflicting files are listed right below the message.

  4. Click the conflicting file and you will have the option to Merge, Compare, Take Source, Take Target. The merge tool in VS is very easy to use.


Posted 2008-10-02T11:31:05.777

Reputation: 955


If you are using intelliJ as IDE Try to merge parent to your branch by

git checkout <localbranch>
git merge origin/<remotebranch>

It will show all conflicts like this

A_MBPro:test anu$ git merge origin/ Auto-merging src/test/java/com/.../ CONFLICT (content): Merge conflict in src/test/java/com/.../

Now note that the file is shown in red in intelliJ Also git status will show

Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified:   src/test/java/com/.../

Open the file in intelliJ, it will have sections with

  <<<<<<< HEAD
    public void testMethod() {
    public void testMethod() { ...
    >>>>>>> origin/<remotebranch>

where HEAD is changes on your local branch and origin/ is changes from the remote branch. Here keep the stuff that you need and remove the stuff you don't need.After that the normal steps should do. That is

   git add
   git commit -m "commit message"
   git push


Posted 2008-10-02T11:31:05.777

Reputation: 446


I follow the below process.

The process to fix merge conflict:

  1. First, pull the latest from the destination branch to which you want to merge git pull origin develop

  2. As you get the latest from the destination, now resolve the conflict manually in IDE by deleting those extra characters.

  3. Do a git add to add these edited files to the git queue so that it can be commit and push to the same branch you are working on.

  4. As git add is done, do a git commit to commit the changes.

  5. Now push the changes to your working branch by git push origin HEAD

This is it and you will see it resolved in your pull request if you are using Bitbucket or GitHub.

Aniruddha Das

Posted 2008-10-02T11:31:05.777

Reputation: 5 119


  1. Create a new feature branch from target branch
  2. Patch in the changes from conflicting feature branch
  3. Resolve conflicts in diff tool as you apply patch
  4. Commit/review clean pull request
  5. Delete the branch that has a conflict.

This has always been faster and easier for me than using Git. It is especially beneficial if changes mess up a pull request and your IDE doesn't handle Git merges very well.


Posted 2008-10-02T11:31:05.777

Reputation: 161


A safer way to resolve conflicts is to use git-mediate (the common solutions suggested here are quite error prone imho).

See this post for a quick intro on how to use it.


Posted 2008-10-02T11:31:05.777

Reputation: 15 300


git checkout branch1

git fetch origin

git rebase -p origin/mainbranch

If there are merge conflicts, fix them. Then, continue the rebase process by running: git rebase –-continue

after the fixing you can commit and push your local branch to remote branch

git push origin branch1

Vinayak Shedgeri

Posted 2008-10-02T11:31:05.777

Reputation: 649


I have Beyond Compare installed, so when using git mergetool it fires up BC which has a very nice GUI way of performing any merge.


Posted 2008-10-02T11:31:05.777

Reputation: 152


If you do not use a tool to merge, first copy your code outside:

- `checkout master`
- `git pull` / get new commit
- `git checkout` to your branch
- `git rebase master`

It resolve conflict and you can copy your code.

trai bui

Posted 2008-10-02T11:31:05.777

Reputation: 414

6Does that sequence always help? Please explain what and how it does and how that code answers the question. – Oleg Estekhin – 2014-06-10T08:32:10.740