LoginSignup
2
0

More than 1 year has passed since last update.

Gitのサブコマンド風エイリアスを作成したい

Last updated at Posted at 2022-03-25

はじめに

  • Gitの各種サブコマンドをショートカットしたコマンドを呼び出したい

パッと思いつくのはailiasでしょうか

.bashrc
alias gitco = "git commit"

ただ、git coのようなエイリアスはサブコマンドとして処理されてしまうためできません

解決法

git-hogeというファイル名で作成した、スクリプトのパスを通しておくとgit hogeで実行してくれます

例えば

~/.bin/git-co
#!/bin/bash
git commit $@

# $@で このコマンドが呼び出されたときの引数をすべて取得できます

作成したらbashなどのシェルで実行権限との付与とパスを通します

$ chmod +u ~/.bin/git-co
$ export PATH="$PATH:$HOME/.bin/"

これでgit cogit checkoutのエイリアスとして実行できます。

Tips

git-hogeはシェルスクリプトとして書けるのでシバンをPythonといったインタプリタに指定して記述できます!

もっとエレガントな解決法

Gitはaliasを組む機能があるので1単純なエイリアスを組みたい場合

$ git config --global alias.co checkout

これで完結します。(オチ)

参考文献

  1. https://git-scm.com/book/ja/v2/Git-%E3%81%AE%E5%9F%BA%E6%9C%AC-Git-%E3%82%A8%E3%82%A4%E3%83%AA%E3%82%A2%E3%82%B9

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