Visual Studio Community 2022 で使用している Git をコマンドプロンプトで使用できるようにする

技術情報
公開 2024年10月20日 最終更新 2025年10月20日
Article Image

はじめに

別途インストールするのが面倒だったので、Visual Studio が内部で使用している git をコマンドラインから使いたいと思いました。
そこで where git で git.exe の場所を確認しました。

PS C:\hoge> where git
PS C:\hoge>

何も表示されませんでした・・・。
コマンドプロンプトから where git を叩いてみます。

C:\Users\koro>where git
C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\cmd\git.exe

こちらはパスが表示されました。
このパスのフォルダを環境変数 Path に追加すればコマンドプロントからも PowerShell からも git が使用できるようになります。

git -v
git version 2.45.2.windows.1
git -v
git version 2.45.2.windows.1

Get-Command(gcm)

git が使えるようになったのは良いのですが、PowerShell でパスが表示されなかった原因はなんでしょうか。
コマンドレットの Get-Command(gcm) を使うと git を見つけてくれました。

PS C:\hoge> gcm git

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     git.exe                                            2.50.1.1   C:\Program Files\Microsoft Visual Studio…

パスをすべて取得するには以下のように実行すれば OK です。

(gcm git).Source
C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\cmd\git.exe

PowerShell で実行形式を探すには where ではなく Get-Command(gcm) を使用したほうがよさそうです。