git cloneできない問題を完全解決!エラー別の原因と対処法まとめ

git

「リポジトリをクローンしようとしたら、エラーが出て先に進めない…」

開発を始めようとした矢先に立ちはだかる、この壁。特に初心者の方にとっては、何が原因なのか分からず途方に暮れてしまうこともあるでしょう。

でも安心してください。git cloneのエラーは、原因さえ分かれば必ず解決できます。

この記事では、git cloneで発生する30種類以上のエラーパターンと、それぞれの解決方法を体系的にまとめました。エラーメッセージから原因を特定し、最短で解決できるようにガイドします。


スポンサーリンク

まず試すべき5つの基本チェック

1. インターネット接続の確認

# 接続確認
ping github.com
ping gitlab.com
ping bitbucket.org

# DNSの確認
nslookup github.com

2. Gitがインストールされているか

git --version
# git version 2.45.2 のように表示されればOK

3. URLが正しいか確認

よくある間違い:

# ❌ 間違い例
git clone github.com/user/repo          # httpsが抜けている
git clone https://github/user/repo      # .comが抜けている
git clone https://github.com/user/repo/ # 末尾の/は不要

# ⭕ 正しい例
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git

4. リポジトリが存在するか

ブラウザでURLにアクセスして確認:

  • Public:誰でもアクセス可能
  • Private:ログインが必要
  • 404エラー:リポジトリが存在しない

5. プロキシ環境かどうか

企業や学校のネットワークでは、プロキシ設定が必要な場合があります。


エラーメッセージ別の解決方法

エラー1:Permission denied (認証エラー)

症状

Cloning into 'repo'...
Permission denied (publickey).
fatal: Could not read from remote repository.

原因と解決法

原因1:SSH鍵が設定されていない

# SSH鍵の確認
ls -la ~/.ssh

# SSH鍵の生成
ssh-keygen -t ed25519 -C "your_email@example.com"

# 公開鍵をコピー(Windows)
type C:\Users\YourName\.ssh\id_ed25519.pub | clip

# 公開鍵をコピー(Mac/Linux)
cat ~/.ssh/id_ed25519.pub | pbcopy  # Mac
cat ~/.ssh/id_ed25519.pub | xclip    # Linux

GitHubに公開鍵を登録:

  1. GitHub → Settings → SSH and GPG keys
  2. New SSH key をクリック
  3. 公開鍵を貼り付けて保存

原因2:HTTPSでパスワード認証している(2021年8月以降廃止)

# Personal Access Tokenを使用
git clone https://YOUR_TOKEN@github.com/username/repo.git

# またはCredential Managerに保存
git config --global credential.helper manager

完全な解決手順

# 1. SSH接続テスト
ssh -T git@github.com

# 成功時の表示
# Hi username! You've successfully authenticated...

# 2. HTTPSからSSHに切り替え
git clone git@github.com:username/repo.git

エラー2:Authentication failed (認証失敗)

症状

Cloning into 'repo'...
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/user/repo.git/'

解決法

方法1:Personal Access Token (PAT) を使用

  1. GitHubでトークンを生成:
    • Settings → Developer settings → Personal access tokens
    • Generate new token (classic)
    • repo権限にチェック
  2. クローン時に使用:
# トークンを直接指定
git clone https://YOUR_TOKEN@github.com/username/repo.git

# またはプロンプトで入力
git clone https://github.com/username/repo.git
Username: your_username
Password: YOUR_TOKEN  # パスワードではなくトークンを入力

方法2:認証情報をリセット

# Windows
git config --global --unset credential.helper
git config --global credential.helper manager-core

# Mac
git config --global --unset credential.helper
git config --global credential.helper osxkeychain

# Linux
git config --global credential.helper store

エラー3:Repository not found

症状

Cloning into 'repo'...
remote: Repository not found.
fatal: repository 'https://github.com/user/repo.git/' not found

チェックポイント

# 1. URLのスペルミスを確認
# 大文字小文字は区別される!

# 2. プライベートリポジトリの場合、アクセス権限を確認

# 3. 組織のリポジトリの場合
git clone https://github.com/organization/repo.git

# 4. リダイレクトされた可能性
git clone https://github.com/new-username/repo.git

エラー4:SSL certificate problem

