You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Gitbash/命令行推送项目到GitHub时遇非Git仓库错误

Why You're Getting "fatal: not a git repository" Error When Pushing to GitHub

Hey there! Let's break down why this error is popping up and how to fix it quickly.

The core issue here is super straightforward: Git can't find a .git folder in your current working directory (or any of its parent folders). That .git folder is Git's "control center"—it stores all the tracking data, commit history, and remote repository links for your project. Without it, Git has no idea this folder is supposed to be a managed repository.

Common Reasons & Fixes

  • You haven't initialized a Git repo in your project folder yet
    If you created your project folder and jumped straight to adding the remote link and pushing, you skipped the critical first step. Here's the step-by-step fix:

    1. First, confirm you're in your project's root directory (use pwd to check your current path if you're unsure).
    2. Initialize the local repository: git init
    3. Stage all your project files: git add .
    4. Create your first commit: git commit -m "Initial commit"
    5. Link your local repo to the GitHub remote: git remote add origin <your-github-repo-url>
    6. Now run your push command again: git push -u origin master
  • You're not working in the right directory
    Maybe you ran the git push command from a subfolder of your project, not the root where the .git folder lives. Here's how to fix this:

    1. Check your current directory with pwd.
    2. Navigate to your project's root folder using cd /full/path/to/your/project.
    3. Verify the .git folder exists by running ls -a (this shows hidden files and folders).
    4. Try the git push -u origin master command again.

A quick heads-up: If you created your GitHub repo recently, the default branch might be main instead of master. If you get an error about the branch not existing, just swap master with main in your push command!

内容的提问来源于stack exchange,提问作者Shane Harper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:03:14