LoginSignup
0
0

More than 3 years have passed since last update.

【Shell】Gitでリモートのデフォルトブランチを取得する方法

Posted at
  • 普段Gitを用いた開発の際に、shellやCIでデフォルトブランチ名を取得した処理を記述する場合がある。
  • ただ、最近諸事情でデフォルト名がmasterからmainへ変更されたため、汎用的に取得する必要がある。
  • そのため、Gitソースのリモートデフォルトブランチ名を取得する2つの方法を記録する。

結果

  • git remoteコマンドを利用した主に2つのやり方は下記。
    • 環境に合わせて選択
# awkの場合
# 出力 : master(or main)
git remote show origin | awk '/HEAD/ {print $NF}'

# grep / cutの場合
# 出力 : master(or main)
git remote show origin | grep 'HEAD branch' | cut -d' ' -f5

参考

0
0
0

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