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.
Also read: The Beginner’s Guide to Git
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:
git config --list
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:
git config --global user.name “Username” git config --global user.email example@email.com
Once the commands execute successfully, verify the set variables using the command:
git config -list
After running this command, you should get an output similar to the one shown:
user.name=Username
user.email=example@email.com
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.
nano /home/ubuntu/.gitconfig
Add the entries as:
[user] name = Username email = example@email.com
Save the file and use the git config command to verify you have added the entries successfully.
Also read: How to Use Emacs for RSS with Elfeed
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.
For example:
Navigate to the directory you wish to use as a repo:
cd ~/workspace
Next, initialize the directory as a git repository with the command:
git init .
Inside the repository, use the commands below to set the username and email.
git config user.name “localusername” git config user.email “user@ubuntu.local”
To verify the changes, use the command:
cat .git/config
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:
[core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [user] name = localusername email = user@ubuntu.local
To show both global and local settings, you can use the git config
command. Here is an example output:
user.name=Username user.email=example@email.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true user.name=Localusername user.email=user@ubuntu.local
Useful git config Commands
The git config
command also allows you to set up other git settings.
For example, you can set the default git editor using the command:
git config --global core.editor vim
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.
git config --global init.defaultBranch initial
Similarly, replace the “initial” with the desired name for your init branch.
Check all your settings as shown in the output below:
user.name=Username user.email=example@email.com core.editor=vim init.defaultbranch=initial core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true user.name=Localusername user.email=user@ubuntu.local
Wrapping Up
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.
Our latest tutorials delivered straight to your inbox