How can i change the current running project to another project in GCP (Google Cloud Platform) account using cli commands other than using gcloud init
manually.
$gcloud projects list
will list the projects running on my account. I want to change the current project to any other project from the list using a cli command.
Check the available projects by running:
gcloud projects list
. This will give you a list of projects which you can access. To switch between projects:gcloud config set project <project-id>
.Also, I recommend checking the active config before making any change to gcloud config. You can do so by running:
gcloud config list
For what its worth if you have a more than a handful of projects, which I do, use:
gcloud init
This will list all your projects and give you the option to change current project settings, add a new project configuration or switch:
It will always ask you to login and display options for different google accounts that you may have.
Given that I manage multiple organisations and projects this approach lets' me to simply switch between them.
You should actually use the project ID and not the name as the other answers imply.
Example:
Then:
It's also the same thing when using just the
--project
flag with one of the commands:If you use the name it will silently accept it but then you'll always get connection or permission issues when trying to deploy something to the project.
Also, if you are using more than one project and don't want to set global project every time, you can use select project flag.
For example: to connect a virtual machine, named
my_vm
under a project namedmy_project
in Google Cloud Platform:This way, you can work with multiple project and change between them easily by just putting project flag. You can find much more information about other GCP flags from here.
I do prefer aliases, and for things that might need multiple commands, based on your project needs, I prefer functions...
Example
It could be that I'm late to answer, but this command made me learn a lot about gcloud SDK
gcloud alpha interactive
It's easier to discover by yourself that you'll need
gcloud config set project my-project
.What is best is tab complication so you see the list of projects by pressing tab twice.