当我在开发分支中执行 git pull origin master 时会发生什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8746631/
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-10 12:39:20  来源:igfitidea点击:

What happens when I do git pull origin master in the develop branch?

gitpull

提问by e-satis

Let's say I have a private topic branch called develop with 2 commits ahead of master.

假设我有一个名为 develop 的私有主题分支,在 master 之前有 2 个提交。

What does git pull origin masterdo?

有什么作用git pull origin master

Pull everything from the remote master in the local develop and merge it? Pull everything in the local master branch and merge it?

从本地开发中的远程 master 中提取所有内容并合并它?拉取本地主分支中的所有内容并合并它?

And is there a way to update master from develop without git checkout masterfirst?

有没有办法在没有git checkout master先开发的情况下更新 master ?

回答by mrj

git pull origin masterpulls the master branch from the remote called origin into your current branch. It only affects your current branch, not your local master branch.

git pull origin master将 master 分支从远程调用 origin 拉入您的当前分支。它只影响您当前的分支,而不影响您本地的主分支。

It'll give you history looking something like this:

它会给你历史看起来像这样:

- x - x - x - x (develop)
   \         /
    x - x - x (origin/master)

Your local master branch is irrelevant in this. git pullis essentially a combination of git fetchand git merge; it fetches the remote branch then merges it into your current branch. It's a merge like any other; it doesn't do anything magical.

您本地的主分支与此无关。git pull本质上是git fetch和的组合git merge;它获取远程分支,然后将其合并到您的当前分支中。这是一个像任何其他合并一样的合并;它不会做任何神奇的事情。

If you want to update your local master branch, you have no choice but to check it out. It's impossible to merge into a branch that's not checked out, because Git needs a work tree in order to perform the merge. (In particular, it's absolutely necessary in order to report merge conflicts and allow you to resolve them.)

如果你想更新你的本地 master 分支,你别无选择,只能检查出来。合并到一个没有检出的分支是不可能的,因为 Git 需要一个工作树来执行合并。(特别是,为了报告合并冲突并允许您解决它们,这是绝对必要的。)

If you happen to know that pulling into master would be a fast-forward (i.e. you have no commits in your local master branch that aren't in origin's master) you can work around, as described in this answer.

如果您碰巧知道拉入 master 将是一个快进(即,您在本地 master 分支中没有提交不在 origin 的 master 中的提交),您可以解决,如本答案中所述

回答by Vlad

Once you commit you changes into your branch by using

使用以下命令将更改提交到分支后

git add -A
git commit -m <message>

You can then do:

然后你可以这样做:

git pull origin master

into your branch and that will keep your commits on top of the master pull. Your branch will now be even with master + your commits on top. So, you can now do:

进入您的分支,这将使您的提交保持在 master pull 之上。您的分支现在将与 master + 您的提交保持一致。所以,你现在可以这样做:

git push

and git will push your changes, together with the master commits into you branch. You can easily then merge that into master on Github.

并且 git 会将您的更改连同主提交一起推送到您的分支中。然后您可以轻松地将其合并到 Github 上的 master 中。