Exporting Config Locally

At this point, we have a fresh installation on production, and a (partially) built site on our local machine. Let's export our local configuration so that we can push it to production.

We can do this either via the UI, or Drush. Drush is by far the fastest, and easiest way to do it. In fact, in practice, it's the only way I manage configuration. So, for those reasons, and for the sake of time, that is what I'll show you now.

Exporting Configuration with Drush

Exporting configuration with Drush is WAY faster than through the ui. The entire export and import procedure can take place in just a handful of commands on the command line.

When importing and exporting configuration with Drush, it is assumed that you'll be doing so with an entire site, not single items. Drush doesn't even support single-item import and export. Drupal Console does, so if you want to import and export single items via the command line, take a look at Drupal console.

Exporting Configuration

Exporting a site's configuration via Drush is done with the command drush config-export or its alias drush cex.

  • drush cex

When I type that in the terminal, and press enter, I get a message that says "Configuration successfully exported to ../config/sync." And if we check our project directory, we'll see the new config folder in the project root, and all of the configuration files are there!

You'll also see that a .htaccess file was also automatically created to further protect the directory from being accessed by someone that shouldn't be snooping around this site's configuration. Yay!

That's all there is to exporting via drush!

However, if you want to move this to another site, that's where Git comes in handy!

Managing Configuration with Git

Git is a fantastic tool for keeping your code version controlled. Now that we have our configuration in code, we can version control that too! Let's do it!

  • git status shows us that we never committed our settings.php file after we changed the config directory or our composer files after we required Admin Toolbar, so I'll do that first.
  • git commit -am "Changed the config directory in settings.php and installed Admin Toolbar."
  • git push

Now, we're just left with the config directory to commit locally. I'll add that to the repo, commit it and push it to the remote repo.

  • git add config
  • git commit -m "Committed first export of the site's configuration"
  • git push