How do I fast-forward other tracking branches in git?

后端 未结 4 645
星月不相逢
星月不相逢 2020-12-15 15:58

We are working with the model of a single remote repository that all of us use. We branch for new features and reintegrate into a trunk branch. Our workflow is that we shoul

相关标签:
4条回答
  • 2020-12-15 16:40

    Try the following:

    $ git pull trunk branch
    
    0 讨论(0)
  • 2020-12-15 16:50

    Do you really need to update a local trunk branch?

    Just fetch origin/trunk and merge it directly in the branch you are working on.
    And that is Mark Longair's advice: git: fetch and merge, don’t pull.


    Oliver mentions in his answer (upvoted):

    git fetch upstream trunk:trunk
    

    As he comments:

    you can skip the merge and simply fast-forward, using a one-liner.

    It does fast-forward the local branch trunk to the remote branch's HEAD.

    See more at "git: update a local branch without checking it out?"

    0 讨论(0)
  • 2020-12-15 16:52

    I think the easiest way to avoid the git checkout trunk, git pull, git checkout branch cycle is to use this answer:

    git fetch upstream trunk:trunk
    

    This does exactly what you want - fast-forward your local branch trunk to the remote branch's HEAD.

    0 讨论(0)
  • 2020-12-15 16:54

    I agree with VonC's answer, but thought it might be fun to answer the question "How do you fast-forward master if you're on another branch?" anyway. I don't think there's a way to do this just using porcelain commands, but this script does the job (obviously you'd want to change master to trunk in your case):

    #!/bin/sh
    
    BRANCH=master
    NEWER=origin/master
    
    if [ x"$(git symbolic-ref HEAD)" = x"refs/heads/$BRANCH" ]
    then
        echo "This doesn't make sense if you're already on the branch '$BRANCH'"
        echo "Just run: git merge $NEWER"
        exit 1
    fi
    
    BRANCH_HASH=$(git rev-parse $BRANCH)
    NEWER_HASH=$(git rev-parse $NEWER)
    MERGE_BASE=$(git merge-base $BRANCH_HASH $NEWER_HASH)
    
    if [ "$MERGE_BASE" = "$BRANCH_HASH" ]
    then
        git update-ref "refs/heads/$BRANCH" "$NEWER_HASH" "$BRANCH_HASH"
    else
        echo "$BRANCH can't be fast-forwarded to $NEWER"
        exit 1
    fi
    

    Update 2012-10-20: this script was just a quick example; for a script that does the same but for merging any arbitrary commit-ish into a local branch (so long as the merge would be a fast-forward), see Jefromi's answer to a similar question.

    0 讨论(0)
提交回复
热议问题