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
| Level | Option | Where it is saved | Applies to |
|---|---|---|---|
| System | --system | Git install folder (for example /etc/gitconfig) | All users of the computer |
| Global | --global | ~/.gitconfig in your home folder | All repositories of your user |
| Local | --local (default) | .git/config inside the repository | Only 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
$ 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.
$ 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.
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |