Git Configuration
What Will We Learn?
Configuring the minimal git setup which will get you go ready to start using git. These are the setup you configure even before actually starting your project which ensures:
- your identity is tied to the project commits
- your custom alias to git commands so that you don’t have to write long commands every times
- other user and global level preferences that will optimize performance and make your development workflow more easier, etc.
Global vs Local Configuration
Generally you will configure Git at global or local level. And the difference is clear:
| Scope | Global | Local |
|---|---|---|
| Affects | All of the repositories in your machine | Only the current repository you are working on |
| Flag | Can be configured with --global flag | It is default — the --local flag |
| Path | ~/.gitconfig or ~/.config/git/config (%USERPROFILE%\.gitconfig.gitconfig for windows) | config file of your local git repo i.e. .git/config |
Note
If you are configuring at local level, make sure you are inside git repository.
Configuring Your Identity
You want to configure your user name and email mainly because it shows up in your commits. It may seem naive operation (you may feel why does I even need it?) at first but it ensures accountability and collaboration more effectively. This is why git asks you your identity before commit.
git config --global user.name "Alice"
git config --global user.email [email protected]It is good to use --global flag which will result in one time setup. If you want per repository control, skip the flag.
Tip
If you are a privacy person, no one is stopping you from providing the fake identity.
Viewing Your Configuration
Check or verify the configuration you set:
git config --listTo view specific configuration:
git config user.emailConfiguring Your Editor
Git uses text editor mostly when you write commit messages. If not configured, it uses your system’s default editor however it’s good to use the editor where you are comfortable.
git config --global core.editor nanoIf you are in windows, you may require full path while setting up editor. Path depends on installation.
Warning
If you don’t setup your editor, you may run into confusing state when Git tries to launch your default editor. This includes prematurely terminated Git operation especially in Windows.
Configuring Line Endings
Windows natively uses \r\n (CRLF) as line endings opposed to unix-like systems \n (LF). To be safe from line ending issues, set core.autocrlf that will convert line endings suited to your system.
- For Windows, set it to
true:
git config --global core.autocrlf true- For Linux and macOs, use
input:
git config --global core.autocrlf inputConfiguring and Using Aliases
Git allows you to set up aliases so that you end up writing shortcuts for long-typed and frequently used commands. You can tailor it for yourself.
Creating Alias
Creating alias is as straightforward as this:
git config --global alias.st statusAfter this, you can use git st as an alias for git status whenever you check git status.
Another example of creating shortcut to last commit as git lastcommit:
git config --global alias.lastcommit 'log HEAD -1'Viewing Alias
The following command will list all aliases you configured:
git config --get-regexp alias`--get-regexp sounds something else just to view aliases, you can even configure your own alias to view aliases:
git config --global alias.show-aliases "config --get-regexp alias"Deleting Alias
To delete a Git alias, use the --unset flag:
git config --global --unset alias.st