Git 樱桃选择与变基 [英] Git cherry pick vs rebase

查看:15
本文介绍了Git 樱桃选择与变基的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 Git.

在线浏览 指出:

<块引用>

但是,还有另一种方法:您可以将 C3 中引入的更改补丁重新应用到 C4 之上.在 Git 中,这称为变基.使用 rebase 命令,您可以将在一个分支上提交的所有更改应用到另一个分支上.

在本例中,您将运行以下命令:

$ git checkout 实验$ git rebase master首先,倒带头部以在其上重放您的工作......应用:添加分阶段命令

这里的问题"是在这个例子中,experiment"分支(rebase的主题)最初是从master"分支分叉出来的,因此它shares提交C0到C2有了它—实际上,实验"是主",包括 C2 加上在其上提交的 C3.(这是最简单的可能情况;当然,实验"可以在其原始基础之上包含几十个提交.)

现在 git rebase 被告知将experiment"重新绑定到master"的 current 提示上,并且 git rebase 是这样的:

  1. 运行 git merge-base 以查看experiment"和master"共享的最后一次提交是什么(换句话说,转移的重点是什么).这是 C2.
  2. 保存自转移点以来所做的所有提交;在我们的玩具示例中,它只是 C3.
  3. 将 HEAD(在操作开始运行之前指向experiment"的提示提交)倒回以指向master"的提示 —我们正在重新构建它.
  4. 尝试按顺序应用每个保存的提交(就像使用 git apply 一样).在我们的玩具示例中,它只是一次提交,C3.假设它的应用程序将产生一个提交 C3'.
  5. 如果一切顺利,实验"引用将更新为指向应用上次保存的提交(在我们的示例中为 C3')所产生的提交.

现在回到你的问题.如您所见,这里技术上 git rebase 确实将一系列从实验"的提交移植到大师"的尖端,因此您可以正确地判断确实存在另一个分支"在这个过程中.但要点是实验"中的提示提交最终成为实验"中的新提示提交,它只是改变了它的基础:

同样,从技术上讲,您可以看出 git rebase 此处包含了master"的某些提交,这是绝对正确的.

I have recently started working with Git.

Going over the Git book online I found the following under the "Git Rebase" section:

With the rebase command, you can take all the changes that were committed on one branch and replay them on another one.

(Quoted from: http://git-scm.com/book/en/Git-Branching-Rebasing)

I thought this is the exact definition of git cherry-pick (reapply a commit or a set of commit objects on the currently checked out branch).

What is the difference between the two ?

解决方案

Since the time git cherry-pick learned to be able to apply multiple commits, the distinction indeed became somewhat moot, but this is something to be called convergent evolution ;-)

The true distinction lies in original intent to create both tools:

  • git rebase's task is to forward-port a series of changes a developer has in their private repository, created against version X of some upstream branch, to version Y of that same branch (Y > X). This effectively changes the base of that series of commits, hence "rebasing".

    (It also allows the developer to transplant a series of commits onto any arbitrary commit, but this is of less obvious use.)

  • git cherry-pick is for bringing an interesting commit from one line of development to another. A classic example is backporting a security fix made on an unstable development branch to a stable (maintenance) branch, where a merge makes no sense, as it would bring a whole lot of unwanted changes.

    Since its first appearance, git cherry-pick has been able to pick several commits at once, one-by-one.

Hence, possibly the most striking difference between these two commands is how they treat the branch they work on: git cherry-pick usually brings a commit from somewhere else and applies it on top of your current branch, recording a new commit, while git rebase takes your current branch and rewrites a series of its own tip commits in one way or another. Yes, this is a heavily dumbed down description of what git rebase can do, but it's intentional, to try to make the general idea sink in.

Update to further explain an example of using git rebase being discussed.

Given this situation,

The Book states:

However, there is another way: you can take the patch of the change that was introduced in C3 and reapply it on top of C4. In Git, this is called rebasing. With the rebase command, you can take all the changes that were committed on one branch and apply them onto another one.

In this example, you’d run the following:

$ git checkout experiment
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: added staged command

"The catch" here is that in this example, the "experiment" branch (the subject for rebasing) was originally forked off the "master" branch, and hence it shares commits C0 through C2 with it — effectively, "experiment" is "master" up to, and including, C2 plus commit C3 on top of it. (This is the simplest possible case; of course, "experiment" could contain several dozens of commits on top of its original base.)

Now git rebase is told to rebase "experiment" onto the current tip of "master", and git rebase goes like this:

  1. Runs git merge-base to see what's the last commit shared by both "experiment" and "master" (what's the point of diversion, in other words). This is C2.
  2. Saves away all the commits made since the diversion point; in our toy example, it's just C3.
  3. Rewinds the HEAD (which points to the tip commit of "experiment" before the operation starts to run) to point to the tip of "master" — we're rebasing onto it.
  4. Tries to apply each of the saved commits (as if with git apply) in order. In our toy example it's just one commit, C3. Let's say its application will produce a commit C3'.
  5. If all went well, the "experiment" reference is updated to point to the commit resulted from applying the last saved commit (C3' in our case).

Now back to your question. As you can see, here technically git rebase indeed transplants a series of commits from "experiment" to the tip of "master", so you can rightfully tell there indeed is "another branch" in the process. But the gist is that the tip commit from "experiment" ended up being the new tip commit in "experiment", it just changed its base:

Again, technically you can tell that git rebase here incorporated certain commits from "master", and this is absolutely correct.

这篇关于Git 樱桃选择与变基的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