LoginSignup
0
0

20240327 gitのエイリアス、先日の改良

Last updated at Posted at 2024-03-27

Gitの小技

あるブランチがマージ済みかどうか、の改善

一昨日かいた「20240325 gitあれこれ、プロンプト、ブランチへのpush禁止、aliasに引数 #Git - Qiita」、このエイリアスは大変無駄がおおかった
git branch --mergedをつかうともっと短い

in = "!git branch --merged | grep $1 >>/dev/null && echo $1 is merged. || echo $1 is NOT merged. #"

今いるブランチ名取得

git rev-parse --abbrev-ref HEAD

alias.nowでエイリアス作っておくと楽ちん

マージ済みローカル/リモートブランチを一括削除

うえで作ったalias.nowを再利用

ローカルブランチの一括削除
  del-in = "!for b in $(git branch --merged | grep -v $(git now)); do git branch -d $b; done #"
リモートブランチの一括削除(引数にリモートリポジトリをとる)
  del-remote-in = "!if [ -z \"$1\" ]; then repo=origin;else repo=$1; fi; for b in $(git branch -a --merged | grep -v $(git now) | grep remotes | cut -d'/' -f3) ; do git push $repo :$b; done #"

これ、最初 if [[ -z $1 ]]とやっていたが、[[: not foundというエラーが出て困っていた
じつはif [[...]]の書き方はbashの記法で、ここではif [...]と書く必要がある
参考:shell - String comparison in bash. [[: not found - Stack Overflow

もっと短く書けないのかな

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