tags:

views:

139

answers:

2

A file couldn't be merged, how do I merge it with git?

+1  A: 

To merge files in git:

  • Analyse the output of the merge attempt: you should see a line telling you there was a CONFLICT when merging file x and that you should fix this conflict before continuing with the merge.
  • Locate and open the relevant file in a text editor (assuming it is not a binary file, that's another story).
  • Look for conflict markers in the file (search for <<<).
  • For each conflicting section, delete the code that no-longer makes sense, or manually edit things until you have a file that does make sense.
  • Back on the command line: git add filewithconfict
  • Finish the merge with a commit: git commit -m "Merged master branch"

Here's an example merge session:

Last login: Mon Nov 30 17:51:55 on ttys002
KidA% cd Desktop 
KidA% mkdir merge.git
KidA% cd merge.git 
KidA% git init
Initialized empty Git repository in /Users/jkp/Desktop/merge.git/.git/
KidA% echo "foo" > test.file
KidA% git add *
KidA% git commit -m "Initial commit"
[master (root-commit) 55f61fc] Initial commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 test.file
KidA% git branch
* master
KidA% git checkout -b topic
Switched to a new branch 'topic'
KidA% echo "bar" > test.file  
KidA% git commit -a -m "Topic commit"
[topic 6c524f3] Topic commit
 1 files changed, 1 insertions(+), 1 deletions(-)
KidA% git checkout master
Switched to branch 'master'
KidA% echo "foobar" > test.file      
KidA% git commit -a -m "Conflicting commit"  
[master a660160] Conflicting commit
 1 files changed, 1 insertions(+), 1 deletions(-)
KidA% git checkout topic
Switched to branch 'topic'
KidA% git merge master
Auto-merging test.file
CONFLICT (content): Merge conflict in test.file
Automatic merge failed; fix conflicts and then commit the result.
KidA% vim test.file

test.file before resolve:

<<<<<<< HEAD
bar
=======
foobar
>>>>>>> master

test.file after resolve:

foobar

KidA% git add test.file  
KidA% git commit -m "Merged master branch"
[topic 44c0cb6] Merged master branch
jkp
+1  A: 

The error message would be (Git Faq):

fatal: Entry 'frotz' not uptodate. Cannot merge.

It means that you have local modifications to 'frotz', which would be lost on checkout. You can give '-m' option to git checkout, which would try three-way merge.

Sometimes the solution is to commit.

also illustrated in git checkout.

-m
--merge

When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context

So the merge process would be:

git checkout -m mytopic
Auto-merging frotz

After this three-way merge, the local modifications are not registered in your index file, so git diff would show you what changes you made since the tip of the new branch.

When a merge conflict happens during switching branches with the -m option, you would see something like this:

$ git checkout -m mytopic
Auto-merging frotz
ERROR: Merge conflict in frotz
fatal: merge program failed

At this point, git diff shows the changes cleanly merged as in the previous example, as well as the changes in the conflicted files.
Edit and resolve the conflict and mark it resolved with git add as usual:

$ edit frotz
$ git add frotz
VonC