Should I git rebase twice?

huangapple go评论58阅读模式
英文:

should I git rebase twice?

问题

  1. 如何重新基于两个分支并保持结构?(过去当我重新基于分支的尖端时,那个分支被重新基于,但其他分支保持不变)

  2. 是否有一种方法可以在主分支上重新创建“MyAwesomeFeature”分支,然后在主分支上创建仅包含“MyExperiment”的更改的另一个分支?

英文:

I have the following situation with a repo.

Over the master branch I made a branch "MyAwesomeFeature" and made several commits.

Later the remote master moved so I git pull origin master and later rebased my branch to the top of master. No problem there

I wanted to try an experiment in my branch, so I did git checkout -b MyExperiment over MyAwesomeFeature branch.

Now master has moved again.

So my question is

  1. How can I rebase both branches and keep the structure? (in the past when I rebased the tip, that was the branch rebased but the other stayed )

  2. Is there a way that I can rebranch "MyAwesomeFeature" over master and then create another branch over master with only the changes of MyExperiment?

答案1

得分: 1

自Git 2.38.0版本开始,您只需将“最长”的分支进行变基并使用 --update-refs

git switch MyExperiment
git rebase -i --update-refs origin/master

这将为您更新MyAwesomeFeature

在Git 2.38之前,您可以选择不同的方法。进行两次变基:

git rebase origin/master MyAwesomeFeature
git rebase --onto MyAwesomeFeature MyAwesomeFeature@{1} MyExperiment

或者只进行一次变基,但需要手动重新设置更新后的分支:

git rebase origin/master MyExperiment
git log origin/master..   # 查找MyAwesomeFeature应指向的新提交
git branch -f MyAwesomeFeature <that_commit>
英文:

Starting with Git 2.38.0, you would rebase only the "longest" branch and use --update-refs:

git switch MyExperiment
git rebase -i --update-refs origin/master

This will update MyAwesomeFeature for you.

Before Git 2.38 you can choose different approaches. Rebase twice:

git rebase origin/master MyAwesomeFeature
git rebase --onto MyAwesomeFeature MyAwesomeFeature@{1} MyExperiment

Rebase once, but reseat the updated branch manually:

git rebase origin/master MyExperiment
git log origin/master..   # look up the new commit that MyAwesomeFeature should point to
git branch -f MyAwesomeFeature &lt;that_commit&gt;

huangapple
  • 本文由 发表于 2023年3月1日 13:44:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定