分支远程HEAD的奇怪行为

时间:2011-11-18 16:11:23

标签: git

有时候我会收到一些消息,说某个分支是#commit的原点。实际上,我没有创建这些提交,而是从原点撤出。

$> git pull origin develop
Enter passphrase for key '###': 
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From ssh://git.#############
 * branch            develop    -> FETCH_HEAD
Updating bbe5577..71907bc
Fast-forward
 ############### |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)

$> git st
# On branch develop
# Your branch is ahead of 'origin/develop' by 1 commit.

tig的输出:

{date} {author} [develop] {message1}
{date} {author} [origin/develop] {message2}

所以,如果我理解得很好,起源的HEAD是我的本地HEAD(或类似的东西),由1.支持。但没有人将该原点的HEAD修复为-1状态。

为什么会这样?

然后我推到那个分支并获得“一切最新”

$> git push origin develop
Enter passphrase for key '###': 
Everything up-to-date

有人可以解释那个分支发生了什么吗?

1 个答案:

答案 0 :(得分:2)

简短的回答是使用当地开发分支的git fetch origin后跟git merge origin/develop

git pull就像git fetch,后面跟着git merge本地分支开发。您当前的存储库中的origin / develop与您的本地开发分支不匹配,因为您的本地副本是您的origin / develop HEAD之前的一个提交。当git意识到更改已经在远程git repo上时,对git push origin develop的调用会更新您的本地origin / develop。

以下是您在本地机器上看到的内容,没有任何更改:

macbook:test joel$ git push origin master
Everything up-to-date
macbook:test joel$ git status
# On branch master
nothing to commit (working directory clean)
macbook:test joel$

在另一台机器上,另一个用户进行更改,提交并将其推送到origin / master:

otherPerson-pc:test bob$ git status
# On branch master
nothing to commit (working directory clean)
otherPerson-pc:test bob$ echo "ddd" >> newfile.txt 
otherPerson-pc:test bob$ git add newfile.txt 
otherPerson-pc:test bob$ git commit --message "added to newfile in master remotely"
[master d14b77e] added to newfile in master remotely
 1 files changed, 1 insertions(+), 0 deletions(-)
otherPerson-pc:test bob$ git push origin master
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 314 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /Users/joel/Desktop/test/test.git
   2cfa7d4..d14b77e  master -> master
otherPerson-pc:test bob$

回到我的本地副本,我没有做出改变。我从原始主人那里取出来接受其他人的更改,然后出现一个提交!:

macbook:test joel$ git status
# On branch master
nothing to commit (working directory clean)
macbook:test joel$ git pull origin master
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /Users/joel/Desktop/test/test
 * branch            master     -> FETCH_HEAD
Updating 2cfa7d4..d14b77e
Fast-forward
 newfile.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

macbook:test joel$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
macbook:test joel$

我的本​​地主人已经从遥控器的主人应用了更改,但我的原始/主人副本是1提交,所以git说我在本地提前做出一个提交,即使我是最新的。 git push origin master在本地更新origin / master,消息消失。

您可以使用git fetchgit merge代替git pull

来避免这种情况

其他开发者可能会再次这样做:

otherPerson-pc:test bob$ git status
# On branch master
nothing to commit (working directory clean)
otherPerson-pc:test bob$ echo "gggg" >> newfile.txt 
otherPerson-pc:test bob$ git add newfile.txt 
otherPerson-pc:test bob$ git commit --message "added gggg newfile in master remotely"
[master 677d031] added eeee newfile in master remotely
 1 files changed, 1 insertions(+), 0 deletions(-)
otherPerson-pc:test bob$ git push origin master
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 321 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /Users/joel/Desktop/test/test.git
   d14b77e..677d031  master -> master
otherPerson-pc:test bob$

在本地我会git获取所有原点,然后手动将origin / master合并到我的合并副本。

macbook:test joel$ git status
# On branch master
nothing to commit (working directory clean)


macbook:test joel$ git fetch origin 
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /Users/joel/Desktop/test/test
   5e335fa..2dae61c  master     -> origin/master

macbook:test joel$ git status
# On branch master
# Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
#
nothing to commit (working directory clean)

macbook:test joel$ git merge origin/master
Updating 5e335fa..2dae61c
Fast-forward
 newfile.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

macbook:test joel$ git status
# On branch master
nothing to commit (working directory clean)
macbook:test joel$

我通常会进行提取然后合并,因为您可以在使用git diff进行合并之前检查冲突。