简体   繁体   English

在git中创建新分支时如何执行壁球?

[英]How do I perform a squash while creating a new branch in git?

Is there any way I can squash all the previous commit before creating a new branch. 有什么办法可以在创建新分支之前压缩所有先前的提交。 I want a clean commit page in bitbucket so that everything on the master branch before the creation of the new branch shows as only 1 commit. 我希望在bitbucket中有一个干净的提交页面,以便在创建新分支之前master分支上的所有内容都显示为仅1次提交。

I know the other options of squash while merging a branch or while pushing a branch. 我知道合并分支或推动分支时压扁的其他选择。 I found another answer in stackoverflow which tells how to squash your commit before pushing by using "git rebase -i", but I was looking for an option to squash the commits in the new branch. 我在stackoverflow中找到了另一个答案,该答案告诉您如何在使用“ git rebase -i”进行推送之前压缩提交,但是我正在寻找一种在新分支中压缩提交的选项。

See the description of the --orphan option in the git-checkout man page.: 请参见git-checkout手册页中--orphan选项的说明:

--orphan <new_branch>
       Create a new orphan branch, named <new_branch>, started from
       <start_point> and switch to it. The first commit made on this new
       branch will have no parents and it will be the root of a new
       history totally disconnected from all the other branches and
       commits.

That seems like what you're trying to do, here. 这似乎是您要尝试执行的操作。

You could create the new branch where you are and then do your rebase -i and you will end up with your old branch untouched and your new branch squashed: 您可以在您所在的位置创建新分支,然后重新设置rebase -i ,最后您的旧分支将保持不变,而新分支将被压缩:

git checkout -b my_new_branch
git rebase -i

When you create your new branch, checkout it or not depending on which branch you want to squash, the new or the old one... 创建新分支时,请根据要压缩的分支,新分支还是旧分支来签收或不签出...

You must understand that git always create new commits, so when you use rebase -i to squash, git just create a new commit with all the content of the commits squashed. 您必须了解git始终会创建新的提交,因此当您使用rebase -i进行压缩时,git会创建一个新的提交,其中所有提交的内容都将被压缩。 The old ones are no more visible because not "handled" by a reference. 旧的不再可见,因为未通过引用“处理”。 The fact that you keep a reference (the old branch), makes that you still see them. 保留引用(旧分支)的事实使您仍然可以看到它们。

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

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