Skip to content

Instantly share code, notes, and snippets.

@emakarov
Last active July 26, 2023 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emakarov/9924c6f57912cb36fb2509c5f90f82ae to your computer and use it in GitHub Desktop.
Save emakarov/9924c6f57912cb36fb2509c5f90f82ae 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()
@golovenok
Copy link

good!

@vryy
Copy link

vryy commented Jul 26, 2023

Thanks for posting this script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment