Git无法正常推送到Heroku问题排查:基于代码副本创建新应用失败
Hey there! Let’s walk through the most common reasons your Git push to Heroku is failing with your Code2 directory, and how to fix them:
When you copied Code1 to Code2, the Git remote might still be pointing to your original App1, or maybe you never added the new app’s remote at all. Here’s how to check and fix this:
- Run
git remote -vin your Code2 directory. You should see aherokuremote pointing to your new app’s Git URL (likehttps://git.heroku.com/your-new-app-name.git). - If there’s no heroku remote, or it’s pointing to App1, run
heroku git:remote -a your-new-app-name(replaceyour-new-app-namewith the actual name of the new app you created on Heroku).
If you just copied the files without the .git folder (or didn’t initialize Git in Code2), Git won’t know how to push anything:
- Check if the
.gitdirectory exists by runningls -la(look for a folder named.git). - If it’s missing, run these commands in order:
git init(initializes a new Git repo in Code2)git add .(stages all files for commit)git commit -m "Initial commit for new Heroku app"(creates the first commit)
Heroku only accepts pushes from a Git repo with no uncommitted changes. Let’s verify:
- Run
git status. If you see any "Changes not staged for commit" or "Untracked files", you need to commit them first:git add .(stage all changes)git commit -m "Commit pending changes before push"
Sometimes the issue is permissions or a misconfigured login:
- Run
heroku appsto list all apps linked to your account. Make sure your new app shows up here. - If it doesn’t, double-check that you created the app under the same Heroku account you’re logged into. If not, you’ll need to either switch accounts (
heroku login -ito log into a different account) or get added as a collaborator on the app. - You can also try logging out and back in to refresh your session:
heroku logoutfollowed byheroku login.
If Git starts pushing but fails during the Heroku build process, the issue is likely with your code or dependencies:
- Run
heroku logs --tailto see real-time logs of the build failure. Look for keywords like "Composer error", "PHP version mismatch", or syntax errors in your scripts. - Common fixes here:
- Ensure your
composer.jsonis valid (no typos, all required dependencies are listed). - Specify a compatible PHP version in
composer.json(Heroku requires this for newer apps—add something like"php": "^8.1"to therequiresection). - Fix any syntax errors in your PHP scripts that are causing the build to crash.
- Ensure your
If you copied the entire .git folder from Code1, your Git history is tied to App1. This might cause issues if you’re trying to push to a new app with a blank history:
- If this is the case, you can either:
- Remove the
.gitfolder and reinitialize Git (follow step 2 above), or - Force push to the new app (but be careful—this overwrites the new app’s Git history):
git push heroku main --force(replacemainwith your branch name if it’s different)
- Remove the
If none of these steps fix the problem, share the exact error message you get when running git push heroku main—that will help narrow down the issue even more!
内容的提问来源于stack exchange,提问作者Outcast

