「git pushするたびにorigin mainって指定するの面倒…」 「git pullしたら”no tracking information”エラーが出た」 「このブランチってどのリモートブランチと繋がってるの?」 「上流ブランチって何?追跡ブランチとは違うの?」
こんな悩みを解決するのが**追跡ブランチ(tracking branch)**の設定です。
追跡ブランチを正しく設定すれば、git push
やgit pull
をシンプルに実行できるようになり、開発効率が大幅にアップします。今回は、追跡ブランチの概念から設定方法、管理テクニックまで完全解説していきます!
追跡ブランチとは? – 基本概念を理解する

追跡ブランチの役割
**追跡ブランチ(Tracking Branch)**とは、ローカルブランチとリモートブランチの関連付けのことです。
ローカルブランチ ←→ リモートブランチ
main 追跡 origin/main
develop 追跡 origin/develop
feature/login 追跡 origin/feature/login
上流ブランチとの関係
用語の整理:
- 追跡ブランチ: ローカルブランチがリモートブランチを追跡している状態
- 上流ブランチ(upstream branch): 追跡先のリモートブランチ
- リモート追跡ブランチ:
origin/main
のような、リモートの状態を表すローカルの参照
# main(ローカル)が origin/main(上流)を追跡している
ローカル: main
↓(追跡)
上流: origin/main
追跡ブランチのメリット
# 追跡ブランチ設定なし
git push origin feature/login
git pull origin feature/login
# 追跡ブランチ設定あり
git push # シンプル!
git pull # 簡単!
追跡ブランチの設定方法
方法1:新規ブランチ作成時に設定
# リモートブランチから作成(自動で追跡設定)
git checkout -b feature/new origin/feature/new
# --trackオプションで明示的に指定
git checkout -b feature/new --track origin/feature/new
# Git 2.23以降の新しい書き方
git switch -c feature/new --track origin/feature/new
# リモートに同名のブランチがある場合は自動追跡
git checkout feature/existing # origin/feature/existingを自動追跡
方法2:既存ブランチに追跡を設定
# 現在のブランチに上流を設定
git branch --set-upstream-to=origin/main
# 短縮形
git branch -u origin/main
# 別のブランチに設定(チェックアウト不要)
git branch --set-upstream-to=origin/develop develop
# 古い書き方(非推奨)
git branch --set-upstream main origin/main # deprecated
方法3:push時に自動設定
# 初回pushで追跡を設定(-u または --set-upstream)
git push -u origin feature/new
# 以降はシンプルにpush可能
git push
# 同名のブランチを自動作成して追跡
git push --set-upstream origin HEAD
方法4:設定ファイルで直接設定
# .git/configを直接編集
[branch “main”]
remote = origin merge = refs/heads/main # git configコマンドで設定 git config branch.main.remote origin git config branch.main.merge refs/heads/main
追跡ブランチの確認方法
現在の追跡状況を確認
# 現在のブランチの追跡情報
git branch -vv
# 出力例:
# * main a1b2c3d [origin/main] Latest commit message
# develop e4f5g6h [origin/develop: ahead 2] Working on feature
# feature/login i7j8k9l [origin/feature/login: behind 3] Login implementation
# 意味:
# [origin/main] - origin/mainを追跡
# [ahead 2] - リモートより2コミット進んでいる
# [behind 3] - リモートより3コミット遅れている
詳細な設定を確認
# 現在のブランチの上流を確認
git rev-parse --abbrev-ref --symbolic-full-name @{u}
# すべてのブランチの追跡情報
git for-each-ref --format='%(refname:short) <- %(upstream:short)' refs/heads
# 特定ブランチの設定確認
git config branch.main.remote
git config branch.main.merge
# より詳細な情報
git remote show origin
ステータスで確認
# git statusでも追跡情報が表示される
git status
# 出力例:
# On branch main
# Your branch is up to date with 'origin/main'. ← 追跡情報
# ブランチ間の差分を確認
git status -sb
# ## main...origin/main [ahead 1, behind 2]
追跡ブランチの変更と削除
追跡先を変更
# developがorigin/developを追跡していたが、
# upstream/developに変更したい
# 方法1:上流を変更
git branch --set-upstream-to=upstream/develop develop
# 方法2:一旦削除して再設定
git branch --unset-upstream develop
git branch --set-upstream-to=upstream/develop develop
# 現在のブランチの追跡先を変更
git branch -u origin/main
追跡を解除
# 現在のブランチの追跡を解除
git branch --unset-upstream
# 特定ブランチの追跡を解除
git branch --unset-upstream feature/old
# 設定ファイルから削除
git config --unset branch.feature/old.remote
git config --unset branch.feature/old.merge
実践的な使用例
例1:フォークしたリポジトリでの設定
# フォーク元をupstreamとして追加
git remote add upstream https://github.com/original/repo.git
# mainブランチはupstreamを追跡
git checkout main
git branch -u upstream/main
# 自分の作業ブランチはoriginを追跡
git checkout -b feature/my-work
git push -u origin feature/my-work
# 確認
git branch -vv
# * feature/my-work abc123 [origin/feature/my-work] My changes
# main def456 [upstream/main] Original repo
例2:複数のリモートを使い分ける
# 複数のリモートがある場合
git remote -v
# origin git@github.com:myuser/repo.git
# upstream git@github.com:original/repo.git
# deploy git@production-server:repo.git
# ブランチごとに異なるリモートを追跡
git branch -u origin/develop develop
git branch -u upstream/main main
git branch -u deploy/production production
# 各ブランチでシンプルにpush/pull可能
git checkout develop && git pull # origin/developから
git checkout main && git pull # upstream/mainから
git checkout production && git push # deploy/productionへ
例3:チーム開発での運用
# チームメンバーのブランチを追跡
git checkout -b review/john-feature --track origin/feature/john-work
# レビュー後、自分のブランチに戻る
git checkout my-feature
git branch -u origin/feature/my-work
# 定期的に上流の変更を取り込む
git pull --rebase # 追跡設定があるので簡単
トラブルシューティング

