Git:仅合并一次提交
通常,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您正在寻找
git-merge< 的
--squash
选项/代码>:
It sounds like you're looking for the
--squash
option ofgit-merge
:这可以使用
git rebase
和squash,或使用git merge --squash
来完成,请参阅Git 合并扁平化
和
git:squash/fixup 早期提交
This can be done using
git rebase
and squash, or usinggit merge --squash
, seeGit merge flattening
and
git: squash/fixup earlier commit
如果您确定只需要一次提交,并且分支永远不会被标记为“合并”,则可以接受(可能是因为您要使用 gitbranch -D my-squash-merged-branch 删除它) code> 并且再也不想看到它),使用这个:
但是,经过多次测试,我相信合并大多数分支的最佳方法是:
这避免了不允许指定的“快进”合并用于许多合并的
-m "message"
选项。它实际上并没有按照最初的要求提供单个提交,但至少可以轻松查看分支的开始/结束,从而轻松进行恢复等。 git log 将显示合并的所有单独提交......但是如果您查看日志的图表(
git log -- graph
),你可以看到 git 确实将其识别为单个合并。如果主分支上发生提交或其他活动,图表将显示从正确位置开始并在当前头部加入的合并分支,但当然所有提交仍将显示在日志中,来自分支的提交位于顶部。
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:However, after much testing, I believe the best way to merge most branches is:
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. Agit log
will show all the individual commits that were merged...... 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.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.
您可以添加一些琐碎的更改(例如 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