';git-pull-origin-mybranch';使本地MyN分支在原点之前提交。为什么?

';git-pull-origin-mybranch';使本地MyN分支在原点之前提交。为什么?,git,version-control,git-pull,Git,Version Control,Git Pull,我只是观察到git pull的一些奇怪之处,我不明白 星期五,我在当地一家分公司工作。让我们称之为mybranch。在离开办公室之前,我把它推到了origin(这是我的github repo):git-push-origin-mybranch 昨天在家里,我把我的分支拉到我的笔记本电脑上,做了更多的编码,然后把我的更改推回到github(源代码) 现在我又开始工作了,我试着把昨天的变化拉到我的工作机器上(周末我在工作场所的本地回购协议中没有任何变化): 这导致了快进合并,这很好。然后我做了一个g

我只是观察到git pull的一些奇怪之处,我不明白

星期五,我在当地一家分公司工作。让我们称之为
mybranch
。在离开办公室之前,我把它推到了origin(这是我的github repo):
git-push-origin-mybranch

昨天在家里,我把我的分支拉到我的笔记本电脑上,做了更多的编码,然后把我的更改推回到github(源代码)

现在我又开始工作了,我试着把昨天的变化拉到我的工作机器上(周末我在工作场所的本地回购协议中没有任何变化):

这导致了快进合并,这很好。然后我做了一个git status,它说:

# On branch mybranch
# Your branch is ahead of 'origin/mybranch' by 6 commits.
#
nothing to commit (working directory clean)
嗯?当我在周末连碰都没碰它的时候,它怎么会提前6个月,而我只是从原点拉过来?因此,我运行了一个
git diff origin/mybranch
,而diff正是我刚刚从远程获取的6个更改

我只能通过运行git fetch origin来“修复”这个问题:

From git@github.com:me/project
af8be00..88b0738  mybranch -> origin/mybranch
显然,我的本地回购协议缺少一些参考对象,但这怎么可能呢?我的意思是,一个pull已经进行了一个fetch,我除了那个分支之外没有做任何工作,所以
git fetch origin
git fetch origin mybranch
应该有相同的结果

我是否应该始终使用
git pull origin
而不是
git pull origin branchname


我很困惑。

git remote-v show在原点时返回什么

如果origin指向github,则状态应该是最新的,而不是在任何远程回购之前。至少,我使用Git1.6.5进行快速测试

无论如何,为了避免这种情况,请明确定义主分支的远程回购:

$ git config branch.master.remote yourGitHubRepo.git
然后,
git-pull-origin-master
,后跟
git-status
应该返回一个干净的状态(前面没有)。
为什么?因为get-fetch-origin-master(包含在git-pull-origin-master中)不仅会更新
fetch\u-HEAD
(如中所述),还会更新本地git存储库中的“远程主分支”。
在这种情况下,您的本地主机似乎不再“领先”远程主机


我可以用git1.6.5测试这一点:

首先,我创建一个workrepo:

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回购

我向我的工作repo添加modif,并将其推送到github repo(作为远程应用程序添加)

我创建了一个家庭回购协议,克隆了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 status确实在“
origin
”前面提到了master geing:

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)
但这仅仅是源代码,而不是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(或者根本没有从github,只定义了一个远程“github”)开始的回购中重复该序列,则状态是干净的:

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来说是干净的。

它可能会对早期的git发出“提前”警告,但无论如何,明确定义的git config branch.master.remote yourGitHubRepo.git
应该能够解决这一问题,即使是早期版本的git。

是否小心添加所有远程设备(原始克隆附带的
origin
除外)使用
git远程添加名称URL
?当它们刚刚被添加到git配置中时,我就看到了这个bug。

git pull
在将显式获取的头(如果没有,则为合并配置的远程分支)合并到当前分支之前,使用适当的参数调用
git fetch

语法:
git fetch
其中
只是一个不带冒号的分支名称,是一个“一次性”提取,它不会对指定远程对象的所有跟踪分支执行标准提取,而是只将命名分支提取到
fetch\u HEAD

更新:对于1.8.4以后的Git版本,如果有一个远程跟踪分支跟踪您请求获取的ref,那么跟踪分支现在将通过
fetch
更新。这一改变是为了避免先前行为造成的混乱


当您执行
git pull
时,
FETCH\u HEAD
会像上面一样更新,然后合并到您签出的
HEAD
中,但是远程存储库的标准跟踪分支都不会更新(git我也注意到了这一点;
git push
似乎也能解决这个问题(报告“所有更新”).
git config--get regexp br.*
可以告诉您的配置是否有本地分支正在跟踪另一个分支。您可以在workRepo中键入
git config branch.master.remote yourGitHubRepo.git
,然后检查(在下一个
git pull origin
)状态是否仍然带有“前进”警告?未设置(空输出)但是,
git remote show origin
向我显示origin指向我的GitHub存储库,所以我想这应该没问题吧?仅git remote(显示GitHub repo的正确地址)是不够的。为了避免出现“
您的分支在前面
”警告消息在
git pull
之后,您还需要首先定义分支的远程名称。因此,我的建议是:键入
git config branch.master.remote yourGitHubRepo.git
,然后尝试
git pull
git status
并查看问题是否仍然存在。感谢您花时间研究此问题。源站删除e已经指向我的GitHub repo。我从GitHub url克隆了该项目,我的本地主分支正在跟踪源/主。至于mybranch,我很确定我是从源/mybranch分支创建的,它应该会自动跟踪它。但是,也许这就是问题所在?本地mybranch没有实际跟踪它
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)
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)
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)