Have you ever read the output of a diff command? It’s harry. As humans we aren’t supposed to be able to grok this. It was intended for computers to create and for computers to consume.
Inevitably, working on a team where you share a source code repository will result in a file conflict. This happens whenever the code repository can’t easily merge the changes you are trying to commit with what’s already been checked in.
I'm going to create a simple Python script to illustrate how this works.
connect.py
Connects to Google and does a search for “merge tools mac os x”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #!/usr/bin/env python
import httplib, urllib
import unittest
from test import test_support
class GoogleSearchTest(unittest.TestCase):
def setUp(self):
self.conn = httplib.HTTPConnection('www.google.com')
def test_connect(self):
self.conn.request('GET', '/search?q=merge+tools+mac+os+x', None, {})
response = self.conn.getresponse()
data = response.read()
self.failUnlessEqual(200, response.status)
def test_main():
test_support.run_unittest(GoogleSearchTest)
if __name__ == '__main__':
test_main()
|
Now let’s pretend that both I and someone else edited the file, upon checking in I get a conflict. It creates two files to let me resolve the conflict.
$ vim -d connect.py.merge-left.r5 connect.py.merge-right.r6
It helps to turn off syntax highlighting
You probably have Vim configured to use syntax highlighting. This causes some conflict with the standard highlighting that diff mode gives you. Let's turn that off. Type this once you have Vim up.
:syntax off
That’s better.
Merging the changes
With the two windows up, you can move between them by using CTRL+W h to go left and CTRL+W l to go right.
Once you get your cursor on a section you want to grab from the other side you need to get it. Do this with dg.
If you want to copy what you have in the current window to the other side you need to put. Do this with do.
Vim will adjust the folding of the file, so that as your changes begin to merge you’ll see less lines. When all your changes are synced up you’ll see something like this.