How to do a non-fast-forward git merge to a branch that isn't checked out?

前端 未结 5 1794
误落风尘
误落风尘 2021-01-04 11:31

I\'m on branch a. I want to merge branch b into branch c. The merge is not a fast-forward, but it also doesn\'t require manual resolut

5条回答
  •  有刺的猬
    2021-01-04 12:15

    Additional to the very sophisticated git read-tree answer by @jthill, there is (nowadays) an IMHO easier way doing this utilising git worktree. This is the basic approach:

    $ git worktree add /tmp/wt c
    $ git -C /tmp/wt merge b
    $ git worktree remove /tmp/wt
    

    Below is a little Bash script which you can call like this:

    $ worktree-merge c b
    

    Script worktree-merge:

    #!/usr/bin/env bash
    
    log() {
        echo -n "LOG: "
        echo "$@" >&2
    }
    
    escape() {
        local string="$1"
        echo "${string//[. \/]/-}"
    }
    
    cleanup() {
        trap "" SIGINT
        log "Removing temporary worktree '$1' ..."
        git worktree remove --force "$1"
    }
    
    prepare_worktree() {
        local reference="$1"
        local worktree="/tmp/MERGE-INTO-`escape "$reference"`"
    
        log "Creating temporary worktree '$worktree' ..."
        trap "cleanup $worktree" EXIT
        git worktree add --force "$worktree" "$reference"
    }
    
    do_merge() {
        local reference="$1"
        local worktree="/tmp/MERGE-INTO-`escape "$reference"`"
        shift
    
        log "Merging ${@@Q} into ${reference@Q} ..."
        git -C "$worktree" merge "$@"
    }
    
    prepare_worktree "$1" &&
    do_merge "$@" &&
    true
    

提交回复
热议问题