「git pullorigin mybranch」は、ローカル mybranch のコミットをオリジンよりも N 個先に残します。なぜ?
-
20-09-2019 - |
質問
奇妙なことに気づきました git pull
, 、わかりません。
金曜日、私は地元の支店で働きました。それを呼びましょう mybranch
. 。オフィスを出る前に、これをオリジン (私の github リポジトリ) にプッシュしました。 git push origin mybranch
.
昨日、家で私は、 pull
mybranch をラップトップに編集し、さらにコーディングを行ってから、変更を github (オリジン) にプッシュして戻しました。
今、私は再び仕事に就いており、昨日からの変更を仕事用マシンにプルしようとしました (週末の間、職場のローカル リポジトリには何も変更しませんでした)。
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
差分は、リモートから取得したばかりの 6 つの変更とまったく同じでした。
これを「修正」するには実行するしかありません git fetch origin
:
From git@github.com:me/project
af8be00..88b0738 mybranch -> origin/mybranch
どうやらローカル リポジトリに参照オブジェクトがいくつか欠けていたようですが、どうしてそうなるのでしょうか?つまり、プルはすでにフェッチを行っており、そのブランチ以外は何も作業していませんでした。 git fetch origin
そして git fetch origin mybranch
同じ結果になるはずですか?
いつも使ったほうがいいですか git pull origin
の代わりに git pull origin branchname
?
私は混乱しています。
解決
git pull
電話 git 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 pull
私よりもう少し多いなら、プレーンをやります git pull
ほとんどの場合パラメータなしで、依存します branch.<branch>.remote
そして branch.<branch>.merge
「正しいことをする」こと。
他のヒント
どのようなリターンをgit remote -v show
のでしょうか?
、ステータスが最新ではなく、先に任意のリモートレポのでなければなりません。少なくとも、Git1.6.5と私は簡単なテストのために使用しています。
とにかく、これを回避するために、明示的にマスターブランチのリモートリポジトリを定義します:
$ git config branch.master.remote yourGitHubRepo.git
その後、git pull origin master
続いgit status
は、(何も先に)きれいな状態を返すべきではありません。
どうして? (gitのプル原点マスターに含まれる)フェッチを取得原点マスターはちょうど(FETCH_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のレポをシミュレート
PS D:\git\tests\pullahead\workrepo> cd ..
PS D:\git\tests\pullahead> git clone --bare workrepo github
私はgithubのレポにプッシュすることを、私の作業レポにMODIFを追加します(リモートとして追加)
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のステータスは、先に 'origin
' のマスター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)
しかし、それは唯一の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の」が定義されてか、まったく原点)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
を持っていた場合は、、status
はgit1.6.5のためのきれいだろう。
これは、以前のgitのために「先に」警告かもしれませんが、とにかく、明示的に定義されたgit config branch.master.remote yourGitHubRepo.git
も、Gitリポジトリの初期のバージョンでは、それの世話をすることができる必要があります。
あなたは、リモートのすべてorigin
を使用して(あなたの元のクローンが付属していますgit remote add NAME URL
を除く)を追加するように注意していますか?私は、彼らはただのgitの設定に追加されてきたときに、このバグを見てきました。