git Git合并展平

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/783731/
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-10 06:21:49  来源:igfitidea点击:

Git merge flattening

gitgit-pullgit-pushgit-merge

提问by Verhogen

If I am working in multiple branches on a single feature, I use git pull branch1 branch2 branch3to pull all the changes into my master branch. However, all the commit logs of each branch are copied as well. How do I flatten the commit log down to a single message?

如果我在单个功能的多个分支中工作,我使用git pull branch1 branch2 branch3将所有更改拉到我的主分支中。但是,每个分支的所有提交日志也会被复制。如何将提交日志扁平化为一条消息?

采纳答案by Pat Notz

You can use interactive rebaseand "squash" the commits -- see also the Git Ready Tutorial on squashing via rebase. Sorry to just dump a link on you but that's a pretty thorough tutorial. Oh, and this will also squash away your merges just fine.

您可以使用交互式rebase并“压缩”提交——另请参阅关于通过 rebase 压缩Git 就绪教程。很抱歉只是将链接转储给您,但这是一个非常全面的教程。哦,这也可以很好地消除您的合并。

回答by Jakub Nar?bski

"git merge --squash" (after "git fetch"; "git pull" is just fetch+merge, pehaps it also allows --squash option) mightbe what you want.

“git merge --squash”(在“git fetch”之后;“git pull”只是fetch+merge,pehaps它也允许--squash选项)可能是你想要的。

From git-merge(1):

git-merge(1)

--squash

Produce the working tree and index state as if a real merge happened, but do not actually make a commit or move the HEAD, nor record $GIT_DIR/MERGE_HEADto cause the next git commit command to create a merge commit. This allows you to create a single commiton top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).

- 壁球

生成工作树和索引状态,就好像真正的合并发生一样,但不实际提交或移动 HEAD,也不记录 $GIT_DIR/MERGE_HEAD导致下一个 git commit 命令创建合并提交。这允许您 在当前分支之上创建单个提交,其效果与合并另一个分支相同(如果是章鱼则更多)。

回答by samaspin

As Brian White commented the problem with git merge --squashis that it gives you no visible link, and therefore no traceability back to the branch (or the individual changes) you merged in.

正如布赖恩·怀特 (Brian White) 所评论的,问题git merge --squash在于它没有给您可见的链接,因此无法追溯到您合并的分支(或单个更改)。

Visibly (when viewed as a graph git log --graph), an important branch that has been merged back in looks no different to an experimental branch that you messed around with and would happily discard. Both are just hanging there unconnected to anything. I personally want to know that a certain branch has been merged back in so I know that work is done.

很明显(当查看图表时git log --graph),一个已经合并回来的重要分支看起来与一个你弄乱并很乐意丢弃的实验分支没有什么不同。两者都只是挂在那里,与任何东西都没有联系。我个人想知道某个分支已经合并回来,所以我知道工作已经完成。

A solution that works for me is to use a merge with the no-fastforward option.

对我有用的解决方案是使用带有 no-fastforward 选项的合并。

git merge --no-ff somebranch -m "my commit message"

This forces git to create a single commit with all the branch changes included, you can set the commit message yourself (if you want) BUT most importantly, it links the new commit back to the branch it just merged in. This visibly shows that work on that branch is complete but also allows you to trace back to see details of the individual commits from the merged branch.

这会强制 git 创建包含所有分支更改的单个提交,您可以自己设置提交消息(如果需要)但最重要的是,它将新提交链接回刚刚合并的分支。这显然表明了工作在该分支上是完整的,但也允许您回溯以查看来自合并分支的各个提交的详细信息。

Here is an example where very simple branches with one and two commits respectively have been merged back into master. I have subsequently deleted the branch tags on the merged branches, however the branch name can still be seen on the merge commit message. The branch name should summarise the changes and if you want to know the exact changes contained you can trace it back to the individual commits. This approach seems to work nicely for simple projects.

这是一个示例,其中分别具有一个和两个提交的非常简单的分支已合并回 master。我随后删除了合并分支上的分支标签,但是仍然可以在合并提交消息中看到分支名称。分支名称应总结更改,如果您想知道包含的确切更改,您可以将其追溯到各个提交。这种方法似乎适用于简单的项目。

Note : I had to manually draw one of the connectors in as it was such a dark blue it was barely visible.

注意:我必须手动绘制其中一个连接器,因为它是深蓝色的,几乎看不见。

git log output

git 日志输出