2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Git】Gitで現在のbranchを環境変数にセットする方法

Last updated at Posted at 2020-04-07

方法

現在developブランチにいるとして下記を実行します。

$ export CURRENT_BRANCH="$(git branch --contains | cut -b 3-)"
$ echo $CURRENT_BRANCH
develop

developブランチが$CURRENT_BRANCHにセットされました。

何をやっているか

$ git branch --contains
* develop

特定のコミットが所属するブランチを探す --contains を使えば、「現在のコミットの所属するブランチ」に絞り込むことができるので、今いるブランチが分かります。(ただし、今のコミットを含むブランチが複数あるときはあんまり意味ないです。当たり前ですが。)

引用

--containsは自動的にHEADを参照します。

これで現在のブランチを出力します。
ただこのままだとカレントブランチを表す*が含まれたままです。

| cut -b 3-

これでパイプで渡された標準出力の3バイト以降を出力します。
* developではd行こうが3バイト目なのでd以降が出力されます。

最後に$()で囲った処理を実行してexoprt CURRENT_BRANCHCURRENT_BRANCHにセットします。

$ export CURRENT_BRANCH="$(git branch --contains | cut -b 3-)"

参考

2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?