Git:仅合并一次提交

发布于 2024-10-10 03:31:31 字数 394 浏览 3 评论 0原文

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

像这样:

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"

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

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).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

层林尽染 2024-10-17 03:31:31

听起来您正在寻找 git-merge< 的 --squash 选项/代码>

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

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

git checkout master
git merge --squash branch -m "super commit"
时光无声 2024-10-17 03:31:31

这可以使用git rebase和squash,或使用git merge --squash来完成,请参阅

Git 合并扁平化

git:squash/fixup 早期提交

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

Git merge flattening

and

git: squash/fixup earlier commit

吃颗糖壮壮胆 2024-10-17 03:31:31

如果您确定只需要一次提交,并且分支永远不会被标记为“合并”,则可以接受(可能是因为您要使用 gitbranch -D my-squash-merged-branch 删除它) code> 并且再也不想看到它),使用这个:

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

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

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

这避免了不允许指定的“快进”合并用于许多合并的 -m "message" 选项。它实际上并没有按照最初的要求提供单个提交,但至少可以轻松查看分支的开始/结束,从而轻松进行恢复等。 git 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
...

但是如果您查看日志的图表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 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-branch and never want to see it again), use this:

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 most branches 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 actually provide 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 log will show all the individual commits that were merged...

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 graph of the log (git log --graph), you can see that git does indeed recognize it as a single merge.

*   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.

不交电费瞎发啥光 2024-10-17 03:31:31

您可以添加一些琐碎的更改(例如 README.md 文件中的空行),使用有意义的描述提交它(例如“要合并到此超级提交中的 FEATURE 分支”),然后使用 --squash 选项合并 FEATURE 分支。

git checkout master

git add README.md

git commit -m "FEATURE 分支将合并到这个超级提交中"

git merge --squash FEATURE

You can add some trivial change (for example empty line in README.md file), commit it with meaningful description (for example "FEATURE branch to be merged in this super commit"), then merge FEATURE branch with --squash option.

git checkout master

git add README.md

git commit -m "FEATURE branch to be merged in this super commit"

git merge --squash FEATURE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文