Git Configuration

Before the first commit, you must tell Git who you are. Git adds your name and email to every commit you make. We do this with the git config command.

Set your name and email

git config --global user.name "Robin Sharma"
git config --global user.email "robin@example.com"

The option --global means the setting is used for all repositories of your user account. You need to do this only one time on a computer.

Levels of configuration

LevelOptionWhere it is savedApplies to
System--systemGit install folder (for example /etc/gitconfig)All users of the computer
Global--global~/.gitconfig in your home folderAll repositories of your user
Local--local (default).git/config inside the repositoryOnly that repository

A local setting wins over the global, and the global wins over the system. For example, you can use your office email in one project, and your personal email in other projects.

See your settings

Terminal output

$ git config user.name
Robin Sharma
$ git config user.email
robin@example.com
$ git config –list
user.name=Robin Sharma
user.email=robin@example.com
init.defaultbranch=main

Set the default branch name

New repositories use the branch name master by default in older Git versions. Most websites now use main. You can set it once:

git config --global init.defaultBranch main

Set your editor

Git opens a text editor when you need to write a long commit message. You can choose your favorite one:

git config --global core.editor "code --wait"

Here code --wait is VS Code. You can use nano or vim too.

Create short names (aliases)

An alias is a shortcut for a long command. With the alias below, you can type git st instead of git status.

Terminal output

$ git config –global alias.st status
$ git config –global alias.co checkout
$ git config –global alias.lg "log –oneline –graph –decorate"
$ git config –get-regexp alias
alias.st status
alias.co checkout
alias.lg log –oneline –graph –decorate

Remove or edit a setting

git config --global --unset alias.co
git config --global --edit

The second command opens the global config file in your editor, where you can change anything by hand.

Note: If you forget to set the name and email, Git may guess them from your computer name, and the commits will have a strange author. Always set them first. Next: Git Workflow.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.