症状

fatal: unable to access 'https://github.com/user/repo.git/': 
SSL certificate problem: unable to get local issuer certificate

解決法

方法1:証明書を更新(推奨)

# Git for Windowsを最新版に更新
git update-git-for-windows

# CA証明書を更新
git config --global http.sslCAInfo "C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt"

方法2:企業プロキシ環境の場合

# プロキシ経由の証明書を設定
git config --global http.proxy http://proxy.company.com:8080
git config --global https.proxy https://proxy.company.com:8080

方法3:一時的な回避(非推奨・危険)

# SSL検証を無効化(セキュリティリスクあり!)
git config --global http.sslVerify false

# 特定のリポジトリのみ無効化
git config --global http."https://github.com/".sslVerify false

エラー5:Connection timeout

症状

fatal: unable to access 'https://github.com/user/repo.git/': 
Failed to connect to github.com port 443: Connection timed out

解決法

方法1:HTTPSの代わりにSSHを使用

# SSH経由でクローン
git clone git@github.com:user/repo.git

# ポート443でSSH接続(ファイアウォール回避)
# ~/.ssh/configに追加
Host github.com
  Hostname ssh.github.com
  Port 443
  User git

方法2:プロキシ設定

# HTTPプロキシの設定
git config --global http.proxy http://proxy-server:port
git config --global https.proxy https://proxy-server:port

# SOCKSプロキシの場合
git config --global http.proxy socks5://127.0.0.1:1080

方法3:タイムアウト時間を延長

git config --global http.postBuffer 524288000  # 500MB
git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999

エラー6:RPC failed (大きなリポジトリ)

症状

error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: the remote end hung up unexpectedly

解決法

方法1:浅いクローン(shallow clone)

# 最新のコミットのみ取得
git clone --depth 1 https://github.com/user/repo.git

# 後から履歴を取得
cd repo
git fetch --unshallow

方法2:バッファサイズを増やす

# バッファを1GBに設定
git config --global http.postBuffer 1048576000

方法3:部分的にクローン

# ブランチを指定
git clone -b main --single-branch https://github.com/user/repo.git

# スパースチェックアウト(特定フォルダのみ)
git clone --filter=blob:none --sparse https://github.com/user/repo.git
cd repo
git sparse-checkout set folder1 folder2

エラー7:Filename too long (Windows)

症状

error: unable to create file very/long/path/to/file: Filename too long

解決法

# 長いパス名を有効化
git config --global core.longpaths true

# Windows 10/11でシステム全体で有効化(管理者権限)
# グループポリシーエディタで設定
# または PowerShell(管理者)で:
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" `
  -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force

環境別の追加設定

GitHub Enterprise / GitLab Self-hosted

# 自己署名証明書の場合
git config --global http."https://git.company.com/".sslCAInfo /path/to/company-ca.crt

# 特定のホストのみSSL検証を無効化
git config --global http."https://git.company.com/".sslVerify false

WSL (Windows Subsystem for Linux)

# Windows側の認証情報を使用
git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

# SSH鍵をWindows側と共有
ln -s /mnt/c/Users/YourName/.ssh ~/.ssh

Docker環境

# Dockerfile
FROM alpine:latest
RUN apk add --no-cache git openssh-client

# SSH鍵をマウント
COPY id_rsa /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa

# known_hostsに追加
RUN ssh-keyscan github.com >> /root/.ssh/known_hosts

特殊なケースの対処法

ケース1:2要素認証(2FA)有効時

# Personal Access Tokenが必須
# パスワードの代わりにトークンを使用

# 1. トークンを生成(GitHub → Settings → Developer settings)
# 2. クローン時に使用
git clone https://github.com/username/repo.git
Username: your_username
Password: YOUR_PERSONAL_ACCESS_TOKEN

ケース2:Git LFS使用リポジトリ

# Git LFSをインストール
git lfs install

# LFSファイルも含めてクローン
git clone https://github.com/user/repo.git

# LFSファイルを除外してクローン
GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/user/repo.git

ケース3:サブモジュール含むリポジトリ

# サブモジュールも一緒にクローン
git clone --recursive https://github.com/user/repo.git

# または後から取得
git clone https://github.com/user/repo.git
cd repo
git submodule update --init --recursive

