Skip to content

Instantly share code, notes, and snippets.

Last active Apr 8, 2022
What would you like to do?
Resolve conflicted areas of a file in favor of theirs or ours and leave correctly merged areas as is
# This is an automation of
# Example:
# --theirs path/to/some/file
import os
import argparse
from shutil import copyfile
import subprocess
parser = argparse.ArgumentParser(description='Resolve conflicted areas of a file during a git merge')
parser.add_argument('path', type=str, help='Path of the file for which to resolve merge conflict')
parser.add_argument('--theirs', action='store_true', help='Use their version wherever there is a conflict')
parser.add_argument('--ours', action='store_true', help='Use our version wherever there is a conflict')
args = parser.parse_args()
def run(cmd, outfilename=None):
if outfilename is not None:
with open(outfilename, "w") as outfile:, stdout=outfile)
def main(args):
if args.theirs and args.ours:
print("Error! You must chose either --theirs or --ours but not both")
elif args.theirs:
which_one = "--theirs"
elif args.ours:
which_one = "--ours"
print("Error! You must chose one of --theirs or --ours")
run(["git", "show", f":1:{args.path}"], f"{args.path}.base")
run(["git", "show", f":2:{args.path}"], f"{args.path}.ours")
run(["git", "show", f":3:{args.path}"], f"{args.path}.theirs")
run(["git", "merge-file", which_one, f"{args.path}.ours", f"{args.path}.base", f"{args.path}.theirs"])
copyfile(f"{args.path}.ours", f"{args.path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment