sumarsono.com
Take it with a grain of salt


Git checkout remote branch yang gak ada di lokal

Posted on

Bukankah tinggal git checkout nama-branch? Nope. Biar ku jelaskan kasusnya krn ini masalah yang spesifik. Aku punya repo [email protected]:nalakawula/dummy.git. Disana ada 3 branch: master, develop, dan staging. Suatu waktu, aku hanya clone satu branch saja, branch develop. Menggunakan perintah git clone --single-branch --branch develop [email protected]:nalakawula/dummy.git. Seiring berjalannya waktu, aku ada keperluan di branch staging. Ketika aku checkout branch staging di lokal:

[semut@semut-pc dummy]$ git checkout staging
error: pathspec 'staging' did not match any file(s) known to git

Hmm, check semua branch:

[semut@semut-pc dummy]$ git branch -a
* develop
  remotes/origin/develop

Ternyata, branch staging tidak terlist. Coba git fetch kemudian check lagi ternyata branch staging masih nihil. Padahal di repo jelas ada.

Solusinya adalah Cek git config,

[semut@semut-pc dummy]$ git config --list | grep fetch
remote.origin.fetch=+refs/heads/develop:refs/remotes/origin/develop

I see, ternyata repo lokalku hanya ngefetch satu branch doang efek pas clone pakai opsi --single-branch.

Aku harus ganti confignya biar fetch semuanya,

[semut@semut-pc dummy]$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'

Coba fetch lagi

[semut@semut-pc dummy]$ git fetch
From github.com:nalakawula/dummy
 * [new branch]      master     -> origin/master
 * [new branch]      staging    -> origin/staging

Sekarang aku yakin bisa checkou branch staging

[semut@semut-pc dummy]$ git checkout staging
Branch 'staging' set up to track remote branch 'staging' from 'origin'.
Switched to a new branch 'staging'

Cek hasilnya

[semut@semut-pc dummy]$ git branch -a
  develop
* staging
  remotes/origin/develop
  remotes/origin/master
  remotes/origin/staging

Sekarang aku sudah berada di branch staging.

Cool~