Gitのaliasで、よく使うコマンドに短い名前を付ける方法
git config --global alias.st status のように設定すると、git st と打つだけで git status が動くようになります。git log --oneline --graph --all のような長いコマンドを毎回打つのが面倒なときや、ターミナルでの Git の操作を少しでも短くしたいときに使う設定です。検証環境で git を実際に打ち、alias を作る前と後の出力をそのまま載せています。
検証環境は Ubuntu 24.04、Git 2.43.0 です。作者の名前とメールアドレスには例示用の値(Neko <neko@example.com>)を設定し、コミットの日時は環境変数で決めました。システム全体の設定ファイルは読まない状態(GIT_CONFIG_NOSYSTEM=1)で、ページャーは使わない設定にしています。Windows・macOS での動作や、bash 以外のシェルでの書き方は試していません。
alias を作る前の git st
alias を何も作っていない状態で git st と打つと、そういうコマンドはないと言われます。似た名前のコマンドの候補は出ますが、実行はされません。
$ git st
git: 'st' is not a git command. See 'git --help'.
The most similar commands are
status
reset
stage
stash
Pro Git の「Git エイリアス」の節にも、Git はコマンドの一部だけを打ってもそのコマンドを推測しない、と書かれています。短い名前で動かしたいなら、自分で alias を作ります。
git config で alias.名前 を設定する
alias は git config で alias.<短い名前> という設定として作ります。--global を付けると、そのユーザーのすべてのリポジトリで使えます。
$ git config --global alias.st status
$ git st
On branch main
nothing to commit, working tree clean
git st の出力は、git status と同じものになりました。alias のあとに付けた引数は、そのまま元のコマンドに渡されます。たとえば git st -s は git status -s と同じで、短い形式で表示されます(このあとの unstage の例で使っています)。
オプション付きのコマンドに名前を付ける
オプションまで含めて1つの名前にしたいときは、値全体を引用符で囲みます。履歴をグラフで1行ずつ見るコマンドを lg にしてみます。
$ git config --global alias.lg "log --oneline --graph --all"
$ git lg
* 7e6795a トップ画面を追加
* b6eccb8 ログイン画面を追加
* 6b9961f 最初のコミット
$ git lg -2
* 7e6795a トップ画面を追加
* b6eccb8 ログイン画面を追加
git lg -2 のように引数を足すと、alias の中身のうしろに付け足されます。ここでは git log --oneline --graph --all -2 として動き、2件だけ表示されました。
Pro Git に例として載っている last(最後のコミットを見る)も作ってみます。
$ git config --global alias.last "log -1 HEAD"
$ git last
commit 7e6795a402aa1c1620a9aac9f3e3b149c1020f39
Author: Neko <neko@example.com>
Date: Thu Nov 12 10:20:00 2026 +0900
トップ画面を追加
覚えやすい名前のコマンドを作る
alias は、覚えにくい操作に分かりやすい名前を付けるのにも使えます。ステージを取り消す git restore --staged -- に、unstage という名前を付けました。
$ git config --global alias.unstage "restore --staged --"
$ printf "猫の写真を集めるアプリです。\n" >> README.md
$ git add README.md
$ git st -s
M README.md
$ git unstage README.md
$ git st -s
M README.md
git status のリファレンスにあるとおり、短い形式の左の列はインデックス(ステージ)、右の列は作業ツリーの状態です。git unstage README.md の前は M が左の列にあり、後は右の列に移っています。ファイルの中身はそのままで、ステージだけが取り消されました。
作った alias を確かめる
ある alias が何に置き換わるかは、git help <alias名> で確かめられます。
$ git help st
'st' is aliased to 'status'
$ git help lg
'lg' is aliased to 'log --oneline --graph --all'
作った alias をまとめて見るには、git config --get-regexp で alias. から始まる設定を探します。--show-scope を付けると、どの設定ファイルに入っているか(global か、リポジトリの local か)も並べて表示されます。
$ git config --global --get-regexp "^alias\."
alias.st status
alias.lg log --oneline --graph --all
alias.last log -1 HEAD
alias.unstage restore --staged --
$ git config --show-scope --get-regexp "^alias\."
global alias.st status
global alias.lg log --oneline --graph --all
global alias.last log -1 HEAD
global alias.unstage restore --staged --
alias を作る前に同じ git config --global --get-regexp "^alias\." を実行したときは、何も表示されませんでした。
--global で作った alias は、git config のリファレンスにあるとおり、ホームフォルダの ~/.gitconfig に書き込まれます。ファイルを開くと [alias] の中に並んでいます。
$ cat ~/.gitconfig
[init]
defaultBranch = main
[user]
name = Neko
email = neko@example.com
[alias]
st = status
lg = log --oneline --graph --all
last = log -1 HEAD
unstage = restore --staged --
既存のコマンドと同じ名前は使えない
git status をいつも短い形式で出したくて、status という名前の alias を作っても効きません。
$ git config --global alias.status "status -s"
$ git config --global --get alias.status
status -s
$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
設定は保存されていますが、git status は元のままの長い形式で表示されました。git config のリファレンスにある alias.* の説明に、混乱やスクリプトでの問題を避けるため、既存の Git のコマンドを隠す alias は無視すると書かれています。この alias は git config --global --unset alias.status で消しました。
中身を打ち間違えたとき
alias の中身のコマンド名を間違えると、alias を使ったときに初めてエラーになります。switch を swich と打ち間違えた場合です。
$ git config --global alias.sw swich
$ git sw main
expansion of alias 'sw' failed; 'swich' is not a git command
$ git config --global alias.sw switch
$ git sw main
Already on 'main'
M README.md
expansion of alias 'sw' failed と、どの alias の中身がおかしいかが表示されます。同じ名前でもう一度 git config を実行すると、値が上書きされて直ります。
! で始めるとシェルのコマンドを動かせる
値を ! で始めると、Git のサブコマンドではなく、シェルのコマンドとして実行されます。中に " や $ を書くことが多いので、値全体をシングルクォートで囲むと書きやすくなります。
なお、! で始まる alias は、リポジトリの一番上のフォルダで実行されます。サブフォルダ src の中で、ファイルの一覧を出す alias を使ってみました。cd src && 〜 の行は、どれもリポジトリの一番上から src に移って実行したものです(1行ずつ一番上に戻ってから打っています)。
$ git config --global alias.files '!ls'
$ cd src && ls
login.txt
top.txt
$ cd src && git files
README.md
src
src の中で ls を打つと src の中身が出ますが、git files ではリポジトリの一番上の中身が出ました。git config のリファレンスにも、シェルのコマンドはリポジトリの一番上のフォルダで実行され、今いるフォルダとは限らないと書かれています。
元のフォルダは、環境変数 GIT_PREFIX に一番上からの相対パスとして入っています。そこへ移ってから実行するように書き直すと、src の中身が出るようになりました。一番上のフォルダで git files を実行すると、これまでどおり一番上の中身が出ます。${GIT_PREFIX:-.} は、GIT_PREFIX が空のときに .(今のフォルダ)を使うためのシェルの書き方です。
$ git config --global alias.files '!cd -- "${GIT_PREFIX:-.}" && ls'
$ git config --global --get alias.files
!cd -- "${GIT_PREFIX:-.}" && ls
$ cd src && git files
login.txt
top.txt
$ git files
README.md
src
引数を途中に差し込む
! の alias でも、付けた引数はコマンドの最後に足されます。echo で確かめると、login が 引数: のうしろに付きました。
$ git config --global alias.say '!echo 引数:'
$ git say login
引数: login
引数をコマンドの途中に入れたいときは、シェルの関数を定義してすぐ呼ぶ書き方を使い、関数の中で $1 として受け取ります。この書き方は、最新の git config のリファレンスでも勧められています。git feat login で feature/login というブランチを作る alias です。
$ git config --global alias.feat '!f() { git switch -c "feature/$1"; }; f'
$ git config --global --get alias.feat
!f() { git switch -c "feature/$1"; }; f
$ git feat login
Switched to a new branch 'feature/login'
$ git branch
* feature/login
main
このリポジトリだけで使う alias
--global を付けずにリポジトリの中で git config を実行すると、alias はそのリポジトリの .git/config に入り、そこでだけ使えます。
$ git config alias.s "status -s"
$ git s
M README.md
$ git config --show-scope --get-regexp "^alias\."
global alias.st status
global alias.lg log --oneline --graph --all
global alias.last log -1 HEAD
global alias.unstage restore --staged --
global alias.sw switch
global alias.files !cd -- "${GIT_PREFIX:-.}" && ls
global alias.say !echo 引数:
global alias.feat !f() { git switch -c "feature/$1"; }; f
local alias.s status -s
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[alias]
s = status -s
一覧では、s だけが local と表示されています。
いらなくなった alias を消す
alias を消すには、作ったときと同じ場所(--global の有無)を指定して --unset を付けます。
$ git config --global --unset alias.lg
$ git lg
git: 'lg' is not a git command. See 'git --help'.
The most similar command is
log
$ git config --show-scope --get-regexp "^alias\."
global alias.st status
global alias.last log -1 HEAD
global alias.unstage restore --staged --
global alias.sw switch
global alias.files !cd -- "${GIT_PREFIX:-.}" && ls
global alias.say !echo 引数:
global alias.feat !f() { git switch -c "feature/$1"; }; f
local alias.s status -s
git lg は、alias を作る前の git st と同じエラーに戻り、一覧からも lg が消えました。





