Git merge conflict to always take the newest file

后端 未结 4 1300
忘了有多久
忘了有多久 2020-12-09 21:04

How can I make a git conflict ALWAYS get resolved by taking the newest file (latest changed timestamp), completley without a prompt?

I am building a syncscript with

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 21:32

    I came up this little merge driver that does what I want. For my purpose it is hard coded to the "master" branch and to the "origin" remote. I do not know how to make these parts dynamic.

    #!/usr/bin/env sh
    if git merge-file -p -q "$2" "$1" "$3" > /dev/null;
            then git merge-file "$2" "$1" "$3";
            else
                    MINE=$(git log --format="%ct" --no-merges master -1);
                    THEIRS=$(git log --format="%ct" --no-merges origin/master -1);
                    if [ $MINE -gt $THEIRS ];
                            then git merge-file -q --ours "$2" "$1" "$3";
                            else git merge-file -q --theirs "$2" "$1" "$3";
                    fi
    fi
    

    In short I look for the last commit with git-log that was not a merge, formatted as UNIX timestamp, then I compare them and run a custom git-merge with eiter ours or their version.

    As a little bonus, it first makes a check to see if it is possible to merge the file without conflict. If that is possible, it merges both files.

提交回复
热议问题