亀の甲羅2

今日もまた朝とく起きて励まなん窓に明るきありあけの月

Git:リモートにしかないブランチをローカルに持ってくる方法2つ

タダノメモ。誰か(構成管理者、前任者)が作成したブランチを自分が担当したり引き継いだりするシナリオを考える。

目次

本文

1. ローカルにリポジトリが全くない場合(→対処:「特定のブランチだけclone」でいいのでは?)

ブランチ指定せずにcloneするとmasterブランチがcloneされるが、ただのPGがmasterブランチで作業することもないはず(運用にもよるが)。とすれば、任意のブランチだけをcloneしてこようという気になる。

> git clone -b <remote_branch_name> <URL>

とりあえず、この要領で任意のブランチをローカルにcloneできる。開発終わったら(push済)、フォルダごと削除しても構わない。(.git/も不要でしょう)
また、このリポジトリで後述の「ローカルにリポジトリがある場合」の手順にて、他のブランチ(master、developなど)をローカルにcheckoutすることもできる。

2. ローカルにリポジトリがある場合(→対処:「特定のブランチをcheckout」)

◆作業者1の現状確認

現在、master、developおよび追跡ブランチがある状態。(カレントdevelop)

MINGW64 ~/Git/sand2/sandbox (develop)
$ git branch -a
* develop
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/master

◆誰か(構成管理者、前任者)

現在、master、developおよび追跡ブランチがある状態。(カレントdevelop)
新しいブランチを作成してリモートへpushする。

MINGW64 ~/Git/sandbox (develop)
$ git branch -a
* develop
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/master


MINGW64 ~/Git/sandbox (develop)
$ git checkout -b feature/#1234
Switched to a new branch 'feature/#1234'

※ブランチ「feature/#1234」を作成


MINGW64 ~/Git/sandbox (feature/#1234)
$ git push --set-upstream origin feature/#1234
・・・
To https://git-hosting.service/xxx/sandbox.git
 * [new branch]      feature/#1234 -> feature/#1234
Branch 'feature/#1234' set up to track remote branch 'feature/#1234' from 'origin'.

※ブランチ「feature/#1234」をリモートへ送出


MINGW64 ~/Git/sandbox (feature/#1234)
$ git branch -a
  develop
  master
* feature/#1234
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/master
  remotes/origin/feature/#1234

※ブランチ「feature/#1234」を確認

◆作業者1側で誰か(構成管理者、前任者)ブランチを引き継ぐ

  1. git fetch *1
  2. git checkout -b feature/#1234 origin/feature/#1234

リモートの状態をfetchして、-bオプションで(ローカルに)ブランチ作成(+リモートブランチをベースにして)

MINGW64 ~/Git/sand2/sandbox (develop)
$ git fetch
From https://git-hosting.service/xxx/sandbox
 * [new branch]      feature/#1234  -> origin/feature/#1234

※fetchでリモートの状態を取得


MINGW64 ~/Git/sand2/sandbox (develop)
$ git branch -a
* develop
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/master
  remotes/origin/feature/#1234        <- ※

※ブランチを確認。追跡ブランチ「feature/#1234」が取り込まれた。


MINGW64 ~/Git/sand2/sandbox (develop)
$ git checkout -b feature/#1234 origin/feature/#1234
Switched to a new branch 'feature/#1234'
Branch 'feature/#1234' set up to track remote branch 'feature/#1234' from 'origin'.

※リモートのブランチ「feature/#1234」をローカルに作成(以降、「feature/#1234」にcheckout状態)

◆作業者1が編集してリモートへ送出

編集~pushまで。

MINGW64 ~/Git/sand2/sandbox (feature/#1234)
$ vi
.git/      memo.md    test.txt   test2.txt  test3.txt  test4.txt


MINGW64 ~/Git/sand2/sandbox (feature/#1234)
$ vi memo.md

※任意のファイルを編集し保存


MINGW64 ~/Git/sand2/sandbox (feature/#1234)
$ git add .

※ステージに上げる


MINGW64 ~/Git/sand2/sandbox (feature/#1234)
$ git commit -m 'test'
[feature/#1234 ed295b2] test
 1 file changed, 2 insertions(+), 2 deletions(-)

※コミット


MINGW64 ~/Git/sand2/sandbox (feature/#1234)
$ git push
・・・
To https://git-hosting.service/xxx/sandbox.git
   055d283..ed295b2  feature/#1234 -> feature/#1234

※送出、追跡ブランチが設定済み(fetchしたときに)なので、--set-upstreamしなくてOK

 


まずはこんな感じかな。

*1:ブランチ名が分かっているなら、「git fetch origin <branch-name>」のコマンドでリモートブランチから指定したブランチだけリモート追跡ブランチを作成することもできる。ブランチ指定をしなければ、すべてのリモートブランチに対するリモート追跡ブランチを作成・更新(フェッチ)する