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?

Git にエイリアス機能があるなんて知らなかったんだが

2
Posted at

前置き (読み飛ばしOK)

今いるブランチ名をコピペしたいタイミングが まぁまぁあるんですけども

git branch で ブランチ一覧が見れるのは知ってる。

けど、マージしてないブランチが多すぎて(削除しろw)
& VSCodeのターミナルで見てるから 表示部がすくないのもあり
2ページ、3ページと表示されて 結局今のブランチは 4ページ目なんてことはママある。

Gitのコマンドには 今いるブランチを表示するコマンドはあるのでソレ使えばいいじゃん!!
ごもっとも。以下のがそれ。

git branch --show-current
けど 頭が悪いので ちょっと長くて覚えられない。。

ので チャックン(Chat GPT)に聞いてみたところ
Git 自体にエイリアス機能が標準で用意されていると教えてくれた。

しかも 1回コマンド打つだけで使えるようになる

なんて素敵なコマンド!!
家のPCでも設定するための記事なので コピペできる形式で以下においておく。


結論:これを1回打つだけ

git config --global alias.nb "branch --show-current"

これで以降は:

git nb

と打つだけで、今いるブランチ名が表示される。

なお、nbは now branch の 略である(チャックンが勝手につけた)

以下はチャックくんからの講釈。
供養のためにおいておきます。


そもそも Git のエイリアスって何?

Git には「コマンドの短縮名を作れる機能」が最初からある。

たとえば:

git config --global alias.st status

とすると、

git status

の代わりに:

git st

で同じ意味になる。

bashrc や zshrc は不要。
Git の標準機能だけで完結する。


よく使うと便利なエイリアス例(コピペ用)

とりあえずこの辺入れておくと、かなり楽になる。

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.nb "branch --show-current"

使い方:

git st
git co main
git nb

かなり入力が減る。


実は何が起きているのか

このコマンド:

git config --global alias.nb "branch --show-current"

は、内部的には
~/.gitconfig に設定を書き込んでいる。

実際に見ると:

cat ~/.gitconfig

こんな感じの内容が追加されているはず:

[alias]
    nb = branch --show-current

つまり:

  • bashrc → シェルの設定
  • git alias → Git の設定

という違い。

Git専用のショートカットが作れるイメージ。


エイリアスを一覧で確認する

git config --global --get-regexp alias

これで登録済みのエイリアスが確認できる。


エイリアスを削除したいとき

git config --global --unset alias.nb

これで git nb は使えなくなる。


個人的に一番気に入っているやつ

これ。

git config --global alias.nb "branch --show-current"

git branch --show-current は便利だけど長いので、
git nb くらいの短さになると体感かなり楽。


まとめ

  • Git には エイリアス機能が標準で存在する
  • bashrc や zshrc を触らなくてもOK
  • git config 1回で使える
  • よく使うコマンドほど短縮すると快適になる

「Git に alias 機能あるの知らなかった」という人には、かなりおすすめ。

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?