0
0

More than 1 year has passed since last update.

`git ls-remote`でブランチの存在も確認したい

Last updated at Posted at 2022-01-04

git ls-remoteでは、リモートブランチを取得するコマンドであり、
これを使うとそもそもリモートブランチが存在する事を確認できる。

3.5 Git のブランチ機能 - リモートブランチ
githubのURLが本当に存在するかシェルスクリプトから確認する

$ REPOSITORY_URL="https://github.com/xxx/xxx"
$ git ls-remote ${REPOSITORY_URL}
XXXXXXXXXXXXXXXXXXXXXXXXX   HEAD
XXXXXXXXXXXXXXXXXXXXXXXXX   refs/heads/master
XXXXXXXXXXXXXXXXXXXXXXXXX   refs/tags/1.4e
YYYYYYYYYYYYYYYYYYYYYYYYY   refs/tags/v1.3
ZZZZZZZZZZZZZZZZZZZZZZZZZ   refs/tags/v1.3b

ブランチ一覧から、確認したいブランチが存在するかを完全一致で確認すればよい。

$ REPOSITORY_URL="https://github.com/xxx/xxx"
$ TARGET_BRANCH="v1.3"
$ git ls-remote ${REPOSITORY_URL} | cut -f 2 | cut -d/ -f 3 | grep --line-regexp "${TARGET_BRANCH}"
v1.3
$ echo $?
0
# `echo $?`の結果、TARGET_BRANCHが存在していれば0(正常終了)
# 存在していなければ1となる。

何か他にもっといい方法があれば教えてほしい、、です。

参考

3.5 Git のブランチ機能 - リモートブランチ
githubのURLが本当に存在するかシェルスクリプトから確認する
Linux:grepコマンドで完全一致する行を表示する方法

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0