需要Git rebase参数上下文解释

Tim*_*nov 5 git github

经常git rebase在某个分支的上下文中使用.例如,如果我想在feature分支的顶部移动分支的基础master(基于最新的提交) - 教程说:

git checkout feature
git rebase master 
Run Code Online (Sandbox Code Playgroud)

在这样的教程之后,有很多问题.例如:如何在不签出的情况下执行相同的操作.如何使用--onto选项执行相同的操作.--onto和rebase 之间有什么区别.我可以通过一系列提交,还是必须是整个分支?等等..

我已多次阅读man page但仍有很大的差距.

所以主要问题是: git如何解析不同场景中的args以及我应该如何解释/想象它们在我脑海中?例如:

git rebase master
git rebase feature master
git rebase --onto master feature
git rebase HEAD~4 HEAD~2
git rebase --onto HEAD~4 HEAD~2
Run Code Online (Sandbox Code Playgroud)

我们假设我们有以下回购:

Z -- W (HEAD, test-branch*) 

A -- B -- C -- D -- E (master)
     \
      1 -- 2 -- 3 (feature)

test-branch is checked out.
Run Code Online (Sandbox Code Playgroud)

Sch*_*ern 7

完整的rebase命令就是这个.

git rebase --onto <onto> <upstream> <branch-to-rebase>
Run Code Online (Sandbox Code Playgroud)

Git将采取所有branch-to-rebase不在其中的更改upstream并将其置于顶部onto.

默认值是......

  • branch-to-rebase:当前分支
  • 上游:分支到rebase的跟踪分支
  • :上游

git checkout feature; git rebase master真的git rebase --onto master master feature.

你通常想要ontoupstream成为一样,但有时它们对于精细手术而言是不同的.在这个例子中,upstream和之间的区别onto很明显.

           A--B--C--D master
               \
                E--F--G next
                       \
                        H--I--J topic
Run Code Online (Sandbox Code Playgroud)

如果你git rebase master topic,它将重新定义所有topic与之无关的提交master.所有介入的分支头都将被忽略.那就是E,F,G,H,I和J.由于onto默认为upstream,它会将它们放在上面master.你会结束这件事.

           A--B--C--D master
               \     \
                \     E'-F'-G'-H'-I'-J' topic
                 \
                  E--F--G next
Run Code Online (Sandbox Code Playgroud)

如果您只是想要提交,nexttopic怎么办?这是--onto有用的地方.告诉Git上游是next,但你想要重新加入master.所以跑git rebase --onto master next topic.这将只选择H,I和J.

           A--B--C--D master
               \     \
                \     H'-I'-J' topic
                 \
                  E--F--G next
Run Code Online (Sandbox Code Playgroud)

  • @TimurFayzrakhmanov`instream`是分支的分支."上游"就像在逆流而上.现在是时间.如果你开始走回分支的历史,走向另一个分支,你就会"上游".在我的例子中,`master`是`topic`的上游.对于rebase意味着什么是Git将采取`branch-to-rebase`中的所有更改,这些更改不在`upstream`中并且将它们放在`onto`之上. (2认同)