简体   繁体   English

git自创建以来一个分支

[英]git rebase a branch since it was created

At the moment when rebasing a branch (eg a feature branch before merging into master ) I have to manually look up when the branch was created to tell git rebase the start-point for rebase . 在重订一个分支我必须手动查找该分公司的成立是为了告诉(如一个特性分支合并到之前)时的那一刻git rebase的起点为rebase

I have the strong feeling that this can be automated — is there a way to tell git rebase to start when the branch was created? 我有强烈的感觉,这可以自动化 - 有没有办法告诉git rebase分支创建时启动?

Assuming you are working on the branch feature/foo which is based on the master branch. 假设您正在处理基于master分支的分支feature/foo

        F--J--K--|feature/baz|
       /
A--B--D--G--|master|
    \
     C--E--H--|feature/foo|HEAD|

In order to rebase the work onto master there are different options: 为了rebase工作到master有不同的选择:

  1. As explained by remram you can explicitly name the target commit using the --onto : 正如remram解释的,您可以使用--onto显式命名目标提交:

     // 1.1. Will move the flag `feature/foo` git rebase --onto master B feature/foo // 1.2. Will not move the flag `feature/foo` git rebase --onto master BH // 1.3. If the HEAD is at H git rebase --onto master B 

    Since the --onto lets you define the target commit it is useful if you want to rebase feature/foo onto feature/baz which do not share a common branch base. 由于--onto允许您定义目标提交,因此如果您想将feature/foo --onto到不共享公共分支库的feature/baz ,则它非常有用。

     // 1.4. Will rebase `feature/foo` onto `feature/baz` git rebase --onto feature/baz B feature/foo /--|feature/baz| / F--J--K--C'--E'--H'--|feature/foo|HEAD| / A--B--D--G--|master| 
  2. As explained by Stefan Fleiter you do not have to name the ancestor if feature/foo is based on master : 正如Stefan Fleiter解释的,如果feature/foo基于master则不必命名祖先:

     // 2.1. Explicitly naming feature/foo as the branch to be rebased onto master git rebase master feature/foo // 2.2. Assuming that feature/foo is currently checked out aka. HEAD git checkout feature/foo git rebase master 
  3. If you do not feel comfortable with the rebase process since your former branch feature/foo "disappears" cherry-pick might be a feel-good alternative for you. 如果你对rebase过程感到不舒服,因为你以前的分支feature/foo “消失”, cherry-pick选择对你来说可能是一个感觉很好的选择。 The command allows to specify a range of commits and behaves very similar to rebase --onto . 该命令允许指定一系列提交,其行为与rebase --onto非常相似。 The difference is that the branch you are cherry-picking from will remain untouched. 不同之处在于,您从中挑选的分支将保持不变。

     // 3.1.1. Start by creating a new branch where master is git checkout master git checkout -b feature/foo-cherried F--J--K--|feature/baz| / A--B--D--G \\ \\ \\ \\-|master|feature/foo-cherried|HEAD| \\ C--E--H--|feature/foo| // 3.1.2. Pick a range starting at the common branch base git cherry-pick B..H F--J--K--|feature/baz| / A--B--D--G--C'--E'--H'--|feature/foo-cherried|HEAD| \\ \\ \\ \\-|master| \\ C--E--H--|feature/foo| // 3.1.3. Later on if everything worked fine you can // delete the unmerged branch feature/foo git branch -D feature/foo F--J--K--|feature/baz| / A--B--D--G--C'--E'--H'--|feature/foo-cherried|HEAD| \\ \\-|master| 

For more information about git rebase --onto I recommend a blog post here: 有关git rebase --onto更多信息,我建议在这里发布博客文章:

The full rebase syntax is git rebase --onto target starting-point mybranch . 完整的rebase语法是git rebase --onto target starting-point mybranch If mybranch is omitted, the current one will be used. 如果省略mybranch ,将使用当前的mybranch

In most cases, you can just do git rebase target . 在大多数情况下,你可以只做git rebase target In this case, the last common ancestor of the current branch ( mybranch ) and target will be used (see merge-base ). 在这种情况下,将使用当前分支( mybranch )和target的最后一个共同祖先(请参阅merge-base )。 So, if you're rebasing a feature branch on the branch it was based on, this last form should work. 所以,如果你在它所基于的分支上重新定义一个特征分支,那么最后一个形式应该可行。

See also the page on rebase from the Git book . 另请参阅Git书中关于rebase页面

When rebasing a feature branch with git you do not have to know the common ancestor. 使用git重新定义功能分支时,您不必了解共同的祖先。 Simply switch to your feature branch 只需切换到您的功能分支

    git checkout my-feature-branch

rebase to master or some other branch you like 重新掌握或掌握你喜欢的其他分支

    git rebase master

switch back to master 切换回主人

    git checkout master

and (fast-forward) merge the feature branch to set HEAD in the master branch to the latest rebased commit. 和(快进)合并功能分支,将主分支中的HEAD设置为最新的重定向提交。

    git merge my-feature-branch

For more details please have a look at the detailed explanation in the Pro Git book. 有关详细信息,请查看Pro Git书中的详细说明 I hope this answers your question. 我希望这回答了你的问题。

What worked very well for me was to just git rebase master -i . 对我来说非常有效的是git rebase master -i

This assumes you're 1) standing on the branch you want to rebase and 2) rebase onto master. 这假设你是1)站在你想要变基的分支上,2)变成主人。 It will also allow me to edit each committ if neccessary. 如果需要,它还允许我编辑每个提交。

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

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