7.

git fatal: Not a valid object name 'master' の原因と対処

編集
この記事の要点
  • fatal: Not a valid object name: 'master'. は「master ブランチが存在しない」エラー
  • 主な原因: ① git init 後に 1 コミットも無い、② デフォルトが main(Git 2.28+)、③ ブランチ名規約変更
  • 対処1: 1 コミット作る → git add . && git commit -m "init"
  • 対処2: git branch -m main でリネーム、または git symbolic-ref HEAD refs/heads/main
  • 予防: git config --global init.defaultBranch main でデフォルトを統一

エラーの全文

$ git checkout master
fatal: Not a valid object name: 'master'.

$ git log master
fatal: bad revision 'master'

$ git diff master
fatal: ambiguous argument 'master': unknown revision or path not in the working tree.

「master というブランチ(あるいはコミット)が見つからない」というエラーです。原因はだいたい 3 パターン。

原因 1: git init 直後で 1 コミットも無い

mkdir myproj && cd myproj
git init
git status
# On branch master
# No commits yet
git log master
# fatal: your current branch 'master' does not have any commits yet

ブランチ「ポインタ」は HEAD として存在するが、コミットが 0 件なので参照先のオブジェクトが無い状態。何か 1 つコミットすると解決します:

echo "# myproj" > README.md
git add README.md
git commit -m "Initial commit"

git log master      # ← 通る

原因 2: デフォルトブランチが main(Git 2.28+)

Git 2.28(2020 年 7 月)以降、init.defaultBranch 設定でデフォルト名を変更可能になりました。GitHub / GitLab / 各種 OS パッケージはデフォルトを main に切り替えています。

git --version
# git version 2.39.0

git init
git status
# On branch main         ← master ではない!
# No commits yet

git checkout master
# fatal: Not a valid object name: 'master'.

対処は 3 通り:

# A) main を master にリネーム(ローカル)
git branch -m main master

# B) これから新規 init するときのデフォルトを master へ
git config --global init.defaultBranch master

# C) main をそのまま使い、ドキュメント / CI を更新(推奨)
git config --global init.defaultBranch main

原因 3: GitHub のデフォルトブランチが main / 取り違え

2020 年 10 月から GitHub は新規リポジトリのデフォルトを main に。clone 直後にローカルにあるのは main で master は存在しません。

git clone https://github.com/user/repo.git
cd repo
git branch -a
# * main
#   remotes/origin/HEAD -> origin/main
#   remotes/origin/main

git checkout master
# fatal: Not a valid object name: 'master'.

# 正しくは
git checkout main

master から main へ完全移行する

# 1) ローカル master を main にリネーム
git branch -m master main

# 2) リモートに main を push
git push -u origin main

# 3) GitHub のデフォルトブランチを main に変更
# (Settings → Branches → Default branch)

# 4) リモート master を削除
git push origin --delete master

# 5) ローカルのリモートトラッキングを更新
git fetch --prune
git remote set-head origin main

その逆: main を master にしたい

# ローカル
git branch -m main master

# リモート
git push -u origin master
git push origin --delete main

# デフォルト変更(GitHub UI で)

# 新規 init のデフォルトを master に
git config --global init.defaultBranch master

HEAD だけ master に向けたい(コミット無し時)

git init 直後でまだコミットが無いまま master という名前にしたい:

git init
git symbolic-ref HEAD refs/heads/master
git status
# On branch master
# No commits yet

echo init > a.txt
git add a.txt
git commit -m "Init"
git log master  # OK

関連する類似エラー

メッセージ意味 / 対処
your current branch 'master' does not have any commits yetgit init 直後で 0 コミット。何か 1 つコミット
error: pathspec 'master' did not match any file(s)checkout 時、master ブランチもファイルも無い。git branch -a 確認
error: src refspec master does not match anypush 対象の master が無い。main を push、または rename
fatal: ambiguous argument 'main': unknown revision逆向きで main が無い。git branch -a

予防策: 設定を統一する

# 全プロジェクトで main を使う(推奨)
git config --global init.defaultBranch main

# 現在の設定確認
git config --global --get init.defaultBranch
# main

# CI / スクリプトでハードコードしている master を main に書き換える
# .github/workflows/*.yml の branches: [master] → [main]
# .gitlab-ci.yml only: [master] → [main]

# Docker タグや README のバッジリンクも要更新

FAQ

Q: チーム内で master と main が混在している
A: 一括移行スクリプトを用意し、リモートでリネーム → 各自 git fetch --prune; git branch -m master main; git branch -u origin/main を実行。

Q: GitHub Actions が master トリガで動かなくなった
A: .github/workflows/*.ymlon.push.branchesmain に変更。

Q: 既存タグ / リリースは大丈夫?
A: タグはコミット SHA を直接指すのでブランチ名変更の影響なし。リリースは紐づくタグごとそのまま残ります。

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. fatal: remote origin already exists.
  2. fatal: '~' does not appear to be a git repository
  3. Cannot rebase: You have unstaged changes. Please commit or stash them.
  4. remote: error: denying non-fast-forward refs/heads/master (you should pull first)
  5. error: pathspec ... did not match any file(s) known to git.
  6. The following untracked working tree files would be overwritten by checkout
  7. fatal: Not a valid object name: 'master'.
  8. Unlink of file 'ファイル名' failed. Should I try again? (y/n)
  9. Another git process seems to be running in this repository, ~
  10. error: Your local changes to the following files would be overwritten by checkout: