Git (https://git-scm.com) is probably the most popular open source software available to manage source code. Git allows us to distribute code to ourselves or other developers, and provides a robust mechanism for tracking changes, creating branches, and staging changes to software, or, in our case, web projects.
While we will not be diving deeply into all the great flexibility that this tool provides, it is important that we touch on the basics of how to use Git within a development workflow.
Generally, there are a handful of tasks that we will perform with Git:
- Creating a repository to store our code.
- Adding code to our repository.
- Tracking changes to our code.
- Pulling and pushing changes.
Git can be installed using a variety of methods, including browsing the Git website at https://git-scm.com/downloads and downloading the latest release suitable for your operating system.
For the sake of demonstration, we will be installing Git on Mac OS X. Once we click on the appropriate link, our download will start and the binary files will be copied to our designated downloads folder. All that is left to do is to extract the files and then double-click on the installer to complete the installation process.
We can validate that Git has been installed correctly by opening up a Terminal window and executing the following command:
which git
The preceding command is illustrated in the following image:
If at any point there is a need to refer to the Git documentation, we can browse https://git-scm.com/doc. The documentation covers everything from the basics to advanced topics.
Assuming that we have Git installed properly, we will need to configure it for use.
Git can be configured locally per project or globally. In most cases, we will want to globally configure Git for use with all our projects. We are only concerned with a few configurations to begin with: mainly, our user.name
and user.email
, which are used for associating our user with commit messages when tracking code.
Begin by opening a Terminal window and executing the following commands:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
If we ever need to view what our configuration contains, we can execute the following command:
git config --list
Now that we have Git installed and configured, we will need to decide where we want to store our code.
While we can create a local repository, it would make more sense to create a remote repository. When someone mentions Git, it is generally synonymous with GitHub (https://github.com/). To use GitHub, we will need to sign up for a free account or log in to an existing account:
Once logged into GitHub, we will create a new empty repository. For the sake of demonstration, we will call our repository Mastering-Drupal-8
:
In the preceding example, the Owner
field would be replaced with your account name and Repository name
based on your preferences. At this point we can click on the Create repository
button to finish the creation of our Remote repository. Next we will create a local repository and push our local file up to GitHub.
To start a local repository, we need to ensure that we are within the folder that contains the files we want to begin tracking. Instantiating a local repository allows us to add files, commit them, and push them up to the remote repository that others can clone and work from. For our example, we will add the Drupal 8 instance we just created.
Begin by opening a Terminal window and entering the following command:
git init
The preceding command is illustrated in the following image:
Now that we have initialized our mastering
folder to be a local repository, we can add the contents of the folder to Git for tracking any changes. Adding and committing files requires two steps.
The first is adding the entire contents of the folder or specific files. In our example, we can add the entire Drupal instance by typing the following command in the Terminal window:
git add.
Second, we need to tell Git what we have added by committing the files and including a message describing what the addition contains. This can be accomplished by entering the following command in the Terminal window:
git commit -m 'Initial Drupal instance added to repo'
With our files added and committed locally, we now need to add a remote origin that our local repository can push to. We can execute the following command in a Terminal window, remembering to replace the origin URL with your own repo path:
git remote add origin https://github.com/chazchumley/Mastering-Drupal-8.git
To find the correct origin URL, simply look at the URL within the browser after the remote repo was created.
Now that our local repository knows that we have a remote repository, we can simply push the committed files to GitHub by executing the following command in a Terminal window:
git push -u origin master
If we navigate to GitHub, we will now see that our once-empty repo contains the Drupal 8 instance that we added locally:
With our files now safely being tracked both locally and remotely, we can ensure that any change we make can be safely retrieved and reverted. Think of this as a snapshot of our code. If we are working in a team environment, we can share the repo with others to clone the repo to their local machines. There is a lot of great documentation on how to manage Drupal workflows using Git at https://www.drupal.org/node/803746.
Realize that these are the very basics of using Git, and depending on the size of your development team, there are additional strategies that may need to be implemented.
At this point, you may be thinking that there is a lot of information to remember when installing and configuring a Drupal project. While you may be right, it is also the reason why virtualizing a development environment makes perfect sense.