Compare old and new versions of force-pushed GitHub pull request

前端 未结 4 1599
忘掉有多难
忘掉有多难 2021-02-04 00:37

Frequently, my colleagues will make some changes to an open pull request, rebase their local branch against the base branch - often squashing their changes into previous commits

4条回答
  •  野性不改
    2021-02-04 01:07

    Checkout this answer to another question which want to do something very similar to what you are trying to achieve.

    It describe your situation like this:

    newcommit -> the new pull request commit
    oldcommit -> the old pull request commit
    upstream -> the base branch commit the new pull request is based on
    

    Now do this:

    git commit-tree newcommit^{tree} -p oldcommit -p upstream -m "message"
    git show 
    

    The idea is that commit-tree will fake a merge between oldcommit and upstream producing newcommit tree and thus containing exactly the code of newcommit. It does not modify your current branch at all, it create a new headless commit and give you its ID. This means git show will list every modification as a conflict resolution, which is the exact difference between the new PR and the old one.

    To be able to do that you need to have the previous PR in your git repository somewhere (if a force push has been performed the git history has been rewritten and can't be recovered unless you have it on your pc or you have access to the server reflog). Check VonC answer for details about this.

    Assuming:

    • base branch: master
    • you have locally the old PR branch: $BRANCH_NAME
    • the new PR in a remote branch: origin/$BRANCH_NAME

    You can do like this:

    # fetch locally upstream changes (origin/$BRANCH_NAME)
    git fetch
    # produce the fake merge commit
    git commit-tree origin/$BRANCH_NAME^{tree} \
             -p $BRANCH_NAME \
             -p `git merge-base master origin/$BRANCH_NAME` \
             -m "message"
    # see "fake" conflict resolution = difference between the two PR
    git show 
    

    the git merge-base is used to find the common ancestor between two branches, in this case to find the commit on which the new PR is based on in the base branch, if you prefer you can write the commit ID directly.

提交回复
热议问题