Git:仅合并一次提交

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

Git: Merge in only one commit

gitbranch

提问by Ivan

Usually, I work with branches in Git, but I don't like to see hundreds of branches in my working tree (Git history). I'm wondering if there is a method in Git to "join" all commits in a branch in only one commit (ideally with a clear commit message).

通常,我在 Git 中使用分支,但我不喜欢在我的工作树(Git 历史)中看到数百个分支。我想知道 Git 中是否有一种方法可以仅在一次提交中“加入”一个分支中的所有提交(最好带有明确的提交消息)。

Something like this:

像这样的东西:

git checkout -b branch
<some work>
git commit -a -m "commit 1"
<some work>
git commit -a -m "commit 2"
<some work>
git commit -a -m "commit 3"
git checkout master
git SUPER-JOIN branch -m "super commit"

After this, only "super commit" will exist in the git log.

在此之后,git 日志中将只存在“超级提交”。

采纳答案by tonio

This can be done using git rebaseand squash, or using git merge --squash, see

这可以使用git rebase和壁球来完成,或者使用git merge --squash,见

Git merge flattening

Git合并展平

and

git: squash/fixup earlier commit

git:壁球/修复较早的提交

回答by Greg Hewgill

It sounds like you're looking for the --squashoption of git-merge:

听起来您正在寻找以下--squash选项 git-merge

git checkout master
git merge --squash branch -m "super commit"

回答by Brian White

If you are positive you want only a single commit and are fine with the branch never being marked as "merged" (perhaps because you're about to delete it with git branch -D my-squash-merged-branchand never want to see it again), use this:

如果您确定只需要一次提交并且分支永远不会被标记为“已合并”(可能是因为您将要删除它git branch -D my-squash-merged-branch并且再也不想看到它),请使用以下命令:

git checkout master
git merge --squash branch-to-merge
git commit -m "message for commit"

However, after much testing, I believe the best way to merge mostbranches is:

但是,经过多次测试,我相信合并大多数分支的最佳方法是:

git checkout master
git merge --no-ff branch-to-merge -m "message for commit"

This avoids the "fast-forward" merge that disallows specifying a -m "message"option for many merges. It doesn't actuallyprovide a single commit as originally requested but at least makes it easy to see the begin/end of the branch and so make for easy reverts and the like. A git logwill show all the individual commits that were merged...

这避免了不允许-m "message"为许多合并指定选项的“快进”合并。它实际上并不像最初请求的那样提供单个提交,但至少可以轻松查看分支的开始/结束,因此可以轻松恢复等。Agit log将显示所有合并的单个提交...

commit a6672a4c3d90c35d5f39c45f307ef6b385660196
Merge: 015f8d6 f84e029
Author: Brian White <[email protected]>
Date:   Wed Jan 15 20:47:35 2014 -0500

    merged something trivial

commit f84e02915faa02afc9a31b8c93a6e7712420687d
Author: Brian White <[email protected]>
Date:   Wed Jan 15 20:47:12 2014 -0500

    added something also trivial

commit 904d5b5ff00d691d63104a77d2e2ca484732a5fb
Author: Brian White <[email protected]>
Date:   Wed Jan 15 20:46:26 2014 -0500

    added something trivial

commit 015f8d681bdaf65725067ee8058215cedb529dd6
Author: Brian White <[email protected]>
Date:   Wed Jan 15 20:23:31 2014 -0500

    optimizations to MyThing
...

... but if you look at a graphof the log (git log --graph), you can see that git does indeed recognize it as a single merge.

...但是如果您查看日志 ( )的图表git log --graph,您会发现 git 确实将其识别为单个合并。

*   commit a6672a4c3d90c35d5f39c45f307ef6b385660196
|\  Merge: 015f8d6 f84e029
| | Author: Brian White <[email protected]>
| | Date:   Wed Jan 15 20:47:35 2014 -0500
| |
| |     merged something trivial
| |
| * commit f84e02915faa02afc9a31b8c93a6e7712420687d
| | Author: Brian White <[email protected]>
| | Date:   Wed Jan 15 20:47:12 2014 -0500
| |
| |     added something also trivial
| |
| * commit 904d5b5ff00d691d63104a77d2e2ca484732a5fb
|/  Author: Brian White <[email protected]>
|   Date:   Wed Jan 15 20:46:26 2014 -0500
|
|       added something trivial
|
* commit 015f8d681bdaf65725067ee8058215cedb529dd6
| Author: Brian White <[email protected]>
| Date:   Wed Jan 15 20:23:31 2014 -0500
|
|     optimizations to MyThing
...

If commits or other activity happens on the master branch, the graph will show the merged branch starting at the correct place and joining at the current head but of course all commits will still be shown in the log with the commits from the branch being at the top.

如果主分支上发生提交或其他活动,图表将显示从正确位置开始并在当前头部加入的合并分支,但当然所有提交仍将显示在日志中,来自分支的提交位于最佳。