この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:5
ページ更新者:T
更新日時:2026-06-11 07:10:02

タイトル: git本体のインストール(Linux)
SEOタイトル: Linux 各ディストリビューションへの git インストール完全ガイド

この記事の要点
  • Ubuntu/Debian: sudo apt install git、RHEL/Rocky/AlmaLinux: sudo dnf install git、Arch: sudo pacman -S git
  • 最新版がほしい場合: Ubuntu は PPA git-core/ppa、RHEL 系は IUS、またはソースビルド
  • インストール後の必須初期化: git config --global user.name / user.email / init.defaultBranch main
  • 認証: HTTPS は Personal Access Token (PAT)、SSH は鍵認証。Git Credential Helper で資格情報キャッシュ
  • 推奨バージョン: 2.40+(partial clone, scalar など最新機能)。git --version で確認

パッケージマネージャでインストール

各ディストロの公式リポジトリから入れるのが最も簡単で安全です。ただしバージョンはやや古いことが多いです:

Ubuntu / Debian

sudo apt update
sudo apt install -y git

git --version
# git version 2.43.0 など

CentOS / RHEL / Rocky / AlmaLinux

# RHEL 8 / 9 系
sudo dnf install -y git

# CentOS 7 系(古い、yum)
sudo yum install -y git

git --version

Fedora

sudo dnf install -y git
# Fedora は dnf がデフォルト、比較的新しい git が入る

Arch / Manjaro

sudo pacman -Sy git
# Arch は常にローリングリリースで最新

openSUSE

sudo zypper install -y git

Alpine

sudo apk add git
# Docker の Alpine ベースイメージで頻出

最新版を入れたい場合

Ubuntu: git-core PPA

Ubuntu 公式の git はリリース時点で固定されており、半年〜1 年遅れになることがあります。最新が欲しいなら PPA:

sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install -y git

git --version
# git version 2.45.x 等の最新が入る

RHEL / Rocky: IUS リポジトリ

# EPEL + IUS 追加
sudo dnf install -y epel-release
sudo dnf install -y https://repo.ius.io/ius-release-el8.rpm

sudo dnf install -y git236   # git 2.36 系を入れる例

ソースからビルド(最新を必ず)

# 依存パッケージ
# Ubuntu
sudo apt install -y dh-autoreconf libcurl4-gnutls-dev libexpat1-dev \
    gettext libz-dev libssl-dev install-info

# RHEL
sudo dnf install -y dh-autoreconf curl-devel expat-devel gettext-devel \
    openssl-devel perl-devel zlib-devel

# ソース取得
wget https://github.com/git/git/archive/refs/tags/v2.45.0.tar.gz
tar xzf v2.45.0.tar.gz
cd git-2.45.0

# ビルド
make configure
./configure --prefix=/usr/local
make all -j$(nproc)
sudo make install

git --version
# git version 2.45.0
which git
# /usr/local/bin/git

インストール直後の初期設定

これをやらないと commit 時にエラーになります:

# 必須2項目
git config --global user.name  "Yamada Taro"
git config --global user.email "taro@example.com"

# モダン推奨設定
git config --global init.defaultBranch main           # init 時 main を使用
git config --global pull.rebase true                  # pull は rebase 動作
git config --global core.editor "vim"                 # コミットメッセージエディタ
git config --global color.ui auto                     # 色付き出力
git config --global push.autoSetupRemote true         # 新ブランチを自動 upstream

# 設定確認
git config --global --list
cat ~/.gitconfig

認証の設定

HTTPS + Personal Access Token (PAT)

GitHub は 2021 年 8 月からパスワード認証を廃止、PAT が必須です:

# GitHub: Settings → Developer settings → Personal access tokens (classic)
# → Generate new token → repo / workflow スコープ選択
# → 表示されたトークン文字列をコピー (例: ghp_xxxxxxxxxxxx)

