Git is by far the most popular distributed version control system in the world. Every developer who works on a Git project can have their own copy of a repository locally. This means that many different people can collaborate on the same project in tandem.
When you’re first getting started with Git, there’s some setup that you need to do. This setup only needs to be done once on your computer.
In this guide, we’re going to talk about how to set up Git using the git config command. We’ll walk through a few git config commands you can use to get started. Without further ado, let’s begin!
What is git config?
The git config command sets configuration values for your Git installation.
This command modifies the contents of Git config files. These files store information such as your username, default editor, and the email you want to associate with your commits.
Before you can start working with repositories, you’ll need to do some initial configuration. We’re going to cover five topics:
- Configuration levels
- Creating your identity
- Setting up an editor
- Creating an alias
- Viewing and reconfiguring Git
Before we start, we’ve got to discuss the different configuration levels for default git configuration options.
Configuration values can be set at three different levels:
- –local: Local values will be applied to the repository in which the git config command is executed. These values are stored in .git/config inside a repository.
- –system: System values are applied to all users on a machine. You should set system-level configuration values with caution because it may alter existing configurations. These values are stored in /etc/gitconfig on Linux.
- –global: Global values are applied to a particular user on an operating system. They are stored within the ~/.gitconfig file in your home directory.
When you’re first setting up Git, you’ll mostly use the –global level.
How to Set Up Your Identity
You’ll need to set up your identity when you first install Git. This is mandatory because every commit contains your name and your email address. You cannot change the authorship information associated with a commit once it has been created.
There are two pieces of information you need to specify: your name and email.
Let’s configure these values using the git config command:
git config --global user.name "Sarah Smith" git config --global user.email "email@example.com"
This will set our name to Sarah Smith and our email to firstname.lastname@example.org inside our Git repositories. All of our future commits will refer to this information. We’ve used the –global option to apply this default git config to all repositories owned by our user.
We can see these values have been set by checking our global configuration file (~/.gitconfig):
[user] email = email@example.com name = Sarah Smith
Our identity has been successfully configured!
How to Set Up Your Editor
Do you like vim? Are you an emacs fan? Does nano suit all your needs? Whatever text editor you prefer, it’s wise to tell Git about it. This is because there are a number of commands, like git commit, which will open up a text editor in which you can type.
Let’s set nano as our default code editor:
git config --global core.editor "nano"
Every time we execute a command that launches a text editor, nano will be used. You can substitute nano for any text editor you have installed on your system.
How to Create an Alias
Do you get tired of repeatedly typing the same commands? Git aliases are here to the rescue. They allow you to write shortcuts for common commands that you write.
Would you rather write
git co instead of
git commit. Do you have a long command that you want to shorten? We can write Git aliases for all these cases.
Let’s write a git alias which calls the git commit command:
git config --global alias.co commit
Every time we run
git co, the
git commit command will be run.
How to View Your Git Configuration
You can view an individual configuration value using the git config command followed by the value you want to view:
git config user.name
This returns “Sarah Smith”. This is the value we set earlier in our code.
You can use the git config –list command to see all the configuration values that are associated with your particular Git installation:
git config --list
Here’s an example of what you may see:
firstname.lastname@example.org user.name=Sarah Smith filter.lfs.clean=git-lfs clean -- %f filter.lfs.smudge=git-lfs smudge -- %f …
The git config command is used to configure your Git installation.
When you first start using Git, you should configure your name and email. This will ensure Git knows what identity to attach to your commits. You should also set an editor. This ensures that if you use a command which references a text editor, your preferred text editor will be used to open the file.
Now you’re ready to start using the git config command line operation like an expert!