如何在XAMPP的htdocs中使用GitHub并解决推送失败问题
Hey there! I totally get where you're stuck—let's sort this out quickly.
Why this is happening
When you cloned the repo to your desktop, Git created a hidden .git folder inside that directory. This folder stores all the repo's configuration, including the remote GitHub URL that git push needs to know where to send your changes.
When you copied the files over to htdocs, you probably only copied the visible files and folders, not the hidden .git one. That means the htdocs version isn't actually a Git repository—it's just a bunch of loose files, so Git has no clue where to push to.
Solutions to fix this
Option 1: Move the entire cloned repo to htdocs (easiest!)
This is the simplest approach because you keep all the existing Git configuration intact:
- First, shut down Apache in XAMPP to avoid file access issues.
- Go to your desktop, grab the entire cloned repository folder (not just the files inside it), and cut-paste it into XAMPP's
htdocsdirectory. - Restart Apache, and now you can work directly from this folder in htdocs. All
git pull,git push, etc., commands will work just like they did on your desktop—no extra setup needed.
Option 2: Turn the htdocs files into a Git repo and link to your GitHub remote
If you can't move the original cloned folder, you can set up Git from scratch in htdocs:
- Open your terminal/command prompt and navigate to your project folder inside
htdocs:cd /path/to/xampp/htdocs/your-project-folder - Initialize a new Git repository here:
git init - Link this repo to your GitHub remote. Replace the URL with your actual repo's URL (you can copy it from GitHub's repo page):
Note: If you prefer HTTPS instead of SSH, use the HTTPS URL from GitHub.git remote add origin git@github.com:your-username/your-repo-name.git - Add all your files to Git and make an initial commit:
git add . git commit -m "Set up repo in htdocs" - Push your changes to GitHub. If your main branch is named
main(most common now) ormaster, adjust the command accordingly:git push -u origin main
Quick heads-up
Before you start, make sure any changes you made in the desktop repo are pushed to GitHub first. That way, you won't run into conflicts between the desktop and htdocs versions of the code.
内容的提问来源于stack exchange,提问作者Maria Giannakou

