git 在分支上展平提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11724666/
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
Flatten commits on a branch
提问by Coffee And Keyboard
Is it possible to condense the commits on a branch into a single commit prior to merging with the main? I thought this would be a fairly common scenario, but maybe I am not using the right search terms.
在与主合并之前,是否可以将分支上的提交压缩为单个提交?我认为这将是一个相当普遍的情况,但也许我没有使用正确的搜索词。
I'll explain the scenario in more detail. Often I would want to make many local commits while working on a change in a branch to ensure I have a comprehensive history of changes. But once through with the changes in the branch, when I am merging to main, I would like to reduce the commits on the branch to a single one and then merge it to main. I do understand that commits are inexpensive in Git, but in some situations, I might just prefer to do this.
我会更详细地解释这个场景。通常,我希望在分支中进行更改时进行许多本地提交,以确保我拥有完整的更改历史记录。但是一旦完成了分支中的更改,当我合并到主时,我想将分支上的提交减少到一个,然后将其合并到主。我知道在 Git 中提交并不昂贵,但在某些情况下,我可能更喜欢这样做。
* merge to main
|\
* | commit 2 on main
* | commit 1 on main
| * commit 2 on branch
| * commit 1 on branch
|/
* branch from main
to be made to look like
看起来像
* merge to main
|\
* | commit 2 on main
* | commit 1 on main
| * commit on branch (branch commits flattened to one)
|/
* branch from main
I'm a novice when it comes to git. If I have erred in the use of terms, I do apologise.
我是 git 的新手。如果我在使用术语时犯了错误,我深表歉意。
采纳答案by Fred Foo
Use interactive rebasing. Find the commit where your branch diverged from master (may using git merge-base
; let's call the commit <diverge>
), then
使用交互式变基。找到你的分支与 master 不同的提交(可以使用git merge-base
;让我们称之为提交<diverge>
),然后
git rebase -i <diverge>
and an editor will pop up, allowing you to rewrite history interactively. You'll want to squash commits.
并且会弹出一个编辑器,允许您交互式地改写历史。你会想要压缩提交。
回答by thameera
I would recommend learning to use the interactive rebase, but in case it seems too complicated to you, you can simply use
我建议学习使用交互式 rebase,但如果你觉得它太复杂,你可以简单地使用
git reset --soft <diverging-commit>
git reset --soft <diverging-commit>
to undo all commits up to the diverging point without changing the index, and
在不更改索引的情况下撤消直到发散点的所有提交,以及
git commit -s
git commit -s
to make a single commit of all the changes.
对所有更改进行一次提交。
回答by Trevor Robinson
You can also use git merge --squash <branch>
to merge a branch without performing any commits:
您还可以git merge --squash <branch>
在不执行任何提交的情况下使用合并分支:
$ git checkout master
$ git merge --squash mybranch
...
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested
$ git commit
(Update: Removed link to article advocating avoidance of history rewriting. I now think squashing small feature branches prior to review is a good practice.)
(更新:删除了提倡避免历史重写的文章链接。我现在认为在之前压缩小功能分支是一个很好的做法。)
回答by chmeee
Look at git rebase
. The -i
option gives you the chance to edit, squash (what you want), or even remove, commits.
看看git rebase
。该-i
选项使您有机会编辑、压缩(您想要的)甚至删除提交。
For example, the way I use it generally is as:
比如我一般的使用方式是这样的:
git rebase -i origin/master
....
git push