为什么 git fetch origin master 失败?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20858452/
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
why git fetch origin master failed?
提问by Charles0429
Assume that my local repository is one commit behind the repository at github.
假设我的本地存储库是 github 存储库后面的一个提交。
Then I commit one commit at the local repository
然后我在本地存储库提交一次提交
At this time
此时
A------>commit 1 Github/master
A------>commit 1 Github/master
A------>commit 2 local repository/master
A------>commit 2 本地仓库/master
I do the following steps to push commit 2 to github:
我执行以下步骤将提交 2 推送到 github:
- git fetch origin master
- git rebase origin/master
- git push origin master
- git fetch 原点大师
- git rebase 原点/主
- git push origin master
But I got the following errors:
但我收到以下错误:
If I try to replace step 1 with
git fetch origin
, it works well
如果我尝试用 替换第 1 步
git fetch origin
,效果很好
Then I tried git fetch origin master:tmp
, a branch named tmp successfully created
然后我试了一下git fetch origin master:tmp
,成功创建了一个名为 tmp 的分支
So, My question is
所以,我的问题是
why git fetch origin master
sometimes works(in the case git fetch origin master:tmp
), while sometimes not work in the case step 1?
为什么git fetch origin master
有时工作(在这种情况下git fetch origin master:tmp
),而有时在案例步骤 1 中不起作用?
回答by Carlos Martín Nieto
This isn't about working or not, but about where you're asking git store what it downloads. If you omit the target in a refpec, you're asking git to store it in FETCH_HEAD. Thus, git fetch origin master
is really git fetch origin master:FETCH_HEAD
, and you're not touching origin/master
or any ref at all (as you can see from the output, master -> FETCH_HEAD
).
这不是关于工作与否,而是关于你在哪里询问 git store 它下载什么。如果在 refpec 中省略目标,则是在要求 git 将其存储在 FETCH_HEAD 中。因此,git fetch origin master
is real git fetch origin master:FETCH_HEAD
,并且您根本没有接触origin/master
或任何参考(正如您从输出中看到的那样master -> FETCH_HEAD
)。
When you run git fetch origin master:tmp
you're asking it to download the master branch (this is yet another layer, guessing that you want to deal with branches) and store it in a local branch named tmp
. You would also see this mapping in the output.
当您运行时,git fetch origin master:tmp
您要求它下载 master 分支(这是另一层,猜测您要处理分支)并将其存储在名为tmp
. 您还将在输出中看到此映射。
If you want to update the remote-tracking branches, simply call git fetch origin
. Calling the two-argument version of git-fetch
is rarely something you want to do.
如果要更新远程跟踪分支,只需调用git fetch origin
. 调用 的双参数版本git-fetch
很少是您想做的事情。
回答by Robin Green
Instead of
代替
git fetch origin master
git rebase origin/master
you should just do
你应该做
git pull --rebase
That will do the right thing.
那会做正确的事情。