How to Set Up Git Using git config
The git config command changes the configuration options in your Git installation. It is often used to set your Git email, editor, and any aliases you want to use with the git command.
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 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!
The git config Command
The git config command sets configuration values for your Git installation. Before you start using Git for a project, you will use this command to configure your Git name and email on your computer.
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
Git Config: Configuration Levels
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.
Git Config Username Command
To set your Git username, run the git config –global user.name command. You should specify both your first and last name but your username can be anything you want to attach to your commits.
Your Git username does not need to be the same as your version control username, such as the one you use on GitHub.
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 our username values using the git config command:
git config --global user.name "Sarah Smith"
This will set our user name to Sarah Smith. 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.
Git Config Email Command
To configure your Git email address, run the git config –global user.email command. This git config email command accepts one argument: your email address.
git config --global user.email "sarah.smith@email.com"
We can see our configuration values have been set by checking our global configuration file (~/.gitconfig):
[user] email = sarah.smith@email.com name = Sarah Smith
Our identity has been successfully configured!
Git Config Editor Command
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.
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.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
How to View Your Git Configuration File
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:
user.email=sarah.smith@email.com user.name=Sarah Smith filter.lfs.clean=git-lfs clean -- %f filter.lfs.smudge=git-lfs smudge -- %f …
Conclusion
The git config command configures 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.
For more learning resources and tutorials on how to learn Git, check out our How to Learn Git guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.