FREELessons: 24Length: 3.1 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

1.3 Getting Started: Configuring Git

One more step before we can actually use Git, and that’s configuring it. In this video, we’ll tell Git who we are and flip a few switches, as well as see where Git really saves those settings.

1.3 Getting Started: Configuring Git

Well, now that we have Git installed, the next step is a little bit of configuration. We have to tell Git who we are, so that it can record that along with the information it records about the code. And we also wanna make one or two settings changes, just to get things off to a nice start. As every screencast from now on probably will, we're gonna start by opening up the terminal. Now since this is our first time actually running any Git commands apart from the git --version that we ran last time, I'm gonna take a minute to explain the kind of basic syntax of most Git commands. Most Git commands start with the word git, and that is a command of its own. And then there are sub-commands. So in this screencast we're gonna be using the sub-command config. So we have git, a space, and then we have the word config. So git config is how I would just say that. git config is the command we're running, but it's actually the git command with the sub-command config. And then, of course, there are gonna be options via flags such as the --system flag or something like that that will be passed to some command. And then, of course, there are going to be value that we'll pass after that, and that is gonna comprise most of the git commands that we run. So, to put it simply again, we're gonna start with the git keyword, a space, the sub-command that we wanna run, and then any flags or options that will be set for this specific command. So as I said.we wanna set some configuration. And so for this we are gonna use the git config command. Now, you can actually set your configuration in three different places. And those three different places are like three different levels of configuration, where we can set something, and it will act on a certain level. Let me explain what I mean by that. The highest level that we can set a configuration option at is the system. To configure something at the system level means that that configuration setting will hold true for every user on the system that you're on. Nowadays, most of us are the only ones who work on our computer, there's a single user account that we use, and so there's not actually much of a reason to use system. If you're on a computer where multiple users exist, and you wanna set something that will hold true for all of them, you'll set that at the system level. The second configuration option is global. Global sounds bigger than system, but it's actually not. The global level could reasonably be renamed to just user. Basically, when you set something globally, it's set for that specific user on the system. So the system settings work for all users. Global settings only work for the user that set them. And any of the settings that we're gonna set in this screencast will be set globally. The third place that you can set options is at the local level, and this just means per repository. There's gonna be some things that will be set only for a specific repository, and in fact what we're gonna find as we go along, and I'll draw attention to this when we get there, is that in some of the screencasts further down the road, when we run specific commands that are not the git config command, that's actually going to set local settings, and Git will be setting those settings and then using them later. Even though they're not specific settings that we have set, Git will set those and use them. And of course there are settings that you can make on the local level as well. And pretty much every setting that Git has can be configured on all three of these levels. However, we're just going to look at the global one. And global settings are stored in your home directory in the gitconfig file. Of course, if you've used the shell very much at all, you'll recognize this pattern as the normal place that configuration files are stored. They're usually stored in your home directory, and they're stored in a file begins with a dot. And so this is where the options that we're setting today will be stored. If you're interested, I'll just mention that system files are stored in the etc/gitconfig file and local settings are set within the project folder in the .git folder that Git creates to store all of its information, and in there there is a config file, so basically they're all git config in some way. Okay, so let's actually set some of these commands. We have to begin by telling Git what your name and email address is. Now realize, this isn't anything to do with authentication or verification. It just takes your name and email address so that when you make a commit, and I know that we haven't discussed commits yet, but that'll be in the next screencast or so, when you make a commit, it's important that you be identified as the person that created that commit. And so we're gonna give Git that information right now so that it will know who is making those changes. Now of course as you can imagine, this is something you're gonna want to set at the global level, or the user level. If there are other users ont he computer, they'll set their own options, so we don't want this to be in the system settings. But it doesn't make sense to put this in the repository level, because you're gonna have the same name and e-mail address across multiple projects. So, the way this works is we're gonna start with git and then we're gonna have config. Remember this is the command and the sub-command. The next thing we wanna do is tell Git what setting we want to set. In our case, we want to set the user.name. So user is kind of the namespace for the specific setting that we're setting, and name is the specific setting itself, and then we're just gonna put a space, and then in quotes here, I'll put my name, Andrew Burgess. And if I hit enter, notice that we actually get an error. Says could not find lock config file .git/config. No such file or directory. Now I've made this error on purpose, to show you something important about the default behavior of git config. As you'll notice, it said it's looking for the .git/config file. If you recall from just a moment ago, I said that that is the place where local Git configuration options are set. They're set within the .git folder, within the Git repository, and then in the config file. However, Git is saying we couldn't find this. So what this tells us is that by default, git config set something locally, at the lowest level possible, if we don't tell it set it anywhere else. So since we want to set this at the global level, I'm going to add --global as a flag to the git config command to tell it, actually, we want you to set this setting at the global level, or in the .gitconfig file in our home directory. So now I'm just gonna do the same thing again, user.name equals Andrew Burgess, and if I hit Enter, you'll see that we do not get any errors, which means that everything went fine. All right, I'm just gonna hit arrow up to get this command again, and we want to this time just set user email, and I'm going to set it to my email address. And we'll hit Enter. Again, that has been set. Let's actually check out our git config file and see how these are being set. So we can just open in vim here, and remember, it's in the home directory, and actually, let me just do this. I'm gonna list the home directory here. Actually, I'm gonna have to list it with -a to show all, and you can see right here, we have the .gitconfig file, just like that. That is the file in our home directory, and so now, let's just run vim .gitconfig and open it up. So here in the file, you can see the two settings that we have just set, and you can see the syntax it's using. We have the square brackets for the kind of namespace, the user is the kind of settings category that we're setting, and then underneath, tabbed in, or in this case just spaced in, you can see that we have name = Andrew Burgess, email = andrew8088@gmail.com. That's my email address, and so now those settings are set. Now Git will come in here when it needs to find this information and it can find it there. But what this tells us, since it's just really basic syntax in a text file, is that we can actually set commands within this. So if I wanted to just change this to andrew88@somethingelse.com. Now if I save this, well, let me show you how we can actually read this. First of all, if I just cat that out, you'll see that it indeed was saved. It's not doing anything special. Second thing we can do is if I run git config user.email, notice that we have output to the terminal here. The email address that I changed, notice it does have that change, something.else at the end. So this tells us two things. First of all, there's absolutely nothing special about this .gitconfig file. It is a simple text file that you can go and edit by hand if you want, and in fact pretty much everything in the internals of Git is a simple text file that you could edit. Now, you certainly shouldn't, at least not at this level, and in fact it's much much easier for the most part to let the Git tools that we're gonna learn do all this modification for you. However, if you want to set a simple setting, that's something that you could do by hand if you knew the syntax and you know what command you want to set. However, often, it's just easier to do it on the command line. The second thing this tells us is that we can actually do a read from this .gitconfig file and not just a write. By not passing any value after user.email, we're actually asking the git config command what do we have set for user email? And now that we know that, I'm just gonna come back up and reset my user email. And then I can ask it again, and you can see that we do have it set. We're gonna set one more configuration option right now, and that is git.config --global, so it's set for the user. And we wanna set color.ui to true. What this does is a lot of the output that Git gives us can be kind of hard to parse if it's just plain black and white text as we have here. However, Git can color some of that to make it more easy to see exactly what's going on. This is gonna make more sense as we get into more advanced commands such as git diff. And so just setting color.ui to true is simply the global way to turn color on when it's appropriate for all the output Git gives us. Now if you want, you can set a lot more customized color settings. For example, setting specific parts to specific colors. However, for now we're just gonna go with the basic git config --global color.ui equals true. And I'm gonna finish this off by catting out our gitconfig one more time. And you can see when we have multiple namespaces, I'm saying that in air quotes, like user and then color. It just simply adds it to the end of file here, color.ui equals true. So now any of our output will be appropriately colored. Now there are a ton of other git config options that we can set. And like I mentioned, a lot of the other git commands that we're gonna be learning actually take it upon themselves to set some of those at the repository level to keep track of different things, such as remote branches and that kind of thing. And we'll look at how that works once we get to those. But for now, this is enough of a start. And if you want to google around for Git configuration options, you can see what other options are available. And as we progress, you might have an idea of what might be useful to you or you might understand more of what you're seeing on the web if you try and Google around a bit more for git config options. However, we're going to leave it at that for now. And next, we're going to look at actually creating our first Git repository.

Back to the top