4
1

More than 5 years have passed since last update.

'git st'が遅いときの対処方法 -- Windows版 --

Last updated at Posted at 2019-08-27

TL;DR

Windows版では、gitのaliasを使っていると展開に時間がかかることがあります。
shellのaliasを使いましょう。

'git status'は打ちにくい

入力補完では候補が複数あって絞りきれません。

$ git sta
stage    stash    status

aliasを使おう

.gitconfigのにaliasを設定します。

~/.gitconfig
[alias]
    st = status

なんか遅い

いざ実行してみるとなんか遅い。なんかというか、実行するたびにちょっと固まっているレベルで遅い。
gitコマンドがどのように実行されているかを確認するためのGIT_TRACEという環境変数があるようです。今回調べていて初めて知りました。

$ GIT_TRACE=1 GIT_TRACE_PERFORMANCE=1 git st
21:43:46.036879 git.c:670               trace: exec: git-st
21:43:46.037550 run-command.c:643       trace: run_command: git-st
21:43:48.335997 git.c:366               trace: alias expansion: st => status
21:43:48.336520 git.c:670               trace: exec: git-status
21:43:48.336885 run-command.c:643       trace: run_command: git-status
21:43:48.356590 git.c:419               trace: built-in: git status
21:43:48.362629 read-cache.c:2256       performance: 0.003905200 s:  read cache .git/index
21:43:48.364862 read-cache.c:1574       performance: 0.000159200 s:  refresh index
21:43:48.368519 diff-lib.c:251          performance: 0.000017500 s:  diff-files
21:43:48.377368 name-hash.c:594         performance: 0.000015600 s:   initialize name hash
21:43:48.378452 dir.c:2282              performance: 0.001283200 s:  read directory
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   main.c

21:43:48.396978 trace.c:477             performance: 0.042009300 s: git command: /usr/libexec/git-core/git-status
21:43:48.399018 trace.c:477             performance: 2.366321700 s: git command: git st

git stのたびに2秒も待たされるのはつらいので、原因調査します。

gitの問題ではない

巨大なリポジトリに対するgitの速度問題への対応策として、git gcなどがありますが、git status自体が遅いわけではないので、今回は当てはまらないようです。

Windows版のgitでは、Windowsプロセスとgitとの橋渡し部分がネックになりやすいという記事を見つけました。
どうやら今回の原因もこれっぽいです。

bashの関数を使う

gitのaliasを使うと遅いということで、bashにaliasを設定する方向で考えます。

普通にbashのaliasを使おうとするとスペースが使えないので、gitのaliasとは使用感が違ってきます。
その場合、bashの関数を使うことで同じ使用感でコマンドのレスポンスを向上できます。

func_git()
{
    if [ $1 == "st" ]; then
        command git status "${@:2}"
    else
        command git "$@"
    fi
}

alias git=func_git

これも今回調べて初めて使ったのですが、"${@:<start>:<end>}"で引数のsliceができるみたいですね。これを使えば他にも便利なaliasが作れそうですね。

4
1
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
4
1