自创建以来 git rebase 分支

lig*_*igi 6 git rebase git-rebase

目前,当重新调整分支(例如合并到master之前的功能分支)时,我必须手动查找分支的创建时间以告知.git rebaserebase

\n\n

我有一种强烈的感觉,这可以自动化 \xe2\x80\x94 有没有办法告诉在创建分支git rebase时开始?

\n

JJD*_*JJD 4

feature/foo假设您正在基于分支的分支上工作master

        F--J--K--|feature/baz|
       /
A--B--D--G--|master|
    \
     C--E--H--|feature/foo|HEAD|
Run Code Online (Sandbox Code Playgroud)

为了rebase继续工作,master有不同的选择:

  1. 正如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 B H
    // 1.3. If the HEAD is at H
    git rebase --onto master B
    
    Run Code Online (Sandbox Code Playgroud)

    由于它--onto允许您定义目标提交,因此如果您想要重新建立不共享公共分支基础的feature/foo基础,那么它会很有用。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|
    
    Run Code Online (Sandbox Code Playgroud)
  2. 正如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
    
    Run Code Online (Sandbox Code Playgroud)
  3. 如果您对这个过程感到不舒服,rebase因为您以前的分支feature/foo“消失”了,cherry-pick这对您来说可能是一个感觉良好的选择。该命令允许指定提交范围,其行为与rebase --onto. 不同之处在于,您从中挑选的分支将保持不变。

    // 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|
    
    Run Code Online (Sandbox Code Playgroud)

有关更多信息,git rebase --onto我在这里推荐一篇博客文章: