Skip to content

Instantly share code, notes, and snippets.

@eliroca
Forked from emakarov/merger.py
Created December 9, 2021 20:47
Show Gist options
  • Save eliroca/96655d6af841258537dca7062f931f38 to your computer and use it in GitHub Desktop.
Save eliroca/96655d6af841258537dca7062f931f38 to your computer and use it in GitHub Desktop.
Python script that can resolve all git conflicts in some file marked with conflicts based on chosen strategy
accept = 'ours' # ours for 'HEAD' #theirs for other's - choose strategy here for all conflicts
filename = 'enter your filename here'
fresult = '{}.{}'.format(filename, 'merged') # result will be near your file with name `original_filename.ext.merged`
with open(filename) as f:
content = f.readlines()
res = open(fresult, 'w')
to_write = True
merging = False
special_line = False
i = 0
for c in content:
i += 1
if c.startswith('<<<<<<< HEAD'):
special_line = True
merging = True
if accept == 'ours':
to_write = True
print('line {} accepting HEAD'.format(i))
else:
to_write = False
print('line {} accepting THEIRS'.format(i))
elif c.startswith('======='):
special_line = True
merging = True
if accept == 'ours':
to_write = False
else:
to_write = True
elif c.startswith('>>>>>>>'):
special_line = True
merging = False
to_write = True
else:
special_line = False
if to_write and not special_line:
res.write(c)
res.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment