- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2023-05-09T08:36:25+00:00","","")
#author("2023-05-17T07:52:08+00:00","","")
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
#contents
絶賛お勉強中の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 [#a9d81a8a]
まず git clone したとき、ローカルのディレクトリには
- いわゆるローカルブランチ master
- リモートのリポジトリを参照する リモートブランチ origin/master
が作成されます。origin/master はサーバにあるブランチだと漠然と思っていたのですが、どうもリモートのリポジトリのブランチを指し示す特別な、 'ローカルの' ブランチのようです。このことをリモートブランチと呼んでたりするんでややこしいです。
#ref(01.png)
この「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
** git fetch/pull/push [#sdbaa091]
さて、リモートのリポジトリをcloneしたとき、ローカルには
- ローカルブランチ master
- リモートブランチ origin/master
が作成されました。このリモートブランチは originサーバを追跡する特別なブランチで、直接自分では操作できません。
さて、リモートリポジトリのブランチは他人がpushすることで、だんだん手元のリモートブランチとは内容がズレてくるわけですが、それを同期するコマンドがgit fetch origin master です。fetch コマンドで、リモートリポジトリのブランチの内容を、ローカルのoriginサーバを追跡しているブランチに反映させます。
fetchだけでなく pull や pushも含めて図にするとこんな感じでしょうか。
#ref(02.png)
** 他のサーバからfetch/pull する [#p4aa6c72]
#ref(03.png)
上記のような、複数のリモートリポジトリを用いる際も、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.
$
**関連リンク [#h2dca419]
-[[Git - リモートブランチ>https://git-scm.com/book/ja/v2/Git-%E3%81%AE%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81%E6%A9%9F%E8%83%BD-%E3%83%AA%E3%83%A2%E3%83%BC%E3%83%88%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81]]
-[[GitHubへpull requestする際のベストプラクティス - hnwの日記>http://d.hatena.ne.jp/hnw/20110528]]
----
この記事は
#vote(おもしろかった[22],そうでもない[0])
#vote(おもしろかった[23],そうでもない[0])
#comment
#topicpath
SIZE(10){現在のアクセス:&counter;}