为什么 Git 拒绝我的 pull 只是因为我在本地分支上有一个提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10067930/
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 is Git rejecting my pull simply because I have a commit on my local branch?
提问by Jason Galuten
There is a Git repository on a server that my colleague and I both push to and pull from. It works fine as long as we pull before committing.
服务器上有一个 Git 存储库,我和我的同事都向它推送和从中提取。只要我们在提交之前拉动它就可以正常工作。
However, if he has pushed to the master branch, and in the mean time I have made a local commit, when I try to pull I get this:
但是,如果他已推送到主分支,同时我进行了本地提交,则当我尝试拉取时,我会得到以下信息:
! [rejected] master -> master (non-fast-forward)
But I know that there should be no conflict.
但我知道不应该有冲突。
The way I get around it is by pulling into a new temporary branch and then merging that into my master like this:
我绕过它的方法是拉入一个新的临时分支,然后像这样将其合并到我的主分支中:
% git pull origin master:temp
From ssh://example.com/home/my/remote/repo
* [new branch] master -> temp
Already up-to-date.
% git merge temp
Already up-to-date.
% git push origin master:master
Notice that Git acts like I'm not doing anything, but really I have shaken it into submission.
请注意,Git 表现得好像我什么都没做,但实际上我已经动摇它屈服了。
Recently I realized that instead of trying to to "convince" Git that it's OK for me to pull. I can just pretend that I haven't committed yet with git reset --soft HEAD^
and git stash
and then do the pull and commit on top of that.
最近我意识到与其试图“说服”Git,我可以拉取。我可以假装我还没有尚未提交git reset --soft HEAD^
和git stash
,然后做拉,并承诺在此之上。
What might be causing this strangely finicky behavior?
什么可能导致这种奇怪的挑剔行为?
I was able to reproduce this problem all on my local machine. Here's what I did:
我能够在我的本地机器上重现这个问题。这是我所做的:
First I made the first "local" repository and added a file.
首先,我创建了第一个“本地”存储库并添加了一个文件。
% cd
% mkdir local-1
% cd local-1/
% mkdir website
% cd website/
% git init
Initialized empty Git repository in /Users/jason/local-1/website/.git/
% touch file
% git add .
% git commit -m 'added file'
[master (root-commit) 6d4b322] added file
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file
Then I made the "remote" repository.
然后我制作了“远程”存储库。
% cd
% mkdir remote
% cd remote
% mkdir website.git
% cd website.git/
% git init --bare
Initialized empty Git repository in /Users/jason/remote/website.git/
Then I went back to the local, created a ref and pushed to the remote.
然后我回到本地,创建一个 ref 并推送到远程。
% cd ~/local-1/website/
% git remote add web ~/remote/website.git
% git push web +master:refs/heads/master
Counting objects: 3, done.
Writing objects: 100% (3/3), 207 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /Users/jason/remote/website.git
* [new branch] master -> master
After that I cloned the remote into a second local.
之后,我将遥控器克隆到第二个本地。
% cd
% mkdir local-2
% cd local-2
% git clone ~/remote/website.git
Cloning into website...
done.
Then I created a ref to the remote from the second local and pushed (this is where I am creating the problem I think).
然后我从第二个本地创建了一个对远程的引用并推送(这是我创建我认为的问题的地方)。
% cd website/
% git remote add web ~/remote/website.git
% git push web +master:refs/heads/master
Everything up-to-date
Then I made a change to local-2, committed, and pushed.
然后我对 local-2 进行了更改,提交并推送。
% touch another
% git add .
% git commit -m 'added another'
[master be91180] added another
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 another
% git push web
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 238 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
To /Users/jason/remote/website.git
6d4b322..be91180 master -> master
Finally, I made a different change to local-1, committed, and tried to push.
最后,我对 local-1 进行了不同的更改,提交并尝试推送。
% cd ~/local-1/website/
% touch something
% git add .
% git commit -m 'added something'
[master 3984529] added something
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 something
% git push web
To /Users/jason/remote/website.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '/Users/jason/remote/website.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again. See the
'Note about fast-forwards' section of 'git push --help' for details.
Blast! How about a pull?
爆破!拉一下怎么样?
% git pull web master:master
From /Users/jason/remote/website
! [rejected] master -> master (non-fast-forward)
OK, so there's the problem. How do I fix it?
好的,问题来了。我如何解决它?
回答by CB Bailey
You probably meant to do:
你可能打算这样做:
git pull web master
Using master:master
is trying to directly update you local master
branch in the fetch stage of the pull which is causing the non fast-forward error.
Usingmaster:master
是尝试master
在 pull 的 fetch 阶段直接更新你的本地分支,这会导致非快进错误。
If you're on a branch that is set up to track web/master
then you only need git pull web
and this will update your remote tracking branches as well.
如果您在一个设置为跟踪的分支上,web/master
那么您只需要git pull web
,这也将更新您的远程跟踪分支。