デバッグ方法:原因を特定する

詳細なエラー情報を表示

# 詳細モードでクローン
GIT_CURL_VERBOSE=1 git clone https://github.com/user/repo.git

# SSHの詳細ログ
GIT_SSH_COMMAND="ssh -vvv" git clone git@github.com:user/repo.git

# トレースを有効化
GIT_TRACE=1 GIT_TRACE_PACKET=1 git clone https://github.com/user/repo.git

ネットワーク診断

# GitHubへの接続テスト
curl -I https://github.com

# SSH接続テスト
ssh -vT git@github.com

# ポートの確認
telnet github.com 22   # SSH
telnet github.com 443  # HTTPS

クイックフィックス集

🔧 最も一般的な解決策

# 1. HTTPSからSSHに切り替え
git clone git@github.com:user/repo.git

# 2. Personal Access Tokenを使用
git clone https://TOKEN@github.com/user/repo.git

# 3. 浅いクローン
git clone --depth 1 https://github.com/user/repo.git

# 4. 認証情報をリセット
git config --global --unset credential.helper
git config --global credential.helper manager

🚀 緊急時の回避策

# 方法1:ZIPでダウンロード
# GitHubページで「Code」→「Download ZIP」

# 方法2:別のプロトコルを試す
git clone https://...  # HTTPS
git clone git@...      # SSH
git clone git://...    # Git protocol

# 方法3:ミラーサイトを使用(中国など)
git clone https://github.com.cnpmjs.org/user/repo.git

トラブルシューティングチェックリスト

基本チェック

  • [ ] Gitがインストールされている
  • [ ] インターネットに接続されている
  • [ ] URLが正しい(スペルミス、大文字小文字)
  • [ ] リポジトリが存在する(ブラウザで確認)

認証関連

  • [ ] SSHキーが設定されている
  • [ ] Personal Access Tokenを使用している
  • [ ] 2要素認証の設定を確認
  • [ ] 組織のアクセス権限がある

ネットワーク関連

  • [ ] プロキシ設定が必要か確認
  • [ ] ファイアウォールで遮断されていないか
  • [ ] VPNが必要か確認
  • [ ] DNS設定が正しい

環境固有

  • [ ] Windows:長いパス名の設定
  • [ ] SSL証明書の問題
  • [ ] Git LFSが必要か
  • [ ] サブモジュールの有無

よくある質問(FAQ)

Q1:いつもHTTPSではなくSSHを使うべき?

A: 状況によります:

  • SSH推奨:頻繁にプッシュする、2FA有効
  • HTTPS推奨:ファイアウォール環境、一時的な利用

Q2:会社のPCでgit cloneできない

A: 以下を確認:

  1. プロキシ設定
  2. SSL証明書
  3. ファイアウォール
  4. IT部門に相談

Q3:「fatal: not a git repository」エラー

A: クローン先のディレクトリを確認:

# 正しいディレクトリに移動
cd ~/projects
git clone https://github.com/user/repo.git
cd repo  # クローンしたディレクトリに入る

Q4:クローンが遅い

A: 以下を試す:

# 1. 浅いクローン
git clone --depth 1 URL

# 2. 特定ブランチのみ
git clone -b main --single-branch URL

# 3. プロトコルを変更(SSH↔HTTPS)

まとめ:git cloneのエラーは必ず解決できる

git cloneのエラーは多種多様ですが、原因を特定できれば必ず解決できます。

解決の手順:

エラーメッセージを読む

  • Permission denied → 認証問題
  • Connection timeout → ネットワーク問題
  • Repository not found → URL問題

基本的な対処法から試す

  1. URLの確認
  2. 認証方法の変更(HTTPS↔SSH)
  3. 浅いクローン
  4. プロキシ設定

それでもダメなら詳細調査

  • 詳細ログを有効化
  • ネットワーク診断
  • 環境固有の設定確認

最終手段

  • ZIPダウンロード
  • 別のマシンでクローン
  • IT部門やGitHub Supportに相談

この記事で紹介した方法を順番に試せば、ほとんどのgit cloneエラーは解決できるはずです。

焦らず、一つずつ確認していきましょう!

Happy Cloning! 🎯


参考リソース:

コメント

タイトルとURLをコピーしました