简体   繁体   English

如何将我的git分支压入同一个分支而无需重新基准化?

[英]How to squash my git branch commits into the same branch without rebasing?

I have a local branch, we're using git-flow with Pull Requests and I'm looking to squash a few commits after receiving PR feedback. 我有一个本地分支,我们将git-flow与Pull Requests一起使用,并且在收到PR反馈后希望压缩一些提交。

How can I squash all my commits (from PR's for example) into the same branch? 如何将所有提交(例如,从PR提交)压缩到同一分支中?

I imagine it would be something like: 我想那会是这样的:

git checkout master                    # For master
git pull                               # Get all branches up-to-date
git checkout feature1                  # Checkout the branch
git checkout -b feature1_squash        # Make a copy of the branch
git branch -D feature1                 # Delete original branch
git checkout master                    # (?) Branch off latest master for safety
git checkout -b feature1               # Make new (empty) original branch
git merge --squash feature1_squash     # Merge with squash into it
git push -f feature1                   # Push, force will be required

but I'm not sure. 但我不确定。
With that many steps it also seems like a good case for using a function to tie it all together and just pass the branch name as a parameter. 经过这么多步骤,使用函数将所有功能绑定在一起,并仅将分支名称作为参数传递,这似乎是一个好案例。 Of course automating it would mean making sure to handle errors, exceptions, edge cases, etc. 当然,自动化将意味着确保处理错误,异常,边缘情况等。

I don't want to use interactive rebase because it's a little tricky to get right for newbies that I train. 我不想使用交互式变基,因为要适合我训练的新手有点棘手。 I also don't want to have to know the number of commits, I just want to do all the ones that exist on this branch. 我也不想知道提交的次数,我只想执行该分支上存在的所有提交。

The answer from VonC is almost there. VonC的答案几乎就在那里。 but knowing that you want to go 3 commits back is tough. 但是知道您想退回3次提交非常困难。

instead you should use merge-base 相反,您应该使用merge-base

git reset --soft $(git merge-base YOUR_BRANCH WHERE_YOU_BRANCHED_FROM)
git commit

and edit your message (or use git commit -m instead) 并编辑您的消息(或使用git commit -m代替)

if your on that branch, you can use HEAD and assuming you branched from master your commands would be (using origin/master due to local master being potentially out of date) 如果您在该分支上,则可以使用HEAD并假设您从master分支出来的命令是(使用origin / master,因为本地master可能已过期)

git reset --soft $(git merge-base HEAD origin/master)
git commit

If you cant remember where you branched from, the place you are merging the PR back in to is likely correct 如果您不记得分支机构的位置,则将PR重新合并到的位置可能是正确的

I think what youre suggesting is unconventional, and more tricky for newbies than using rebase -i to squash. 我认为您的建议是不合常规的,对于新手而言,比使用rebase -i进行挤压更棘手。

What is difficult about git rebase -i HEAD~N (with N being the number of commits to go back to for squashing)? git rebase -i HEAD~NN是要压回的提交次数)有什么困难? You simply read the list, and squash down the commits as you need. 您只需阅读列表,然后根据需要压缩提交即可。 The " Rewriting History " guide has more detail, and I think its perfectly suitable for a new user. 重写历史记录 ”指南有更多详细信息,我认为它非常适合新用户。

You could always make a demo branch for your trainees to practice on. 您可以随时为您的受训人员创建一个演示分支,以进行练习。 Simply make 5-6 commits to a branch for demo purposes, and have the trainee squash the commits with interactive rebase, then make a PR to you to prove they can handle it on a live branch. 只需为演示目的向分支提交5-6次提交,然后让受训者使用交互式变基来压缩提交,然后对您进行PR,以证明他们可以在实时分支上处理该提交。

The alternative to an interactive rebase to squash commits (especially consecutive commits on a branch) is a git reset --soft . 交互式rebase来git reset --soft提交(尤其是分支上的连续提交)的替代方法是git reset --soft
See " Squash my last X commits together using Git " 请参阅“ 使用Git压缩我的最后X个提交

git reset --soft HEAD~3 && 
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

This is one of the " Practical uses of git reset --soft " I mentioned before. 这是我之前提到的“ git reset --soft实际使用 ”之一。

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

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