Heroku CLI技术疑问:heroku config如何识别目标应用?
heroku config Commands Great question! You're spot-on that the local directory name alone doesn't tell Heroku which app to interact with. Let me break down the key ways the CLI figures out which server-side app to target:
Git Remote Configuration
This is the primary method. When you first deploy an app viaheroku createor link a local repo to an existing Heroku app withheroku git:remote -a <app-name>, Heroku adds a Git remote namedherokuto your local repository. This remote points directly to your app's Heroku Git URL (e.g.,https://git.heroku.com/your-app-name.git). The CLI checks this remote entry to map your local directory to the correct server app. If your second local directory doesn't have this remote set up, the CLI has no way to know which app you're referring to.Local
.git/configFile
The remote details are stored in your repo's.git/configfile. You can verify this by runningcat .git/configin your working directory—look for a section like this:[remote "heroku"] url = https://git.heroku.com/your-app-name.git fetch = +refs/heads/*:refs/remotes/heroku/*If this section is missing in your second directory, that's exactly why
heroku configfails to run properly there.Explicit App Flag
You can always bypass automatic detection by specifying the app directly using the-aor--appflag. For example:heroku config -a your-app-nameThis is handy if you have multiple local repos for the same Heroku app, or if you want to run commands without being in the app's local directory.
Default App Context (Less Common)
If you're using Heroku Teams or manage multiple apps, you might have set a default app viaheroku apps:default <app-name>. The CLI will use this default if no Git remote is found and no app is specified explicitly.
Quick Fix for Your Second Directory
To get heroku config working in your second local folder, just run this command from within that directory:
heroku git:remote -a your-app-name
This adds the required Heroku Git remote, and the CLI will then correctly map the directory to your server-side app.
内容的提问来源于stack exchange,提问作者Michel

