初めての git push で止まる時は、エラーの文で原因が分かれます。どれか分からない時は、まず次の2行で「コミットがあるか」と「ブランチの名前」を見ます。

まずこれを打つ

git log --oneline -1
git branch

1行目でエラーが出たら、コミットがまだ1つも無い(原因A)。2行目で * master と出たら、ブランチの名前が main ではない(原因B)。

原因の見分け方

出ている文字原因直し方
error: src refspec main does not match anyA. コミットがまだ無いgit add と git commit をしてから push
同じ src refspec main で、コミットはあるB. ブランチの名前が mastergit push -u origin master、または名前を main に替える
has no upstream branchC. このブランチの送り先がまだ決まっていないgit push -u origin main(初回だけ)
'origin' does not appear to be a git repositoryD. 送り先(origin)の登録がまだgit remote add origin URL
remote origin already exists送り先はもう登録済み(2回目の登録)git remote -v で URL が合っていれば何もしなくていい。URL を替えるなら git remote set-url origin URL

原因A:コミットがまだ無い

git push origin main は「手元の main というブランチを送る」という意味です。ところが、一度もコミットしていないと、main というブランチはまだできていないので、送る物がありません。それが src refspec main does not match any(送る元の main が見つからない)です。

hanako@MacBook-Air cafe % git push origin main
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/hanako/cafe.git'
送る元(src)の main が見つからない = 送る物が無い

ファイルを作っただけでは、Git の記録には入りません。git add で入れる物を選び、git commit で記録してから送ります。

原因B:ブランチの名前が master

Git は、設定(init.defaultBranch)をしていないと、いまの版でも最初のブランチの名前を master にします。この場合もコミットはあるのに、同じ src refspec main does not match any が出ます。git branch で * master と出たら、送るブランチの名前を合わせます。

GitHub の新しいリポジトリの案内は main を前提にしているので、手元の名前を main に替えるのが分かりやすいです。git branch -M main と打つと、いまのブランチの名前が main に替わります。次に作るリポジトリから main にしたい時は、git config --global init.defaultBranch main を一度打っておきます。

原因C:送り先がまだ決まっていない

コミットもあり、名前も main なのに、git push だけを打つと次のように止まります。このブランチを、送り先のどのブランチにつなげるか(upstream)がまだ決まっていないからです。

hanako@MacBook-Air cafe % git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use

  git push --set-upstream origin main

hanako@MacBook-Air cafe % git push -u origin main
To https://github.com/hanako/cafe.git
* [new branch]      main -> main
branch 'main' set up to track 'origin/main'.
-u を付けた1回で、つなぎ先が覚えられる

(1つ目のエラーの後には、push.autoSetupRemote の案内が2行続きます。省いています。)

-u は --set-upstream の短い書き方で、「この送り先を覚えておいて」という指定です。付けるのは初回の1回だけで、次からは git push だけで送れます。

初めての push(全部まとめて)

git add .
git commit -m "最初のコミット"
git branch -M main
git push -u origin main

送り先を登録していなければ、最初に git remote add origin https://github.com/ユーザー名/リポジトリ名.git を打ちます。

  1. git log --oneline -1コミットがあるか(無ければ add と commit)
  2. git branch名前が master なら git branch -M main
  3. git remote -v何も出なければ git remote add origin URL
  4. git push -u origin main初回だけ -u を付ける

パスワードを聞かれて止まる時は git push で Authentication failed を見てください。add・commit・push の関係は、図解の Git 入門 で1本の流れとして説明しています。

この記事の画面は、macOS 26.6 / zsh 5.9 / git 2.51.0 で実際に出したものです。

Gitで、ほかによくあるつまずき