我如何从远程跟踪分支“git fetch”和“git merge”(如“git pull”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2602546/
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
How Do I 'git fetch' and 'git merge' from a Remote Tracking Branch (like 'git pull')
提问by kaybenleroll
I have set up some remote tracking branches in git, but I never seem to be able to merge them into the local branch once I have updated them with 'git fetch'.
我已经在 git 中设置了一些远程跟踪分支,但是一旦我用“git fetch”更新它们,我似乎永远无法将它们合并到本地分支中。
For example, suppose I have remote branch called 'an-other-branch'. I set that up locally as a tracking branch using
例如,假设我有一个名为“an-other-branch”的远程分支。我在本地将其设置为使用的跟踪分支
git branch --track an-other-branch origin/an-other-branch
So far, so good. But if that branch gets updated (usually by me moving machine and commiting from that machine), and I want to update it on the original machine, I'm running into trouble with fetch/merge:
到现在为止还挺好。但是,如果该分支被更新(通常是通过我移动机器并从该机器提交),并且我想在原始机器上更新它,我就会遇到获取/合并的麻烦:
git fetch origin an-other-branch
git merge origin/an-other-branch
Whenever I do this, I get an 'Already up-to-date' message and nothing merges.
每当我这样做时,我都会收到一条“已经是最新的”消息,但没有任何合并。
However, a
然而,一个
git pull origin an-other-branch
always updates it like you would expect.
总是像您期望的那样更新它。
Also, running git diff
另外,运行 git diff
git diff origin/an-other-branch
shows that there are differences, so I think I have my syntax wrong.
表明存在差异,所以我认为我的语法错误。
What am I doing wrong?
我究竟做错了什么?
EDIT [2010-04-09]:I have checked a couple of times, and I'm definitely not on a different branch. Should my 'git fetch' followed by a 'git merge' (as shown above) do the exact same thing as a git pull? I will get some workflow showing the results of a git status etc.
编辑 [2010-04-09]:我检查了几次,我绝对不在不同的分支上。我的 'git fetch' 后跟一个 'git merge'(如上所示)是否应该与 git pull 做完全相同的事情?我会得到一些显示 git status 等结果的工作流程。
回答by Gareth
You don't fetch a branch, you fetch an entire remote:
您不获取分支,而是获取整个遥控器:
git fetch origin
git merge origin/an-other-branch
回答by cdunn2001
Selecting just one branch: fetch
/merge
vs.pull
只选择一个分支:fetch
/ merge
vs.pull
People often advise you to separate "fetching" from "merging". They say instead of this:
人们经常建议您将“获取”与“合并”分开。他们说而不是这样:
git pull remoteR branchB
do this:
做这个:
git fetch remoteR
git merge remoteR branchB
What they don't mention is that such a fetch command will actually fetch allbranches from the remote repo, which is notwhat that pull command does. If you have thousands of branches in the remote repo, but you do not want to see all of them, you can run this obscure command:
他们没有提到的是,这样的 fetch 命令实际上会从远程 repo 中获取所有分支,这不是pull 命令所做的。如果远程仓库中有数千个分支,但不想看到所有分支,则可以运行以下晦涩的命令:
git fetch remoteR refs/heads/branchB:refs/remotes/remoteR/branchB
git branch -a # to verify
git branch -t branchB remoteR/branchB
Of course, that's ridiculously hard to remember, so if you really want to avoid fetching all branches, it is better to alter your .git/config
as described in ProGit.
当然,这很难记住,所以如果你真的想避免获取所有分支,最好.git/config
按照 ProGit 中的描述改变你的。
Huh?
嗯?
The best explanation of all this is in Chapter 9-5 of ProGit, Git Internals - The Refspec(or via github). That is amazingly hard to find via Google.
所有这一切的最好解释是在 ProGit 的第 9-5 章,Git Internals - The Refspec(或通过 github)。通过谷歌很难找到。
First, we need to clear up some terminology. For remote-branch-tracking, there are typically 3 different branches to be aware of:
首先,我们需要澄清一些术语。对于远程分支跟踪,通常需要注意 3 个不同的分支:
- The branch on the remote repo:
refs/heads/branchB
inside the other repo - Your remote-tracking branch:
refs/remotes/remoteR/branchB
in yourrepo - Your own branch:
refs/heads/branchB
inside yourrepo
- 远程仓库上的分支:
refs/heads/branchB
在另一个仓库内 - 您的远程跟踪分支:
refs/remotes/remoteR/branchB
在您的仓库中 - 你自己的分支:
refs/heads/branchB
在你的仓库里
Remote-tracking branches (in refs/remotes
) are read-only. You do not modify those directly. You modify your own branch, and then you push to the corresponding branch at the remote repo. The result is not reflected in your refs/remotes
until after an appropriate pull or fetch. That distinction was difficult for me to understand from the git man-pages, mainly because the local branch (refs/heads/branchB
) is said to "track" the remote-tracking branch when .git/config
defines branch.branchB.remote = remoteR
.
远程跟踪分支(in refs/remotes
)是只读的。您不要直接修改它们。你修改自己的分支,然后推送到远程仓库对应的分支。refs/remotes
直到适当的拉取或提取之后,结果才会反映在您的文件中。我很难从 git 手册页中理解这种区别,主要是因为本地分支 ( refs/heads/branchB
) 在.git/config
定义时被称为“跟踪”远程跟踪分支branch.branchB.remote = remoteR
。
Think of 'refs' as C++ pointers. Physically, they are files containing SHA-digests, but basically they are just pointers into the commit tree. git fetch
will add many nodes to your commit-tree, but how git decides what pointers to move is a bit complicated.
将“refs”视为 C++ 指针。从物理上讲,它们是包含 SHA 摘要的文件,但基本上它们只是指向提交树的指针。git fetch
将向您的提交树添加许多节点,但是 git 如何决定要移动的指针有点复杂。
As mentioned in another answer, neither
正如另一个答案中提到的,两者都没有
git pull remoteR branchB
nor
也不
git fetch remoteR branchB
would move refs/remotes/branches/branchB
, and the latter certainly cannot move refs/heads/branchB
. However, both move FETCH_HEAD
. (You can cat
any of these files inside .git/
to see when they change.) And git merge
will refer to FETCH_HEAD
, while setting MERGE_ORIG
, etc.
会动refs/remotes/branches/branchB
,后者肯定动不了refs/heads/branchB
。然而,两者都移动FETCH_HEAD
。(你可以cat
任意里面的这些文件.git/
时看到他们改变的。)git merge
将把FETCH_HEAD
,而设定MERGE_ORIG
,等等。
回答by VonC
Are you sure you are on the local an-other-branch
when you merge?
你确定an-other-branch
你合并时在本地吗?
git fetch origin an-other-branch
git checkout an-other-branch
git merge origin/an-other-branch
The other explanation:
在其他的解释:
all the changes from the branch you're trying to merge have already been merged to the branch you're currently on.
More specifically it means that the branch you're trying to merge is a parent of your current branchif you're ahead of the remote repo by one commit, it's the remote repo that's out of date, not you.
您尝试合并的分支的所有更改都已合并到您当前所在的分支。
更具体地说,这意味着您尝试合并的分支是当前分支的父分支如果您在一次提交之前领先于远程仓库,那么过期的是远程仓库,而不是您。
But in your case, if git pull
works, that just means you are not on the right branch.
但是在您的情况下,如果git pull
有效,那仅意味着您不在正确的分支上。
回答by RDL
Git pull is actually a combo tool: it runs git fetch (getting the changes) and git merge (merging them with your current copy)
Git pull 实际上是一个组合工具:它运行 git fetch (获取更改)和 git merge (将它们与您当前的副本合并)
Are you sure you are on the correct branch?
你确定你在正确的分支上吗?
回答by user1524957
these are the commands:
这些是命令:
git fetch origin
git merge origin/somebranch somebranch
if you do this on the second line:
如果您在第二行执行此操作:
git merge origin somebranch
it will try to merge the local master into your current branch.
它将尝试将本地 master 合并到您当前的分支中。
The question, as I've understood it, was you fetched already locally and want to now merge your branch to the latest of the samebranch.
正如我所理解的,问题是您已经在本地获取并希望现在将您的分支合并到同一分支的最新版本。