我在一个有两个分支a和B的项目上工作。我通常在分支a上工作,并合并分支B中的内容。对于合并,我通常会这样做:

git merge origin/branchB

但是,我也希望保留分支B的本地副本,因为我可能会偶尔检查分支,而不必首先与分支a合并。为此,我会:

git checkout branchB
git pull
git checkout branchA

有没有一种方法可以在一个命令中完成上述操作,而不必来回切换分支?我应该使用gitupdate-ref吗?怎样


当前回答

只是拉主人而不检查我用的主人

git获取源主机:主机

其他回答

您可以克隆回购并在新回购中进行合并。在同一个文件系统上,这将硬链接而不是复制大部分数据。最后将结果拉入原始回购。

正如Amber所说,快进合并是唯一可以想象得到的情况。任何其他合并都可能需要经过整个三方合并,应用补丁,解决冲突-这意味着需要有文件。

我身边正好有一个脚本:在不接触工作树的情况下进行快速向前合并(除非您要合并到HEAD)。它有点长,因为它至少有点健壮——它检查以确保合并是快速前进的,然后在不检查分支的情况下执行它,但会产生与之前相同的结果——您可以看到更改的diff-stat摘要,并且reflog中的条目与快速前进的合并完全相同,而不是使用branch-f时得到的“重置”结果。如果将其命名为gitmergeff并将其放到bin目录中,则可以将其作为git命令调用:gitmergeff。

#!/bin/bash

_usage() {
    echo "Usage: git merge-ff <branch> <committish-to-merge>" 1>&2
    exit 1
}

_merge_ff() {
    branch="$1"
    commit="$2"

    branch_orig_hash="$(git show-ref -s --verify refs/heads/$branch 2> /dev/null)"
    if [ $? -ne 0 ]; then
        echo "Error: unknown branch $branch" 1>&2
        _usage
    fi

    commit_orig_hash="$(git rev-parse --verify $commit 2> /dev/null)"
    if [ $? -ne 0 ]; then
        echo "Error: unknown revision $commit" 1>&2
        _usage
    fi

    if [ "$(git symbolic-ref HEAD)" = "refs/heads/$branch" ]; then
        git merge $quiet --ff-only "$commit"
    else
        if [ "$(git merge-base $branch_orig_hash $commit_orig_hash)" != "$branch_orig_hash" ]; then
            echo "Error: merging $commit into $branch would not be a fast-forward" 1>&2
            exit 1
        fi
        echo "Updating ${branch_orig_hash:0:7}..${commit_orig_hash:0:7}"
        if git update-ref -m "merge $commit: Fast forward" "refs/heads/$branch" "$commit_orig_hash" "$branch_orig_hash"; then
            if [ -z $quiet ]; then
                echo "Fast forward"
                git diff --stat "$branch@{1}" "$branch"
            fi
        else
            echo "Error: fast forward using update-ref failed" 1>&2
        fi
    fi
}

while getopts "q" opt; do
    case $opt in
        q ) quiet="-q";;
        * ) ;;
    esac
done
shift $((OPTIND-1))

case $# in
    2 ) _merge_ff "$1" "$2";;
    * ) _usage
esac

注:如果有人看到该脚本有任何问题,请评论!这是一份写就忘的工作,但我很乐意改进它。

git worktree add [-f] [--detach] [--checkout] [--lock] [-b <new-branch>] <path> [<commit-ish>]

你可以尝试git worktree让两个分支并排打开,这听起来可能是你想要的,但与我在这里看到的其他一些答案非常不同。

通过这种方式,您可以在同一个git repo中有两个单独的分支进行跟踪,因此您只需获取一次即可在两个工作树中获取更新(而不必分别获取两次git clone和git pull)

Worktree将为您的代码创建一个新的工作目录,您可以在其中同时签出不同的分支,而不是在原地交换分支。

当您想要删除它时,可以使用

git worktree remove [-f] <worktree>

如果您想保持与要合并的分支相同的树(即,不是真正的“合并”),可以这样做。

# Check if you can fast-forward
if git merge-base --is-ancestor a b; then
    git update-ref refs/heads/a refs/heads/b
    exit
fi

# Else, create a "merge" commit
commit="$(git commit-tree -p a -p b -m "merge b into a" "$(git show -s --pretty=format:%T b)")"
# And update the branch to point to that commit
git update-ref refs/heads/a "$commit"

只是拉主人而不检查我用的主人

git获取源主机:主机