当运行git remote -v show
命令时,它返回什么关于origin的信息?
如果origin指向GitHub,则状态应该是最新的,并且不会超前于任何远程存储库。至少,在我使用Git1.6.5进行快速测试时是这样的。
无论如何,为了避免这种情况,应明确定义主分支的远程存储库:
$ git config branch.master.remote yourGitHubRepo.git
然后执行
git pull origin master
,接着执行
git status
应该会返回一个干净的状态(没有 ahead)。为什么?因为
git fetch origin master
(包含在
git pull origin master
中)不仅会更新
FETCH_HEAD
(正如
Charles Bailey 在
他的回答 中所解释的那样),还会更新你本地 Git 仓库中的“远程主分支”。这种情况下,你本地的主分支就不再“领先”于远程主分支了。
我可以使用 git1.6.5 进行测试:
首先创建一个工作仓库:
PS D:\git\tests> cd pullahead
PS D:\git\tests\pullahead> git init workrepo
Initialized empty Git repository in D:/git/tests/pullahead/workrepo/.git/
PS D:\git\tests\pullahead> cd workrepo
PS D:\git\tests\pullahead\workrepo> echo firstContent > afile.txt
PS D:\git\tests\pullahead\workrepo> git add -A
PS D:\git\tests\pullahead\workrepo> git commit -m "first commit"
我通过创建一个裸仓库(可以从任何地方接收推送)来模拟 GitHub 仓库。
PS D:\git\tests\pullahead\workrepo> cd ..
PS D:\git\tests\pullahead> git clone --bare workrepo github
我在工作的代码库中添加了修改,并将其推送到GitHub代码库(作为远程仓库)
PS D:\git\tests\pullahead> cd workrepo
PS D:\git\tests\pullahead\workrepo> echo aModif >> afile.txt
PS D:\git\tests\pullahead\workrepo> git ci -a -m "a modif to send to github"
PS D:\git\tests\pullahead\workrepo> git remote add github d:/git/tests/pullahead/github
PS D:\git\tests\pullahead\workrepo> git push github
我创建了一个主页仓库,并从GitHub上克隆它,在其中进行了一些修改,然后上传到GitHub。
PS D:\git\tests\pullahead\workrepo> cd ..
PS D:\git\tests\pullahead> git clone github homerepo
PS D:\git\tests\pullahead> cd homerepo
PS D:\git\tests\pullahead\homerepo> type afile.txt
firstContent
aModif
PS D:\git\tests\pullahead\homerepo> echo aHomeModif1 >> afile.txt
PS D:\git\tests\pullahead\homerepo> git ci -a -m "a first home modif"
PS D:\git\tests\pullahead\homerepo> echo aHomeModif2 >> afile.txt
PS D:\git\tests\pullahead\homerepo> git ci -a -m "a second home modif"
PS D:\git\tests\pullahead\homerepo> git push github
我随后复制了 workrepo 以进行第一次实验。
PS D:\git\tests\pullahead\workrepo4> cd ..
PS D:\git\tests\pullahead> git clone workrepo workrepo2
Initialized empty Git repository in D:/git/tests/pullahead/workrepo2/.git/
PS D:\git\tests\pullahead> cd workrepo2
PS D:\git\tests\pullahead\workrepo2> git remote add github d:/git/tests/pullahead/github
PS D:\git\tests\pullahead\workrepo2> git pull github master
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From d:/git/tests/pullahead/github
* branch master -> FETCH_HEAD
Updating c2763f2..75ad279
Fast forward
afile.txt | Bin 46 -> 98 bytes
1 files changed, 0 insertions(+), 0 deletions(-)
在那个代码库中,git状态确实提到了主分支“master”领先于“origin”的情况:
PS D:\git\tests\pullahead\workrepo5> git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
nothing to commit (working directory clean)
但这只是origin
不是github:
PS D:\git\tests\pullahead\workrepo2> git remote -v show
github d:/git/tests/pullahead/github (fetch)
github d:/git/tests/pullahead/github (push)
origin D:/git/tests/pullahead/workrepo (fetch)
origin D:/git/tests/pullahead/workrepo (push)
但是,如果我在一个具有指向 GitHub 的 origin(或者根本没有 origin,只定义了一个远程的“github”)的 repo 中重复该序列,则状态干净:
PS D:\git\tests\pullahead\workrepo2> cd ..
PS D:\git\tests\pullahead> git clone workrepo workrepo4
PS D:\git\tests\pullahead> cd workrepo4
PS D:\git\tests\pullahead\workrepo4> git remote rm origin
PS D:\git\tests\pullahead\workrepo4> git remote add github d:/git/tests/pullahead/github
PS D:\git\tests\pullahead\workrepo4> git pull github master
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From d:/git/tests/pullahead/github
* branch master -> FETCH_HEAD
Updating c2763f2..75ad279
Fast forward
afile.txt | Bin 46 -> 98 bytes
1 files changed, 0 insertions(+), 0 deletions(-)
PS D:\git\tests\pullahead\workrepo4> git status
# On branch master
nothing to commit (working directory clean)
如果我只有origin
指向github
,那么对于git1.6.5来说,status
应该是干净的。
对于早期版本的Git,可能会有“ahead”警告,但是无论如何,明确定义git config branch.master.remote yourGitHubRepo.git
应该能够处理这个问题。
git push
似乎也可以解决它(报告“全部更新”)。 - Ben Jamesgit config --get-regexp br.*
可以告诉你,如果你的配置有一个本地分支正在跟踪另一个分支。 - VonCgit config branch.master.remote yourGitHubRepo.git
并检查(在下一次git pull origin
)状态是否仍然显示 'ahead' 警告吗? - VonCgit remote
命令(显示 GitHub 存储库的正确地址)是不够的。为了避免在git pull
后出现 "Your branch is ahead
" 警告消息,您需要首先定义分支的远程名称。因此,我的建议是:输入git config branch.master.remote yourGitHubRepo.git
,然后尝试git pull
和git status
,看看问题是否仍然存在。 - VonC