简体   繁体   English

从master合并后的Squash功能分支提交

[英]Squash feature branch commit after merging from master

I am trying to squash commits in a branch to that when it is finally merged to master (after pull request if aproved) the commit history looks clean.Hence before raising the pull request I do a 我试图在一个分支中压缩提交,当它最终合并到master(在拉取请求后如果被批准)提交历史看起来很干净。在提出pull请求之前我做了一个

git rebase -i

and rewrite the history. 并重写历史。

However while developing the feature in its branch I have to merge the content of the master onto the branch because master usually moves ahead due to other feature branches being merged. 然而,当在其分支中开发该特征时,我必须将主内容合并到分支上,因为由于其他特征分支被合并,主人通常会向前移动。

I see ones I merged master to feature branch I can not squash he commits any more using interactive rebase. 我看到我合并master到feature分支我不能压缩他使用交互式rebase再提交。 It leads to unusual diff during pull requests ie changes which came as part of merges from master. 它在拉取请求期间导致异常差异,即作为来自主合并的合并的一部分而发生的变化。

What is the best way to squash the commits in this case? 在这种情况下,压缩提交的最佳方法是什么?

If you just want to sqaush everything, then you have a way simpler way to do that that does not rely on using interactive rebasing. 如果你只是想要sqaush所有东西,那么你有一种更简单的方法来做到这一点,而不依赖于使用交互式变基。 You can just do a soft reset to your master branch and then commit those changes: 您只需对主分支进行软重置,然后提交这些更改:

git reset --soft master
git commit -m 'All changes from my branch squashed'

This basically resets the branch pointer to the master branch, without changing any of the content in your working directory. 这基本上会重置指向master分支的分支指针,而不会更改工作目录中的任何内容。 So the result is that you have all those changes in your index which you can commit then at once. 因此,结果是您可以在索引中进行所有这些更改,然后您可以立即提交。

If you want the feature branch is on the top of master branch, so that your pull request contains changes from other feature branches merged. 如果您希望功能分支位于主分支的顶部,那么您的请求包含来自其他功能分支合并的更改。 You can use below commands: 您可以使用以下命令:

git checkout feature
git pull origin master --rebase

This will rebase feature branch on the top of master branch after fetching from origin master. 这将从源主控器获取后在主分支的顶部重新定义功能分支。

I am trying to squash commits in a branch to that when it is finally merged to master (after pull request if aproved) the commit history looks clean 我试图将一个分支中的提交压缩到最终合并到master的时候(在pull请求之后如果被批准),提交历史看起来很干净

You can use soft reset to squash your branch (say, feature ) commits. 您可以使用soft reset来压缩分支(例如, feature )提交。 Say you have 5 commits in feature branch. 假设您在feature分支中有5个提交。 Now you need 5 commits = 1 new commit. 现在你需要5次提交= 1次新提交。

$ git checkout feature
$ git log                        # see how many commits do you need to squash

$ git reset --soft HEAD~4        # note: if you have N commits then HEAD~{N-1}
Or, git reset --soft <first-commit> # reset to first commit of your branch

$ git add .                      # add the files  
$ git commit --amend -am 'Squash all commits'  # squash all the commits

$ git push -f origin feature     # force(-f) push to remote since git history is changed  

Now, Pull master branch changes. 现在,Pull master分支发生了变化。

$ git pull origin master

Create a Pull request now using GitHub GUI (browser). 现在使用GitHub GUI(浏览器)创建Pull请求。

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

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