Git分支-HEAD分支上的拉取请求也需要先前的分支提交 [英] Git branching - pull request on HEAD branch takes also prior branch commits

查看:115
本文介绍了Git分支-HEAD分支上的拉取请求也需要先前的分支提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自IBM RTC,所以我需要习惯Git。

我创建了一个存储库,在master分支上进行了两次提交,并打开了请求请求。

I come from IBM RTC, so I need to get used to Git.
I have forked a repository, done a couple commits on my master branch and opened a pull request.

Pull request:
original-repository/master <- my-repository/master
  commit-1
  commit-2

然后我创建了一个新分支并进行了更改。我从新分支开始打开了另一个拉取请求,这就是我的发现。

I then created a new branch and pushed a change. I opened another pull request starting from the new branch, and that's what I find.

Pull request:
original-repository/master <- my-repository/newbranch
  commit-1
  commit-2
  commit-3

如果我想只用 commit-3 发出拉动请求怎么办?

What if I want to have a pull request with only commit-3?

推荐答案

我真的很惊讶它没有重复... 结果我已经回答了几乎相同的问题。在提交方面,它是重复项(即,有一个分支包含2个不必要的提交)。但是,问题的表达方式却完全不同(例如,另一个问题未提及拉取请求),如果不是我的回答,我也找不到。

I'm really surprised it's not a duplicate... It turns out I already answered almost the same question. Commit-wise it is a duplicate (i.e. there is a branch with 2 unnecessary commits). However, the problem is stated in quite a different way (e.g. the other question doesn't mention pull requests), and I wouldn't find it if it wasn't my answer there.

使用 git rebase- -onto

在您的本地存储库中:

git checkout newbranch
git rebase master --onto origin/master
git push origin +newbranch  # May look slightly different for you; The point is, you need to force push (the + does that).

NB,第二行, master 可以由 HEAD〜或其他对 commit-2 的引用代替。

NB, in the second line, master could be replaced by HEAD~ or other reference to commit-2.

简短说明:git在指向它的提交(在本例中为 master )和当前提交之间进行提交,并在提交处逐一复制它们。定义的点是-到。如果某些提交无法自动应用,则该过程将被暂停,您必须手动解决冲突。

Short explanation: git takes commits between the one pointed to it (in this case master) and current commit, and replicates them one-by-one at the point defined be --onto. If some commits cannot be applied automatically, the process is suspended and you have to resolve conflicts manually.

注意事项:除非您确定安全,否则请勿强行推入。如果您推送到自己的fork资源库,并且可以假设没有其他人使用它,那么您可能会很好(即使在技术上是公开的)。这是您的私人分支机构,有时可能足以表示它很好。但是重要的是要知道与删除历史记录有关的风险(或尝试尝试删除它们)。

Caveat: do not force-push unless you are sure it's safe. If you push to your own fork repository and can assume no one else uses it, you are probably fine (even if it's technically public). It is your private branch, which sometimes may be enough to mean it's fine. But it's important to know risks related to deleting the history (or attempting to).

替代方法(樱桃选择):我对另一个问题的回答详细介绍了一些替代方案。那里的情况基本相同,分支F1包含不必要的提交。

Alternatives (cherry-pick): my answer to the other question details some alternatives. The situation there is essentially the same, branch F1 contains unnecessary commits.

这篇关于Git分支-HEAD分支上的拉取请求也需要先前的分支提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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