Installing & Configuring Git
Before you dive into using Git to make branches and repositories and commits, you should ensure your computer has it installed and configured correctly.
The good news is that most modern OS distributions come with Git already installed. To check, open a terminal window and type the command git -v. If Git is installed, this command tells you its version. If you want to know where Git is installed, type the which git command in Terminal to show the location.
Some integrated development environments (IDEs), like Xcode, install their own version of Git, and others help you install Git during their setup. If you’ve found that Git isn’t on your machine or if you ever want to install a different version than the one on your machine, you can find a download link on the downloads page of the official Git website.
Setting Your Name
If you remember from the last lesson, every commit had a name and an email address. When you’re working on a project and have a question about some code, Git can tell you who added it to the codebase. The first time you make a commit, if you haven’t already set your email address and name, Git uses your system username and your computer’s name because the fields can’t be blank. Git will then show you a message saying you probably need to change the values and where it stored them.
You can avoid this by setting your name and email first. One of the nice things about working with Git is that it stores all the configuration settings in plain text files, making them easy to change when needed.
When you start working with Git, it puts a file in your home directory called .gitconfig. Because the filename starts with a ., it’s usually invisible. Anything in this configuration file will apply to all of your Git repositories.
In each repository, Git can create another configuration file: .git/config. Most of the settings in this file are about the current repository: things like the URLs of any remote repositories and information about all the branches. But you can also override any of the global configuration settings by adding a value with the same entry in the repository configuration.
There’s also a system-level configuration file, but typically you don’t need to work with that one.
Before you set values in the global Git configuration, it’s a good idea to see if any values exist. To list all the values in the global .gitconfig in your home directory, type the command:
git config --global --list
Because you specify --global, you don’t need to be in your home directory for the command to work. If you haven’t used Git much, the list of values should be short.
To add your name or update the name that is in the .gitconfig, use this command:
git config --global user.name "Your Name Goes Here"
If you use the --list again, the new name should appear in the configuration. Adding an email is similar:
git config --global user.email "name@address"
The user part of the key is the section name. When you list the configuration file again, you’ll notice that the keys in the file are name and email, which are in the [user] section.
Over time, your .gitconfig file will add more things, so digging through the entire list becomes time-consuming. When you know the name of the key, you can use the --get command:
git config --global --get user.name
The command to show all the values in a particular section is slightly different. Here’s the command to show all the values in the [user] section:
git config --global --get-regexp user
As a beginning Git user, you’ll likely only need to work with the [user] section and perhaps the configuration’s [http] and [core] sections. The [http] and [core] sections are where you can enter proxy information if Git is going to need to use your proxy server.
You can find an extensive list of keys at the documentation page for git-config. You can configure everything from error message text to output colors to how each Git command handles particular situations. Your IDE or any Git helper program you install can also add its own keys to the configuration.
When making many changes to the configuration file, adding keys one at a time gets tedious. Git supplies an --edit command that opens the appropriate configuration file with a text editor.
git config --global --edit
Now that you’ve done the basic configuration, it’s time to make a shiny new repository.
Creating a Repository
Remember that Git runs only when you execute a command rather than all the time. Because of this, when you tell Git to start managing a directory, it creates a hidden .git directory where it stores what it needs. Without running a Git command, you can see if Git is managing a directory by looking for that .git directory.
Git can manage any directory on your computer. You can create a new directory, ask Git to manage it, and start working in it. You can also start working in a directory and then ask Git to manage it. Git only knows about changes made after it starts managing a directory, so it’s a good idea to add Git early in the process.
Using Terminal, you can ask Git to create a new directory and manage it:
git init newProject
Git creates a directory called newProject and adds the .git directory to it.
If you’ve already got a directory, you can git init that directory:
git init existingProject
You could also navigate into the directory and ask Git to manage the current directory:
git init
The lesson’s next section is a demo of some of the things you’ve just learned and how a graphical Git client app like GitHub Desktop handles the tasks.