Search code examples
gitconflictrebase

git - reorder commit on branch as early as possible without a conflict


How would I go about moving a commit up a branch to be as early as possible without any conflicts (without much manual work, eg rebase -i)?

Eg

A-B-C-D-X

should become

A-B-X-C-D

if swapping X with C and D has no conflicts but swapping X with B would result in a conflict.

Thanks.


Solution

  • Well, this pretty much works but needs some cleanup.

    After working with it for a while, I've bumped into another problem which I've posted here.

    #!/bin/sh -e
    
    # todo: integrate with git
    GIT_DIR=./.git/
    
    commitid=$1
    
    if [ "$1" = "" ]; then
       echo usage: $0 commitid
       exit 1
    fi
    
    tmpdir="$GIT_DIR/bubble-work"
    /bin/rm -rf "$tmpdir"
    mkdir "$tmpdir"
    
    # checkout commit with detached head
    git checkout -q $commitid^0 || die "could not detach HEAD"
    
    while [ 1 = 1 ]; do
    
    # todo pipe output to avoid temp files
    # see git-rebase.sh
    
    patchfile=`git format-patch -k --full-index --src-prefix=a/ --dst-prefix=b/ --no-renames -o "$tmpdir" HEAD~1`
    echo patch = $patchfile
    
    git checkout -q HEAD~2
    git am --rebasing "$patchfile" || die "git am failed"
    /bin/rm -f "$patchfile"
    
    echo looping
    done
    
    
    
    /bin/rm -rf "$tmpdir"