本地目录非Git仓库报错:如何将项目上传至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
Double-check you’re in the right folder
First, confirm you’re inside your project directory. Runpwd(Linux/macOS) or check your path viacd(Windows Command Prompt/PowerShell) to verify your current location. If you’re in the wrong folder, navigate to your project withcd /path/to/your/project.Initialize the Git repository
Once you’re in the correct project folder, run this command to turn it into a Git repo:git initThis creates the hidden
.gitfolder that Git needs to do its work.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).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"Link to your GitHub repository
Now connect your local repo to the remote GitHub repo you created. Replaceyour-usernameandyour-repo-namewith your actual GitHub username and repo name:git remote add origin https://github.com/your-username/your-repo-name.gitPush to GitHub
Finally, push your local commits to the GitHub repo. If your main branch is namedmain(the standard now) use:git push -u origin mainIf your branch is still named
master, replacemainwithmaster.
Common pitfalls to watch for
- Accidentally deleting the
.gitfolder: If you removed this hidden folder by mistake, just re-rungit initto 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
originalready existing, rungit remote remove originthen re-run thegit remote add origincommand.
内容的提问来源于stack exchange,提问作者Pratiksha Jadhav




