比较强制推送的GitHub Pull请求的旧版本和新版本 [英] Compare old and new versions of force-pushed GitHub pull request

查看:122
本文介绍了比较强制推送的GitHub Pull请求的旧版本和新版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我的同事会对打开的拉取请求进行一些更改,将本地分支与基础分支重新建立基础-经常将更改也压入以前的提交中,然后强制推入.

如何查看PR的旧版本和PR的新版本之间有什么变化?

我想我可以在首次提出PR时进行git pullgit checkout $BRANCH_NAME的操作,然后在PR更新后进行git fetch然后是git diff $BRANCH_NAME..origin/$BRANCH_NAME的操作-但这也将显示已经引入到PR中的更改.基本分支(通常是主分支),并通过重新部署进入PR.是否可以消除这些噪音并仅显示PR本身发生了什么变化?

解决方案

结帐回答另一个想要解决的问题做一些与您要达到的目标非常相似的事情.

它像这样描述您的情况:

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

现在执行此操作:

git commit-tree newcommit^{tree} -p oldcommit -p upstream -m "message"
git show <commit id returned by previous command>

这个想法是,提交树将伪造oldcommitupstream之间的合并,从而生成newcommit树,从而精确地包含newcommit的代码. 它根本不会修改您当前的分支,它会创建一个新的无头提交并给您ID. 这意味着git show会将所有修改都列为冲突解决方案,这是新PR与旧PR之间的确切区别.

要执行此操作,您需要将以前的PR保存在git存储库中的某个位置(如果执行了强制推送,则git历史记录已被重写并且无法恢复,除非您将其保存在PC上或有权访问服务器reflog).查看 VonC 答案以获取有关此信息的详细信息.

假设:

  • 基本分支:master
  • 您在本地有旧的PR分支:$BRANCH_NAME
  • 远程分支中的新PR:origin/$BRANCH_NAME

您可以这样做:

# 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 <commit id returned by previous command>

git merge-base用于查找两个分支之间的公共祖先,在这种情况下,可以在基础分支中查找新PR所基于的提交,如果您愿意,可以直接编写提交ID. /p>

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 as well - and force-push.

How can I see what changed between the old version of the PR and the new version of the PR?

I guess I could do a git pull and git checkout $BRANCH_NAME when the PR was first raised, then git fetch and then git diff $BRANCH_NAME..origin/$BRANCH_NAME after the PR was updated - but that will also show changes that have been introduced into the base branch (typically master) and brought into the PR via a rebase. Is it possible to remove that noise and just show what has changed in the PR itself?

解决方案

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 <commit id returned by previous command>

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 <commit id returned by previous command>

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.

这篇关于比较强制推送的GitHub Pull请求的旧版本和新版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