简体   繁体   English

合并后保留分支历史

[英]Keeping history of branch after merging

Sorry, Git beginner here.抱歉,这里是 Git 初学者。 I have two branches, feature and first-feature .我有两个分支, featurefirst-feature I do 10 commits in first-feature and then merge everything back over to feature .我在first-feature做了 10 次提交,然后将所有内容合并回feature I am wondering, which of the following two scenarios is most common for a Git workflow or if one of them is even wrong?我想知道,以下两种情况中哪一种对于 Git 工作流程最常见,或者其中一种甚至是错误的?

A) Do the 10 commits of first-feature show up in the history of feature as well a)这些10提交的first-feature在历史显示feature ,以及

or或者

B) feature has only a single commit which represents the new feature? B) feature只有一个代表新功能的提交?

Any help is highly appreciated!任何帮助表示高度赞赏!

When merging two branches, git merge preserve all commits made to the merged branch, unless you use the squash flag .合并两个分支时, git merge保留对合并分支所做的所有提交,除非您使用squash flag For example, if you have a feature branch and it has 3 commits when you merge that to the master , all of those commits will show on the master branch history and it will appear as if those commits were written on top of master branch all along.例如,如果您有一个feature branch并且当您将其合并到master时它有 3 个提交,则所有这些提交都将显示在master branch历史记录中,并且看起来好像这些提交一直写在master branch之上.

Edit:编辑:

Preserving history has some pros to it:保存历史有一些优点:

  • It allows you to preserve the commit history for any given feature while not worrying about overriding commits and changing history.它允许您保留任何给定功能的提交历史记录,而不必担心覆盖提交和更改历史记录。 It helps you avoid unnecessary git reverts or resets!它可以帮助您避免不必要的 git 恢复或重置!
  • Different features remain isolated and don't interfere with existing commit histories.不同的功能保持隔离,不会干扰现有的提交历史。
  • Can help you re-integrate a completed feature branch.可以帮助你重新集成一个完整的功能分支。

On the other hand, if you value more a clean, linear history then git rebase or the squash flag may be most appropriate.另一方面,如果您更看重干净、线性的历史记录,那么git rebasesquash flag可能是最合适的。 You will avoid unnecessary commits and keep changes more centralized and linear!您将避免不必要的提交并使更改更加集中和线性!

Unless you specify explicitly, the commits will not be squashed.除非您明确指定,否则不会压缩提交。

So所以

git checkout feature
git merge first-feature
git push

will copy the individual commits将复制单个提交

whereas然而

git checkout feature
git merge --squash first-feature
git push

will create a single merge commit.将创建单个合并提交。

In general squashing is recommended to keep the commit history clean.一般来说,建议使用压缩以保持提交历史记录干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM