我是git的新手,我无法理解以下行为:
我有一个bitbucket repo,我在本地克隆,然后我在本地修改和提交,然后我推送到远程仓库。如果我进行隐式推送或“推送起源”,则没有问题。如果我使用显式远程URL推送,我会收到状态消息“你的分支超前于'origin / master'由1 commit'。
似乎git不能将显式地址识别为与原点相同,所以究竟是怎么回事。如果我做另一个隐式推送,它什么都不做,但它确实清除了所述消息。
这是一个示例会话:
baz@bhost:/more/coding/git-tests/ask1$ git --version
git version 1.7.2.5
baz@bhost:/more/coding/git-tests$ git clone https://shishani@bitbucket.org/shishani/dirasi.git ask1
Cloning into ask1...
Password:
remote: Counting objects: 24054, done.
remote: Compressing objects: 100% (6300/6300), done.
remote: Total 24054 (delta 17124), reused 24024 (delta 17106)
Receiving objects: 100% (24054/24054), 11.83 MiB | 251 KiB/s, done.
Resolving deltas: 100% (17124/17124), done.
baz@bhost:/more/coding/git-tests$ cd ask1
baz@bhost:/more/coding/git-tests/ask1$ jed setup.py
baz@bhost:/more/coding/git-tests/ask1$ git commit -a
[master a053f28] modified: setup.py
1 files changed, 1 insertions(+), 0 deletions(-)
baz@bhost:/more/coding/git-tests/ask1$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
baz@bhost:/more/coding/git-tests/ask1$ git remote show origin
Password:
* remote origin
Fetch URL: https://shishani@bitbucket.org/shishani/dirasi.git
Push URL: https://shishani@bitbucket.org/shishani/dirasi.git
HEAD branch: master
Remote branch:
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (fast-forwardable)
baz@bhost:/more/coding/git-tests/ask1$ git push
Password:
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 314 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: bb/acl: shishani is allowed. accepted payload.
To https://shishani@bitbucket.org/shishani/dirasi.git
5e9ffd1..a053f28 master -> master
baz@bhost:/more/coding/git-tests/ask1$ git status
# On branch master
nothing to commit (working directory clean)
baz@bhost:/more/coding/git-tests/ask1$ jed setup.py
baz@bhost:/more/coding/git-tests/ask1$ git commit -a
[master 6d0e236] modified: setup.py
1 files changed, 1 insertions(+), 0 deletions(-)
baz@bhost:/more/coding/git-tests/ask1$ git push https://shishani@bitbucket.org/shishani/dirasi.git master
Password:
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 298 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: bb/acl: shishani is allowed. accepted payload.
To https://shishani@bitbucket.org/shishani/dirasi.git
a053f28..6d0e236 master -> master
baz@bhost:/more/coding/git-tests/ask1$ git status
# On branch master
**# Your branch is ahead of 'origin/master' by 1 commit.** <-- this does not reflect current state
#
nothing to commit (working directory clean)
baz@bhost:/more/coding/git-tests/ask1$ git push
Password:
Everything up-to-date
baz@bhost:/more/coding/git-tests/ask1$ git status
# On branch master
nothing to commit (working directory clean)
答案 0 :(得分:19)
我相信您需要在git
意识到远程端已更新之前将更改恢复。您可以通过快速git pull
执行此操作(或者,如您在示例中所示,对跟踪的来源git push
)。
您也可以手动更改refs/remotes/origin
指向的提交。
期望内容跟踪器识别存储库的任意远程同义词是不公平的;想象一下,如果您有五个不同的URL,它们都是相同的服务器端存储库。当您推送回购A时,这是否意味着您的更改已提交到回购B(分支的来源)? git没办法知道。相反,它只在两种情况下更新远程头引用:拉动和推送到默认目标。
答案 1 :(得分:19)
当状态显示Your branch is ahead of 'origin/master' by 1 commit
时,它实际上意味着origin/master
。也就是说,在您的仓库中有一个名为origin/master
的指针,指向该HEAD
的{{1}}提交,并且您的remote branch
在此提交之前。
对于您的repo识别的所有遥控器,如果您拉/推/取,它会在仓库中创建一个master
指针。它只是一个指向提交的指针,如果你尝试<remote>/<branchname>
那个分支,你只会处于一个分离状态。
显然,当您执行checkout
或git push
时,此指针会更新,但显式推送到网址不会更新该指针,从而更新状态。
如果您在推送到网址后执行git push origin master
或git fetch
,则状态消息也会在此情况下消失。
注意:如果您有多个遥控器,并且您将分支设置为跟踪其他远程分支,例如说git pull
,则您的状态消息在这种情况下将是upstream master
。因此,只有跟踪的远程分支才能进行比较。请参阅Your branch is ahead of 'upstream/master' by 1 commit
以查看当前分支正在跟踪的远程分支。
答案 2 :(得分:8)
是的,在这种情况下,您必须执行git fetch
或git fetch origin
。 Git并不关心你去哪里,只有远程(和远程跟踪分支)在报告你提前多少提交的状态时才是最重要的。
答案 3 :(得分:0)
请注意,自git1.8.1rc1(2012年12月)以来,Git will include hint messages:
if (advice_status_hints)
+ strbuf_addf(sb,
+ _(" (use \"git push\" to publish your local commits)\n"));
当“
git checkout
”检出分支时,它会告诉用户新分支相对于它所构建的远程跟踪分支的距离(或前方)有多远。
该消息现在还建议如何通过推或拉来同步它们。可以使用
advice.statusHints
配置变量禁用此功能。