git rebase origin/develop 与 git rebase develop

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42617683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:51:17  来源:igfitidea点击:

git rebase origin/develop vs git rebase develop

gitgit-rebase

提问by Polymerase

Let's assume the current branch is MyFeatureX. And the local developbranch is up-to-date with origin. Would the two statements below be equivalent? What is the recommended syntax?

让我们假设当前分支是 MyFeatureX。并且本地develop分支是最新的起源。下面的两个语句是等价的吗?推荐的语法是什么?

git rebase origin/develop 
git rebase develop 

Please note: This is notthe same question as git rebase origin vs.git rebase origin/master

请注意:这与git rebase origin vs.git rebase origin/master不是同一个问题

回答by Andy Ray

Your local branch developtracksorigin/developand they might not always have the same commits in them.

您的本地分支develop跟踪origin/develop并且它们可能并不总是具有相同的提交。

$ cat ~/.git/config

[remote "origin"]
    url = [email protected]/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

This means we have a "remote" (a repository living somewhere else) arbitrarily named "origin".

这意味着我们有一个任意命名为“origin”的“远程”(位于其他地方的存储库)。

Your local developbranch is tracking the branch from that remote repository, and there's a local reference to that data called origin/develop. For the most part, we consider these two things to always contain the same commits. However, you need to explicitly update your local developbranch to get the latest data from origin. This is mostly commonly done with a pull:

您的本地develop分支正在跟踪该远程存储库中的分支,并且对该数据的本地引用称为origin/develop. 在大多数情况下,我们认为这两件事总是包含相同的提交。但是,您需要显式更新本地develop分支以从源获取最新数据。这通常是通过拉动来完成的:

$ git status
    on branch develop
$ git pull
    ...pulls latest changes from origin

However, a git pullactually does two steps, a fetchand a merge. The first step it does under the hood is fetch all the latest commits from origin, which you can do with

然而, agit pull实际上做了两个步骤, afetch和 a merge。它在后台执行的第一步是从源中获取所有最新提交,您可以使用它

git fetch origin

This will update the branch origin/develop, but not your local branch develop.

这将更新分支origin/develop,但不会更新您的本地分支develop

The newest commits are hidden "behind the scenes" in your local .git directory, which you can reference from the branch named "origin/develop".

最新的提交隐藏在您本地 .git 目录中的“幕后”,您可以从名为“origin/develop”的分支中引用该目录。

After a fetch, to actually make your local branch origin, you would have to do git merge origin/develop. This technically isn't a merge, it will be a "fast forward," meaning git is smart enough to make your local originbranch match origin/developwithout actually merging anything. For this reason and others, merges freak me out in git.

获取后,要实际创建本地分支origin,您必须执行git merge origin/develop. 从技术上讲,这不是合并,而是“快进”,这意味着 git 足够聪明,可以在不实际合并任何内容的情况下使您的本地origin分支匹配origin/develop。出于这个原因和其他原因,合并让我在 git 中感到害怕。

So if you rebase off of develop, there's a chance it will be out of date (older) than origin/develop.

所以,如果你的衍合过develop,有一个机会,这将是日(以上)比了origin/develop

I personally do this workflow before rebasing:

我个人在变基之前执行此工作流程:

git fetch --all
git rebase origin/branchname

This means that I can pull all data down without having to think too much about what branch I'm on, and rebase off the latest code from the remote repository. Later, on the developbranch, a simple git pullwill make sure you're up to date.

这意味着我可以提取所有数据而不必过多考虑我所在的分支,并从远程存储库中重新设置最新代码。稍后,在develop分支上,一个简单的方法git pull将确保您是最新的。

回答by Penguin Brian

If developpoints to the same commit as origin/developthen the two commands are exactly the same. I would tend to use origin/developin case I had forgotten to update my local develop(assuming this branch is eventually going to be pushed to origin/develop).

如果develop指向相同的提交,origin/develop则这两个命令完全相同。origin/develop如果我忘记更新我的本地develop(假设这个分支最终会被推送到origin/develop),我会倾向于使用。

回答by torek

As I wrote in my other (long) answer to your previous question Why is "rebase --onto ABC" different than "rebase ABC"?, both of these commands turn off --fork-pointmode, so—provided that both developand origin/developresolve to the same commit hash, as they do in your question premise—the two commands will do the same thing.

正如我在对您之前的问题的另一个(长篇)回答中所写的那样,为什么“rebase --onto ABC”与“rebase ABC”不同?,这两个命令的关闭--fork-point模式,所以提供的这两个developorigin/develop决心同犯散,因为他们在你的问题的前提下,这两个命令会做同样的事情做。

Omitting the <upstream>argument turns --fork-pointon by default, and the reflog for the branch's configured upstream may have a lot of data in it. In this case, they may act quite differently. By adding an explicit --no-fork-point, you can prevent git merge-base --fork-pointfrom dropping upstream-removed commits.

省略<upstream>参数--fork-point默认开启,分支配置的上游的 reflog 中可能有很多数据。在这种情况下,它们的行为可能完全不同。通过添加显式--no-fork-point,您可以防止git merge-base --fork-point删除上游删除的提交。