エラー1:No tracking information
# エラーメッセージ
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
# 原因:追跡ブランチが設定されていない
# 解決策1:追跡を設定
git branch --set-upstream-to=origin/main
# 解決策2:明示的に指定
git pull origin main
エラー2:Cannot setup tracking information
# エラーメッセージ
Cannot setup tracking information; starting point 'origin/feature' is not a branch
# 原因:リモートブランチが存在しない
# 解決策:まずfetchする
git fetch origin
git branch --set-upstream-to=origin/feature
# またはブランチを作成してpush
git push -u origin feature
エラー3:Diverged branches
# 状況:ローカルとリモートが分岐
Your branch and 'origin/main' have diverged
# 解決策1:マージ
git pull # 追跡設定があれば自動的にorigin/mainからマージ
# 解決策2:リベース
git pull --rebase
# 解決策3:強制的にリモートに合わせる(危険!)
git reset --hard @{u} # @{u}は上流ブランチを指す
便利な設定とエイリアス
グローバル設定
# pushのデフォルト動作を設定
# current: 現在のブランチと同名のリモートブランチにpush
git config --global push.default current
# simple: 追跡ブランチにのみpush(Git 2.0以降のデフォルト)
git config --global push.default simple
# 新規ブランチを自動的に追跡
git config --global branch.autoSetupMerge always
# pullでrebaseをデフォルトに
git config --global branch.autosetuprebase always
便利なエイリアス
# ~/.gitconfigに追加
[alias]
# 追跡情報を表示
track = branch -vv
# 上流を設定
set-upstream = branch --set-upstream-to
# 現在の上流を表示
upstream = rev-parse --abbrev-ref --symbolic-full-name @{u}
# 上流との差分を確認
incoming = log ..@{u}
outgoing = log @{u}..
# 上流をリセット
reset-upstream = !git branch --unset-upstream && git branch --set-upstream-to
# すべてのブランチの追跡情報
tracks = for-each-ref --format='%(refname:short) <- %(upstream:short)' refs/heads
# 使用例
git track # 追跡情報を表示
git set-upstream origin/main # 上流を設定
git incoming # pullで取得される変更を確認
git outgoing # pushで送信される変更を確認
スクリプトで自動化
全ブランチの追跡を自動設定
#!/bin/bash
# setup_tracking.sh - すべてのローカルブランチに追跡を設定
for branch in $(git branch | sed 's/\*//g'); do
# リモートに同名のブランチがあるか確認
if git ls-remote --heads origin | grep -q "refs/heads/$branch"; then
echo "Setting up tracking for $branch..."
git branch --set-upstream-to=origin/$branch $branch
else
echo "No remote branch for $branch"
fi
done
echo "Tracking setup complete!"
git branch -vv
追跡情報のレポート生成
#!/bin/bash
# tracking_report.sh - 追跡状況のレポートを生成
echo "=== Git Tracking Branch Report ==="
echo "Date: $(date)"
echo ""
echo "Remote repositories:"
git remote -v
echo ""
echo "Branch tracking information:"
git branch -vv
echo ""
echo "Detailed tracking setup:"
for branch in $(git branch | sed 's/\*//g'); do
upstream=$(git config branch.$branch.remote)
merge=$(git config branch.$branch.merge)
if [ -n "$upstream" ]; then
echo "$branch -> $upstream/$(basename $merge)"
else
echo "$branch -> (no tracking)"
fi
done
ベストプラクティス
追跡ブランチ設定の原則
# 1. 作業開始時に必ず追跡を設定
git checkout -b feature/new
git push -u origin feature/new
# 2. cloneした直後に確認
git clone <repository>
cd <repository>
git branch -vv
# 3. 定期的に追跡状況を確認
git fetch --all
git branch -vv
# 4. 不要な追跡は削除
git branch -d old-feature # ローカルブランチ削除
git push origin --delete old-feature # リモートブランチ削除
チームでのルール例
# チーム開発での追跡ブランチルール
# 1. mainとdevelopは必ずorigin/を追跡
git branch -u origin/main main
git branch -u origin/develop develop
# 2. featureブランチは作成者のリモートを追跡
git push -u origin feature/user-story-123
# 3. リリースブランチは専用リモートを追跡
git branch -u release/v1.0.0 release/v1.0.0
# 4. ホットフィックスは本番環境を追跡
git branch -u production/hotfix hotfix/critical-bug
まとめ – 追跡ブランチで効率的な開発を
Git追跡ブランチの設定と管理、完全マスターできましたか?
基本を押さえる
✅ git push -u origin branch
– 初回pushで追跡設定
✅ git branch -vv
– 追跡状況の確認
✅ git branch -u origin/branch
– 追跡先の設定・変更
✅ @{u}
– 上流ブランチを参照する記法
効率化のポイント
- 新規ブランチは必ず
-u
でpush - 定期的に
git branch -vv
で確認 - エイリアスで操作を簡略化
- チームでルールを統一
トラブル回避
- fetchしてからtracking設定
- リモートブランチの存在を確認
- 分岐時の対処法を理解
- 不要な追跡は削除
追跡ブランチを正しく設定すれば、日々のGit操作が格段に楽になります。シンプルなgit push
とgit pull
で、スムーズな開発を実現しましょう!
コメント