How to create a Git repository and add a complete Drupal site to it (a Drupal how-to)

How to create a Git repository and add a complete Drupal site to it (a Drupal how-to)

September 1, 2019 2 By Bernardo Ryan


In this section, what we’re going to do is
add a complete Drupal site to version control. Now, we’re going to talk a bit more about
version control and give you some background if you don’t have that already. And we’ll also talk more about the command
line. But it’s really exciting to get a site on
version control for the first time, so I’m going to go ahead and do that before we move
on to the conceptual stuff behind version control. So what I’m going to do is navigate to a website
where I’ve installed just a default Drupal 7 installation. I have downloaded Drupal 7 dot 7. Now, this is one version back from current
which is 7 point 8, but we’re using an older version just so we can go through the upgrade
process in later videos. Now, if you’d like, you can do the rest of
the series on an existing Drupal site. But because we’re going to be modifying files,
we’ll be adding some dummy modules, you may want to create a fresh Drupal installation
in order to practice on so you don’t accidentally make modifications that you’re not able to
roll back easily. So my site is going to be on loc dot gitsite
dot com and if we go ahead and open up this folder, you’ll see that we have just a default
Drupal installation. Now, we need to get to this place on the command
line. So I’m going to jump to the command line. I’m going to hit Command-K which will clear
the back scroll which is everything that has previously been printed out on the screen. So we have a fresh command line here. Now, when we’re on the command line, we’re
always within a particular directory in the file system and we can find out what directory
that is by typing pwd which is short for print working directory. And hit enter and it says that we’re in Users
slash chrisshattuck. Now, this isn’t where the website is that
I want to add a version control. So I need to change the directory by typing
cd and then the name of the directory. I know that it’s at Chris Websites loc dot
gitsite dot com. And you may have noticed that some of the
text I was typing was automatically completed and I was able to do that by pressing tab. And when you press tab, it will autocomplete
anything that you’re typing in. I’m going to hit enter and now we’re in the
directory. So if I do pwd again, we get the name of the
directory that we’re in. Just to make sure, I’m going to type ls which
will list the files in our directory. And you can see that we have all of the files
that exist in a default Drupal installation. So we should be good to go here. There are three steps we need to take in order
to get our site into a Git repository. The first is to initialize repository or create
the repository, the second is to add all of the files in our site to the repository, and
the third is to make a commit. Now, we’ll come back to all of those things
in detail in just a minute. But let’s go ahead and start the beginning
which is initializing a repository. So I’m going to go ahead and type git init
and you see we have a line that says “Initialized empty Git repository in Chris Websites loc
dot gitsite dot com, and then it says slash dot git. Now, this is a new folder that got created
when we initialized the repository and we’ll come back to that folder in a bit. But for now, so if I set to say that this
is the folder where all of our Git information is stored. Now, let’s say we want to learn a little bit
more about git init. What we can use is a command called help. So I’m going to go ahead and type in the command
get help init. So what this says is that we’re running the
Git program, we’re running the help command, and we’re passing init as a parameter to this
command because we want to learn more about it. I’m going to go ahead and hit enter and this
will open up the Git Manual so we can learn more about it. And I can use the arrow keys to go down or
up, I can use space to jump down a full page, and if I want to exit out of this I can type
q and that jumps me out. Using help is a great way to understand what
you’re doing throughout this process. So even if we don’t take the time to open
up the help file and read, you might still consider pausing the video and doing that
for every new command that you see. You might end up finding some really interesting
features in Git that way. Let’s look at one more command before we add
all of the files to our repository. I’m going to go ahead and type git status,
and this will give us information about our repository. Now, you see we have a lot of red. These are all files that have not been added
to the repository. Now, I’m scrolling up and you can see that
we actually have a lot of back scroll. We have some of the help that we saw earlier
and then right here is where we get to the git status command. And so everything below this is what happened
after we ran that command. So first, it says On branch master, we’ll
get to branches in a bit, Initial commit, and then it has a list of Untracked files
and actually tells us how to add individual files which is a logical next step. And then everything in red are all files that
haven’t been added to the repository. And you see not only files but folders listed
here as well — includes, misc, modules, and so on. Let’s go ahead and add all of our files in. I’m going to do so by using the add command
and then I’m using a dot to indicate that we want to add everything in this directory. We didn’t get any feedback. We just get jumped to an empty command line
afterwards. But if we type git status again, we see all
of these green files which are all files that have been added. Now, while we call them added at this point,
they actually haven’t been committed to the repository so we haven’t added them the version
control yet. This is kind of a middle area where we decide
what we’re going to do when we’re going to commit. So we’re going to use the git commit command
in order to actually add it to our repository. We’re going to add a parameter and we’re doing
so by doing dash m and then adding our message. So dash m is short for message and we’ll go
ahead and call it first commit and I’m going to hit enter. So if you’re following along, congratulations
because you’ve just added your first website to version control. What this means right away is that if you
begin making modifications to this code base, you can roll back to this snapshot that we
just took of your code base or compare any changes that you make to this snapshot. If we do a git status now, you see that we
have nothing to commit. Our working directory is clean which means
we’re good to go. Now, if we step back up to the command that
we used, git commit, and we’re curious what that dash m meant, and you’ll see these additional
parameters often, you can just do git help commit. And if you scroll down, you’ll see a list
of options here and we can continue scrolling down until we get to the dash m which is right
here. It says message and here’s the long version
of it, and it says Use the givenas the commit message. Okay. I’m going to hit q to exit out of that.