91

git pull私はちょうど私が理解していない、について奇妙な何かを観察しました。

金曜日に、私は地元の支店で働きました。それを呼びましょうmybranch。オフィスを離れる前に、私はそれを元の場所にプッシュしました(これは私のgithubリポジトリです)git push origin mybranch:。

昨日自宅で、pullmybranchをラップトップに編集し、さらにコーディングを行ってから、変更をgithub(origin)にプッシュしました。

今、私は再び仕事をしていて、昨日から自分の作業マシンに変更をプルしようとしました(週末に職場のローカルリポジトリで何も変更しませんでした):

git pull origin mybranch

これにより早送りマージが発生しましたが、これは問題ありません。それから私はをしました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そして、diffsは私がちょうどリモートから引っ張った6つの変更でした。

私はこれを実行することによってのみ「修正」することができましたgit fetch origin

From git@github.com:me/project
af8be00..88b0738  mybranch -> origin/mybranch

どうやら、私のローカルリポジトリにはいくつかの参照オブジェクトがありませんでしたが、どうすればそれができますか?つまり、プルはすでにフェッチを実行し、そのブランチ以外は何も処理しなかったので、git fetch origingit fetch origin mybranchは同じ結果になるはずですか?

git pull origin代わりに常に使用する必要がありgit pull origin branchnameますか?

よくわかりません。

4

3 に答える 3

116

git pullgit fetch明示的にフェッチされたヘッドをマージする前に(または、マージ用に構成されたリモートブランチがない場合は)、適切なパラメーターを使用して呼び出します。

構文:git fetch <repository> <ref>ここで<ref>、コロンのない単なるブランチ名は、指定されたリモートの追跡されたすべてのブランチの標準フェッチを実行せず、代わりに名前付きブランチのみをにフェッチする「ワンショット」フェッチですFETCH_HEAD

更新: 1.8.4以降のGitバージョンの場合、フェッチするように要求した参照を追跡するリモート追跡ブランチがある場合、追跡ブランチはによって更新されfetchます。この変更は、以前の動作が引き起こした混乱を避けるために特別に行われました。

を実行するgit pull <repository> <ref>と、FETCH_HEAD上記のように更新され、チェックアウトされたものにマージされますHEADが、リモートリポジトリの標準の追跡ブランチは更新されません(Git <1.8.4)。これは、ローカルではリモートブランチよりも進んでいるように見えますが、実際には最新の状態になっていることを意味します。

個人的には、マージする前に強制更新に関する警告が表示され、マージしているものをプレビューできるため、常にgit fetchフォローしています。使用量が少し多い場合は、ほとんどパラメーターのないプレーンを実行します。当時の、 「正しいことをする」ことに依存している。git merge <remote>/<branch>git pullgit pullbranch.<branch>.remotebranch.<branch>.merge

于 2009-11-16T18:08:24.517 に答える
3

git remote -v show起源に関しては何が返されますか?

オリジンがgithubを指している場合、ステータスは最新であり、リモートリポジトリよりも前ではありません。少なくとも、Git1.6.5では、簡単なテストに使用しています。

とにかく、これを回避するには、マスターブランチのリモートリポジトリを明示的に定義します。

$ git config branch.master.remote yourGitHubRepo.git

次に、、のgit pull origin mastergit statusにクリーンステータスを返す必要があります(先にありません)。
なんで?get fetch origin master(git pull origin masterに含まれている)はFETCH_HEADCharles Baileyが彼の回答で説明しているように)更新するだけでなく、ローカル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レポジトリをシミュレートします

PS D:\git\tests\pullahead\workrepo> cd ..
PS D:\git\tests\pullahead> git clone --bare workrepo github

作業リポジトリにmodifを追加し、それを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 statusは、マスターが' 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)

しかし、それはorigingithubではありません。

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」が定義されている)リポジトリでシーケンスを繰り返すと、ステータスはクリーンになります。

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を指しているだけの場合githubstatusgit1.6.5ではクリーンになります。
以前のgitには「先行」警告が表示される場合がありますが、とにかく、git config branch.master.remote yourGitHubRepo.gitGitの初期バージョンであっても、明示的に定義されたものがそれを処理できるはずです。

于 2009-11-16T12:48:53.407 に答える
2

originを使用してすべてのリモコン(元のクローンに付属しているものを除く)を追加するように注意していますgit remote add NAME URLか?このバグは、gitconfigに追加されたばかりのときに見たものです。

于 2009-11-16T14:14:15.460 に答える