Git is a distributed version control system developed by Linus Torvalds to help build the Linux kernel. Since its initial days, Git has grown tremendously to become the most popular version control system.
Git allows multiple users to simultaneously contribute to a single project, track changes, revert to previous versions, and create branches for various project versions. This is why it is important to set up your username and email in Git so each commit can be traced back to the user.
The essence of this guide is to walk you through the basics of getting started with Git after installation, particularly setting up a username and email in Git.
Note: while we are using Ubuntu for this tutorial, the steps will apply regardless of the OS you are working with.
How to Configure a Global Git Username and Email
After installing Git, you need to set your username and email address. Git allows you to set a global username and email to be used in all your git projects or local credentials used in a specific repository.
To set your git credentials, use the
git config command. Git config is a built-in tool that allows you to view and set git variables.
In Ubuntu, the git configuration variables are in the following directories:
- /etc/gitconfig – This file stores the git configurations for all users and their repositories.
- ~/.gitconfig – The .gitconfig file in the home directory; stores git configurations for a specific user.
- .git/config – This stores the git configuration for the local repository.
To verify your username and email configuration, use the command:
If you do not get any output from the command above, set your username and email.
To set the global commit username and email, enter the commands:
Once the commands execute successfully, verify the set variables using the command:
After running this command, you should get an output similar to the one shown:
You can also edit the git config file to add the username and password. Using your favorite text editor, edit the “~/.gitconfig” file and add the username and email.
Add the entries as:
Save the file and use the git config command to verify you have added the entries successfully.
How to Configure a Local Git Username and Email
Git also allows you to configure a local username and email. You can use local credentials for a specific repository.
To do this, use the
git config command without the
--global flag from inside the repository directory.
Navigate to the directory you wish to use as a repo:
Next, initialize the directory as a git repository with the command:
Inside the repository, use the commands below to set the username and email.
To verify the changes, use the command:
The command above will navigate to the .git directory inside your local repository and show the config file contents. Git stores the configurations for a specific repo in the .git/config file.
The output for this will be:
To show both global and local settings, you can use the
git config command. Here is an example output:
Useful git config Commands
git config command also allows you to set up other git settings.
For example, you can set the default git editor using the command:
Replace vim with the editor of your choice, such as Emacs, nano, etc.
You can also change the default name for the initial branch – set to “master” by default.
Use the command below to change the default init branch name.
Similarly, replace the “initial” with the desired name for your init branch.
Check all your settings as shown in the output below:
Git is an incredible tool that is helping users maintain, contribute, and share their work with others. To be more efficient when using Git, you can also make use of Git Alias to improve your workflow, or learn how to delete a local or remote branch.