Git:根据拉取请求中的新目录将拉取请求拆分为较小的 PR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30768148/
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
Git: split pull request into smaller PR's based upon the new directories in the pull request
提问by user3768149
I have a monolith of a feature branch. Instead of one massive PR into master I would like to split it up into 3 prs.
我有一个功能分支的单体。我想将其拆分为 3 个 prs,而不是将一个庞大的 PR 转换为 master。
Ideally I want to pull out some standalone code from the feature branch into a PR by itself. This code is in a new directory and isn't called into yet so it would be a relatively safe PR. However instead of just copying the directory and creating a single commit and PR I would like to retain all the commit history of the changes for the new PR.
理想情况下,我想从功能分支中提取一些独立代码到 PR 中。此代码位于新目录中,尚未调用,因此它是一个相对安全的 PR。但是,我想保留新 PR 更改的所有提交历史记录,而不是仅仅复制目录并创建单个提交和 PR。
Is this possible using Git? I've looked into filter-branch but it seems like that is for splitting a repo into two, not for splitting a diff of changes into two (if that makes sense).
这可以使用 Git 吗?我已经研究过 filter-branch,但似乎是为了将一个 repo 一分为二,而不是将不同的更改一分为二(如果有道理的话)。
回答by VonC
is there a way to pull files out of a branch including their commit history into a new branch?
有没有办法将文件从分支中提取出来,包括它们的提交历史记录到新分支中?
You pull a commit, meaning a whole repo into a branch, not just some files.
你拉一个commit,意味着整个 repo 到一个分支,而不仅仅是一些文件。
One good option would then be to reset the files you don't want to their content pre-branch
一个不错的选择是将您不想要的文件重置为分支前的内容
--x--x--x (master)
\
y--y--y (branch B1 with mixed work)
\
z (new branch B2, but with some files reset to master)
B2
starts from B1
and include the files you want, with their history.
But B2
also includes other files from B1
that you would like to be as master
.
B2
从B1
并包含您想要的文件及其历史记录。
但B2
还包括B1
您希望作为master
.
You can do (in B2
) a git reset --soft master
: a git status
would tell you all the changes you need to do in order for your index to reflect master. Simply don't add changes that involves the files you want to keep.
And don't commit. You just want to stage changes back to master
for somefiles.
你可以做 (in B2
) a git reset --soft master
: agit status
会告诉你你需要做的所有更改,以便你的索引反映主人。只需不要添加涉及您要保留的文件的更改。
不要承诺。您只想master
为某些文件暂存更改。
(See also "Practical uses of git reset --soft
?")
(另见“ 的实际用途git reset --soft
?”)
Once that is done, a git reset --soft B2
moves HEAD back to B2
(but with an index recording all the changes and deletions necessary to reflect master for the right files).
完成后,git reset --soft B2
将 HEAD 移回B2
(但使用索引记录反映正确文件的母版所需的所有更改和删除)。
You can commit now, with the other files reverted to master
and the files you want untouched, identical to B1
, with their history intact.
您现在可以提交,其他文件将恢复为master
原状,您希望保持原状,与 相同B1
,其历史记录完好无损。
回答by PSkocik
Github pull requests always concern a branch. If you want to "split a pull request", make sure it is split well-enough into commits, and then make a branch for each pull request you want to make and make sure each such feature branch has all the relevant commits.
Github 拉取请求总是涉及一个分支。如果您想“拆分拉取请求”,请确保将其充分拆分为提交,然后为您要创建的每个拉取请求创建一个分支,并确保每个此类功能分支都具有所有相关的提交。
It looks like a good strategy for you might be to create 3 branches next to your main branch, cherry-pick the relevant commits to each branch, delete them on main, and have main merge with each of the three branches. Then you could have a PR for each branch and shared history too.
对您来说,一个不错的策略可能是在主分支旁边创建 3 个分支,挑选每个分支的相关提交,在主分支上删除它们,然后将主分支与三个分支中的每个分支合并。然后你可以为每个分支创建一个 PR 并共享历史记录。
回答by Jesse Smith
This is my preferred solution, but in this case you will lose your git history. If you really want to keep it, you can simply copy-paste it into the commit message of each individual PR.
这是我首选的解决方案,但在这种情况下,您将丢失 git 历史记录。如果你真的想保留它,你可以简单地将它复制粘贴到每个 PR 的提交消息中。
I normally go through the list of changed files and create a .patch file for each set of changes that belong in a separate PR. In this case, this would be a very simple way to separate changes by directory.
我通常会查看已更改文件的列表,并为属于单独 PR 的每组更改创建一个 .patch 文件。在这种情况下,这将是一种按目录分隔更改的非常简单的方法。
For example, the following would put changes to the README.md and users_controllers.rb files, and any files within the views directory into a match file, which could then be applied on the new branch. Then a new PR can be made.
例如,以下内容会将 README.md 和 users_controllers.rb 文件的更改以及 views 目录中的任何文件放入匹配文件中,然后可以将其应用于新分支。然后可以进行新的 PR。
First checkout the branch with the too-big PR, then
首先用太大的 PR 签出分支,然后
git diff master head -- README.md app/controller/users_controller.rb app/views > first_group_of_changes.patch
git diff master head -- README.md app/controller/users_controller.rb app/views > first_group_of_changes.patch