git 在 GitHub 上执行拉取请求时避免不需要的合并提交和其他提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5968964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:27:36  来源:igfitidea点击:

Avoid unwanted merge commits and other commits when doing pull request on GitHub

gitgithub

提问by Ivan Z. Siu

I forked a project on Github.

我在 Github 上 fork 了一个项目。

Let the remote upstream be upstreamand my remote repository be origin. My local masterbranch is set to track the remote masterbranch. Then I added some stuff in local master, and I merged with the upstream every now and then.

让远程上游为upstream,我的远程存储库为origin. 我的本地master分支设置为跟踪远程master分支。然后我在 local 中添加了一些东西master,并时不时地与上游合并。

Not until today when I want to issue a pull request did I find the problem: the pull request consists those merge commits, and those unwanted commitsthat I did previously without care. However what I want is just to submit the last commit I did, which should be pulled as a single commit. What can I do to rescue this?

直到今天,当我想发出拉取请求时,我才发现问题:拉取请求包含那些合并提交,以及我之前不小心所做的那些不需要的提交。但是,我想要的只是提交我所做的最后一次提交,应该将其作为单个提交进行提取。我能做些什么来拯救这个?

回答by Arrowmaster

Instead of merging you want to rebase. You can do this manually, or automatically when pulling.

而不是合并,你想变基。您可以手动执行此操作,也可以在拉动时自动执行此操作。

git pull --rebase upstream master
git push --force origin master

Once you've started doing merges though this will get hard to do, you'll need to reset the branch back to before you did a merge commit.

一旦您开始进行合并,虽然这将很难做到,您需要将分支重置回进行合并提交之前。

回答by csd

If I understand your question, you want to get rid of the intermediate/throwaway commits that you did in your branch. Try something like this:

如果我理解你的问题,你想摆脱你在你的分支中所做的中间/一次性提交。尝试这样的事情:

git checkout -b for-upstream remotes/origin/master (create a new branch from the upstream origin)
git cherry-pick <sha-of-the-one-commit-you-want-to-submit> (fix any conflicts if necessary)

this should give you a local "for-upstream" branch which contains just the upstream master + your 1 commit. You can then submit that branch for pull request

这应该为您提供一个本地“for-upstream”分支,其中仅包含上游主服务器 + 您的 1 次提交。然后您可以提交该分支以进行拉取请求

回答by wewals

On Github, You can't create a pull request for a single specific checkin on a branch that has multiple checkins separating it from upstream.

在 Github 上,您无法为分支上的单个特定签入创建拉取请求,该分支具有多个签入将其与上游分开。

Create a branch specifically for each pull request you intend to make. This allows you to continue working without fear of polluting a pull request.

专门为您打算发出的每个拉取请求创建一个分支。这使您可以继续工作而不必担心污染拉取请求。

回答by Jay Sullivan

Would this work: Create a separate branch with just the commit you want and issue a pull request on that branch.

这是否可行:仅使用您想要的提交创建一个单独的分支,然后在该分支上发出拉取请求。

回答by Ivan Akcheurov

This looks like an answer to your question (section "Update 2011-04-15" of the topic):

这看起来像是对您问题的回答(该主题的“更新 2011-04-15”部分):

Git workflow and rebase vs merge questions

Git 工作流程和变基与合并问题

Micah describes the technique of squash mergeswhich let you merge changes from your feature branch as a single commit to the master branch.

Micah 描述了压缩合并技术,该技术使您可以将来自功能分支的更改作为单个提交合并到主分支。