作業コピー全体のステータスとインデックスを確認する必要があるため、大規模なリポジトリでは使用git status
または同様の処理が遅くなります。インデックスのみに関心があるため、インデックスの状態をチェックするだけのはるかに高速なコマンドを使用できます。
具体的には、 を使用できますgit ls-files --unmerged
。競合状態のファイルがない場合、そのコマンドは出力を生成せず、存在する場合は次のようなものを生成します。
$ git ls-files --unmerged
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 1 filename
100644 4a58007052a65fbc2fc3f910f2855f45a4058e74 2 filename
100644 65b2df87f7df3aeedef04be96703e55ac19c2cfb 3 filename
したがって、そのファイルが出力を生成するかどうかを確認できます: [[ -z $(git ls-files --unmerged) ]]
. そのコマンドは、リポジトリがクリーンな場合はゼロのリターン コードを返し、リポジトリに競合がある場合はゼロ以外を返します。逆の動作には に-z
置き換えます。-n
以下を に追加できます~/.gitconfig
。
[alias]
conflicts = ![[ -n $(git ls-files --unmerged) ]]
list-conflicts = "!cd ${GIT_PREFIX:-.}; git ls-files --unmerged | cut -f2 | sort -u"
これにより、次のような動作が得られます。
$ git st
# On branch master
nothing to commit (working directory clean)
$ git conflicts && echo 'Conflicts exist' || echo 'No conflicts'
No conflicts
$ git merge other-branch
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.
$ git conflicts && echo 'Conflicts exist' || echo 'No conflicts'
Conflicts exist
$ git list-conflicts
file
( cd ${GIT_PREFIX:-.}
2 番目のエイリアスの部分は、リポジトリ全体ではなく、現在のディレクトリ内の競合するファイルのリストのみを取得することを意味します。)