git fetch コマンドは、リモートリポジトリの最新の変更をローカルに取得するためのコマンドです。
ただし 作業ブランチには自動で反映せず、リモート追跡ブランチに更新内容をダウンロードします。
構文(Syntax)
git fetch [リモート名] [ブランチ名]
主なオプション一覧
| オプション | 説明 | 使用例 |
|---|---|---|
| (なし) | すべてのリモートから更新を取得 | git fetch |
<リモート名> | 指定したリモートから取得 | git fetch origin |
<リモート> <ブランチ> | 特定ブランチのみ取得 | git fetch origin main |
--all | 登録されているすべてのリモートから取得 | git fetch --all |
-p, --prune | リモートで削除されたブランチをローカルからも削除 | git fetch -p |
--dry-run | 実際には取得せず更新内容を確認 | git fetch --dry-run |
実行例
すべての更新を取得
git fetch
特定リモートの更新を取得
git fetch origin
特定ブランチを取得
git fetch origin develop
すべてのリモートから更新を取得
git fetch --all
削除されたリモートブランチを整理
git fetch -p
出力例:
x [deleted] (none) -> origin/old-feature
dry-run で確認
git fetch --dry-run
出力例:
From https://github.com/user/repo
* [new branch] feature-x -> origin/feature-x
エラー例(存在しないリモート)
git fetch notfound
出力例:
fatal: 'notfound' does not appear to be a git repository
fatal: Could not read from remote repository.
関連コマンド
git pull:git fetch+git mergeを行い、作業ブランチに反映する。git remote: リモートリポジトリを管理する。git merge:fetchした変更を統合する。
備考
git fetchは安全な操作で、作業ブランチを壊す心配はありません。- 更新内容を反映するには、
git mergeまたはgit rebaseを行う必要があります。 git pullはfetchとmergeをまとめて行うため、挙動を分けて確認したいときはgit fetchを使うのが便利です。
参考
- Git公式ドキュメント: https://git-scm.com/docs/git-fetch

コメント