git command for making one branch like another

前端 未结 9 1330
半阙折子戏
半阙折子戏 2020-11-22 11:36

I\'m trying to take a branch with changes and bring it back to be identical to the upstream it diverged from. The changes are both local and have been pushed to github, so n

9条回答
  •  再見小時候
    2020-11-22 11:51

    There's also a way with little help of plumbing command - IMHO the most straightforward. Say you want to emulate "theirs" for 2 branches case:

    head1=$(git show --pretty=format:"%H" -s foo)
    head2=$(git show --pretty=format:"%H" -s bar)
    tree=$(git show --pretty=format:"%T" -s bar)
    newhead=$(git commit-tree $tree -p $head1 -p $head2 <<<"merge commit message")
    git reset --hard $newhead
    

    This merges arbitrary number of heads (2 in the example above) using tree of one of them (bar in the example above, providing 'theirs' tree), disregarding any diff/file issues (commit-tree is low level command, so it doesn't care about those). Note that head can be just 1 (so equivalent of cherry-pick with "theirs").

    Note, that which parent head is specified first, can influence some stuff (see e.g. --first-parent of git-log command) - so keep that in mind.

    Instead of git-show, anything else capable of outputting tree and commit hashes can be used - whatever one's is used to parsing (cat-file, rev-list, ...). You can follow everything with git commit --amend to interactively beautify commit message.

提交回复
热议问题