Git:如何对从分支到主控的提交进行变基和压缩?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15727597/
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
Git: How to rebase and squash commits from branch to master?
提问by Viacheslav Kondratiuk
I'm trying to rebase and squash all my commits from current branch to master. Here is what I'm trying to do:
我正在尝试重新设置并压缩从当前分支到主分支的所有提交。这是我想要做的:
git checkout -b new-feature
make a couple of commits, after it I was trying:
做几次提交,之后我尝试:
git rebase -i master
in this case commits will remain in new-feature
branch
在这种情况下,提交将保留在new-feature
分支中
git checkout master
git rebase -i new-feature
It gives me and edit window with noop message.
它给了我和带有 noop 消息的编辑窗口。
I know about command:
我知道命令:
git merge --squash new-feature
But I'm currently working on learning of rebase
command.
但我目前正在学习rebase
命令。
采纳答案by knittl
When rebasing, Git will not move commits to another branch. It will move the branch including all its commits. If you want to get the commits into master after rebasing on top of it, use git merge <branch tip or commit of branch>
to fast-forward the master branch to that commit.
变基时,Git 不会将提交移动到另一个分支。它将移动分支,包括其所有提交。如果您想在基于它的基础上将提交提交到 master,请使用git merge <branch tip or commit of branch>
将 master 分支快进到该提交。
回答by Joe
Lets go though the steps.
让我们通过这些步骤。
1 - We create a new feature branch
1 - 我们创建一个新的功能分支
git checkout -b new-feature
2 - Now you can add/remove and update whatever you want on your new branch
2 - 现在您可以在新分支上添加/删除和更新您想要的任何内容
git add <new-file>
git commit -am "Added new file"
git rm <file-name>
git commit -am "Removed a file"
cat "add more stuff to file" >> <new-file>
git commit -am "Updated files"
3 - Next, pick and squash any commits down into one nice pretty commit message
3 - 接下来,选择并压缩任何提交到一个漂亮的提交消息中
git rebase -i master
The key thing you need to remember here is to change the text that says "pick" to "squash" for all of the commits after the first commit. This will squash all of the commits down to your master branch.
在此您需要记住的关键是在第一次提交后将所有提交的“pick”文本更改为“squash”。这会将所有提交压缩到您的主分支。
4 - Select the master branch
4 - 选择主分支
git checkout master
5 - Move the HEAD and the master branch to where new-feature is:
5 - 将 HEAD 和 master 分支移动到新功能所在的位置:
git rebase new-feature
You can try all of the commands out in this visual tool: http://pcottle.github.io/learnGitBranching/
您可以在此可视化工具中尝试所有命令:http: //pcottle.github.io/learnGitBranching/