Gitでコマンドにalias(=別名)を設定し、爆速でコマンドをたたく
前提
gitについてある程度知っている前提です。 gitとはそもそも何か?ということについてはこちらをご覧ください
なぜalias(=別名)が必要か
例えば、 コンソールでgitを使用している人が一番使うであろうコマンドのこちら
$git status
リポジトリの状態を確認するコマンドですが、ほんとによく使います、、
$git add
から$git push
までの間に3,4回使ったりすることも稀によくあると思います。
使用頻度が高いくせして、'status'と6文字もタイピングする必要があるのは、なかなかめんどくさい。
そこで、別名を付けることで、タイピング量を減らしてやろう!というのが目的です。
それでは見ていきましょう。
aliasの設定
※ 前提(補足)
gitの設定にはスコープ(=設定の有効範囲)があり、次の3つに分かれます。
スコープについての詳しい説明はこちらが大変参考になります。
system | global | local |
---|---|---|
システム単位 | ユーザ単位 | リポジトリ単位 |
今回はglobalスコープにaliasを設定します。
alias設定
ここでは私も使用している設定を例として挙げます。
ちなみにタイトルでは「コマンドにaliasをつける」と言っていますが、実際はコマンド以外にもつけられます。
$git config --global alias.a "add" $git config --global alias.b "branch" $git config --global alias.c "checkout" $git config --global alias.ch "cherry-pick" $git config --global alias.d "diff" $git config --global alias.l "log --graph --all --format='%%x09%%C(cyan bold)%%an%%Creset%%x09%%C(yellow)%%h%%Creset %%C(magenta reverse)%%d%%Creset%%s' --branches" $git config --global alias.rmh "reset --mixed head" $git config --global alias.rhh "reset --hard head" $git config --global alias.s "status"
例だと、status
にはs
というaliasを付けています。
設定は%userprofile%\.gitconfig
に下記のように反映されます。
[alias] a = add b = branch c = checkout ch = cherry-pick d = diff l = log --graph --all --format='%x09%C(cyan bold)%an%Creset%x09%C(yellow)%h%Creset %C(magenta reverse)%d%Creset %s' --branches rmh = reset --mixed head rhh = reset --hard head s = status
動作確認
gitリポジトリで、$git status
と$git s
を試してみます。
同じ結果が得られればOKです。
$git status On branch temp Your branch is up to date with 'origin/temp'. nothing to commit, working tree clean
$git s On branch temp Your branch is up to date with 'origin/temp'. nothing to commit, working tree clean
大丈夫そうですね。
お読みいただきありがとうございました。