# クローン時に input
git clone https://github.com/user/repo.git
# Username: user
# Password: ghp_xxxxxxxxxxxx   ← PAT を入力

# 毎回入力は面倒なので Credential Helper を有効化
git config --global credential.helper cache             # 一時キャッシュ
git config --global credential.helper "cache --timeout=86400"

# 永続保存(平文、個人 PC のみ)
git config --global credential.helper store

# Gnome キーリング / KDE Wallet 連携
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

# macOS Keychain
git config --global credential.helper osxkeychain

SSH 鍵認証

# 鍵生成(Ed25519 推奨)
ssh-keygen -t ed25519 -C "taro@example.com"
# → ~/.ssh/id_ed25519 / id_ed25519.pub

# パスフレーズは付けるべき
# ssh-agent でセッション中はパスフレーズ省略
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

# 公開鍵を GitHub に登録
cat ~/.ssh/id_ed25519.pub
# → GitHub Settings → SSH and GPG keys → New SSH key に貼り付け

# 接続テスト
ssh -T git@github.com
# Hi username! You've successfully authenticated, ...

# SSH 形式 URL でクローン
git clone git@github.com:user/repo.git

主要ディストロでの git バージョン比較

ディストロ標準リポの git最新版を入れる方法
Ubuntu 24.04 LTS2.43git-core/ppa で 2.45+
Ubuntu 22.04 LTS2.34同上
Debian 122.39backports で新しめ
RHEL 9 / Rocky 92.43IUS / ソース
RHEL 8 / Rocky 82.39 (AppStream)dnf module enable git:2.x
CentOS 71.8(古い、要注意)IUS or ソース必須
Fedora 40+2.45+標準で最新
Arch / Manjaro常に最新pacman -Syu
Alpine2.40+apk upgrade

動作確認

# バージョン
git --version

# 動作テスト
mkdir ~/git-test && cd ~/git-test
git init
echo "hello" > README.md
git add .
git commit -m "Initial commit"
git log

# 設定確認
git config --list
git config --get user.email

# 既存リポジトリのクローン(公開リポで認証無し)
git clone https://github.com/git/git.git

古い CentOS 7 で git 1.8 を使う場合の注意

git 1.8 系は多くのモダン機能が無く、GitHub の HTTPS Basic 認証も廃止後は使えません。以下のいずれかを推奨:

  • OS を Rocky Linux 8/9 や AlmaLinux に移行
  • IUS リポジトリで git2.x を入れる
  • ソースから 2.40+ をビルド
  • Docker コンテナ内で新しい git を使う

トラブル

git: command not found

# パッケージインストール忘れ
which git
echo $?         # 1 が返る

# パス確認
echo $PATH

# ソースから入れた場合 /usr/local/bin がパスに無い
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

SSL 証明書エラー

fatal: unable to access 'https://github.com/...':
SSL certificate problem: unable to get local issuer certificate
# 証明書ストア更新
sudo apt install ca-certificates
sudo update-ca-certificates

# 暫定回避(推奨しない)
git config --global http.sslVerify false

FAQ

Q: snap や flatpak で入れていい?
A: 通常パッケージで十分。snap 版は IO の挙動が独特で hooks が動かないことがあるため非推奨。

Q: rpm / deb の手動ダウンロードで入れたい
A: https://git-scm.com/download/linux から各ディストロの最新ビルド情報あり。RHEL 系は別途リポジトリ追加が必要なケース多し。

Q: WSL の Ubuntu で入れた git と Windows の Git Bash、どう使い分け?
A: WSL 内で完結する作業は Linux 版、Windows のエクスプローラと連携するなら Git for Windows。同一リポを両方から触ると改行コードでハマるので避ける。

関連

  • git config — 初期設定
  • SSH 鍵作成 — 認証セットアップ
  • GitHub CLI (gh) — PAT 管理 + リポジトリ操作
  • git-lfs — 大容量ファイル対応、別途インストール