BZR Logo This page walks through a simple bzr example where you need to merge with upstream changes and handle a merge conflict.

For the purpose of this demo we'll use a junk repository I've put on launchpad. This repository has 3 commits. However, as you can see from the branch command below, we are initially only getting the first two revisions. This will let us create a merge conflict to resolve:

$ cd /tmp
$ bzr branch -r2 lp:~doanac/+junk/merge-example
$ cd merge-example

At this point you have a simple repository with two files:

$ echo "= FILE 1:"; cat file1.txt; echo "= FILE2:";  cat file2.txt 
= FILE 1:
this is line one of file 1
this is line two of file 1

line 5
= FILE2:
this is line one of file 2
this is line two of file 2

line 5

Now lets pretend we would like to spell out the numbers in file2.txt:

$ sed -i -e 's/5/five/g' file2.txt 
$ sed -i -e 's/2/two/g' file2.txt 

# You can run "bzr diff" here to see the changes for yourself

$ bzr commit -m "spell out numbers" file2.txt 
Committing to: /tmp/merge-example/                                             
modified file2.txt
Committed revision 3.

At this point we'll pretend time has passed and we want to sync up with the latest changes on launchpad. We do this by doing a merge:

$ bzr merge
Merging from remembered parent location bzr+ssh://
 M  file2.txt                                                                  
Text conflict in file2.txt
1 conflicts encountered.                                                       
$ cat file2.txt
<<<<<<< TREE
this is line one of file two
this is line two of file two

line five
This is line one of file 2
This is line two of file 2

line 5

# NOTE: bzr leaves different versions of the file to help resolve the conflict
$ ls file2.txt*
file2.txt  file2.txt.BASE  file2.txt.OTHER  file2.txt.THIS

As you can see here, bzr detected a merge conflict. It indicates the conflict with "<<<<<<< TREE" and ">>>>>>> MERGE-SOURCE". In this case we have a fairly simple conflict: Revision 3 on launchpad capitalized the "t's". We can fix this with a simple sed command and then complete the merge:

$ sed  -e 's/^t/T/' file2.txt.THIS > file2.txt
$ bzr resolved file2.txt
1 conflict(s) resolved, 0 remaining
$ bzr commit -m "merged with lp"
Committing to: /tmp/poop/                                                      
modified file2.txt
Committed revision 4.

We now have updated our branch with the latest version of code. You can visualize this from the command line with:

$ bzr log --line
4: Andy Doan 2011-02-17 [merge] merged with lp
3: Andy Doan 2011-02-17 spell out numbers
2: Andy Doan 2011-02-17 add file2.txt
1: Andy Doan 2011-02-17 initial commit file1.txt

An even better way to view this is graphically with bzr visualize:



Resources/HowTo/BZR-MergeConflict (last modified 2011-04-15 14:58:38)