You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

本地目录非Git仓库报错:如何将项目上传至GitHub?

Fix: "this directory does not appear to be a Git repository" When Uploading to GitHub

Hey, I’ve hit this exact error more times than I can count when getting a local project up on GitHub—let’s break down why it’s happening and how to fix it fast.

Why this error pops up

Git throws this message because the folder you’re currently working in hasn’t been initialized as a Git repository. Git needs that hidden .git folder (created when you initialize) to track changes, manage commits, and link to remote repos like GitHub.

Step-by-step fix

  1. Double-check you’re in the right folder
    First, confirm you’re inside your project directory. Run pwd (Linux/macOS) or check your path via cd (Windows Command Prompt/PowerShell) to verify your current location. If you’re in the wrong folder, navigate to your project with cd /path/to/your/project.

  2. Initialize the Git repository
    Once you’re in the correct project folder, run this command to turn it into a Git repo:

    git init
    

    This creates the hidden .git folder that Git needs to do its work.

  3. Stage your files
    Add all your project files to the Git staging area with:

    git add .
    

    If you only want to add specific files, replace the . with the file names (e.g., git add index.html style.css).

  4. Make your first commit
    Commit the staged files with a clear message (replace the text with something meaningful for your project):

    git commit -m "Initial commit: Add core project files"
    
  5. Link to your GitHub repository
    Now connect your local repo to the remote GitHub repo you created. Replace your-username and your-repo-name with your actual GitHub username and repo name:

    git remote add origin https://github.com/your-username/your-repo-name.git
    
  6. Push to GitHub
    Finally, push your local commits to the GitHub repo. If your main branch is named main (the standard now) use:

    git push -u origin main
    

    If your branch is still named master, replace main with master.

Common pitfalls to watch for

  • Accidentally deleting the .git folder: If you removed this hidden folder by mistake, just re-run git init to recreate it.
  • Already initialized but in the wrong directory: If you initialized Git in a parent folder instead of your project folder, navigate to the correct project folder and start over with git init.
  • Remote repo already exists: If you get an error about origin already existing, run git remote remove origin then re-run the git remote add origin command.

内容的提问来源于stack exchange,提问作者Pratiksha Jadhav

火山引擎 最新活动