絶賛お勉強中のGitについてですが、今回整理したいのはいわゆる
git push origin master
のorigin や
git branch --set-upstream-to origin/master master
にでてくる origin/master 、ローカルブランチとリモートブランチなどの件です。 オリジンってなんなのとか、 スラつけて書いたり書かなかったり、なんてのがとてもややこしいですよね。
ここでは、git clone してから git push するまで、どのようなことが起こっているかを整理しつつ、originとか origin/master とかってナンなのってのをまとめたいと思います
まず git clone したとき、ローカルのディレクトリには
が作成されます。origin/master はサーバにあるブランチだと漠然と思っていたのですが、どうもリモートのリポジトリのブランチを指し示す特別な、 'ローカルの' ブランチのようです。このことをリモートブランチと呼んでたりするんでややこしいです。
この「origin」には cloneした先のリポジトリサーバのURLが自動的に設定されていまます。
$ git config --list .... remote.origin.url=https://github.com/xxxx/hello.git
こんな感じです
ちなみに、このリモートリポジトリのサーバ名は自由に追加することができます
$ git remote origin $ git remote add teamone https://github.com/xxxx/hello.git 追加(ちなみに削除はremove) $ git remote origin teamone $ git config --list remote.origin.url=https://github.com/xxxx/hello.git remote.teamone.url=https://github.com/masatomix/helloworld1.git
さて、リモートのリポジトリをcloneしたとき、ローカルには
が作成されました。このリモートブランチは originサーバを追跡する特別なブランチで、直接自分では操作できません。
さて、リモートリポジトリのブランチは他人がpushすることで、だんだん手元のリモートブランチとは内容がズレてくるわけですが、それを同期するコマンドがgit fetch origin master です。fetch コマンドで、リモートリポジトリのブランチの内容を、ローカルのoriginサーバを追跡しているブランチに反映させます。
fetchだけでなく pull や pushも含めて図にするとこんな感じでしょうか。
上記のような、複数のリモートリポジトリを用いる際も、origin とか upstreamとか名前をつけることで、落ち着いて
$ git fetch upstream master <- upstream/master に upstreamリポジトリの変更分を更新する $ git pull upstream master <- upstream/master とローカルのmasterブランチに、upstreamリポジトリの変更分を反映させる
などとすることができます。ちなみに構築方法は以下の通り:
$ git clone https://github.com/xxxxxx/Spoon-Knife.git Cloning into 'Spoon-Knife'... remote: Counting objects: 16, done. remote: Total 16 (delta 0), reused 0 (delta 0), pack-reused 16 Unpacking objects: 100% (16/16), done.
$ cd Spoon-Knife/ $ git branch -vv * master d0dd1f6 [origin/master] Pointing to the guide for forking
$ git remote add upstream https://github.com/octocat/Spoon-Knife.git $ git remote origin upstream $ git config --list remote.origin.url=https://github.com/xxxxxx/Spoon-Knife.git remote.upstream.url=https://github.com/octocat/Spoon-Knife.git
$ git fetch upstream master From https://github.com/octocat/Spoon-Knife * branch master -> FETCH_HEAD * [new branch] master -> upstream/master $ git pull upstream master From https://github.com/octocat/Spoon-Knife * branch master -> FETCH_HEAD Already up-to-date. $
この記事は
現在のアクセス:6934