When we use Git on a new computer for the first time, we need to configure a few things. Below are a few examples of configurations we will set as we get started with Git:
- our name and email address,
- what our preferred text editor is,
- and that we want to use these settings globally (i.e. for every project).
Command Line Git Setup
On a command line, Git commands are written as git verb options
, where verb
is what we actually want to do and options
is additional optional information which may be needed for the verb
.
Line Endings
As with other keys, when you press Enter or ↵ or on Macs, Return on your keyboard, your computer encodes this input as a character. Different operating systems use different character(s) to represent the end of a line. (You may also hear these referred to as newlines or line breaks.) Because Git uses these characters to compare files, it may cause unexpected issues when editing a file on different machines. Though it is beyond the scope of this lesson, you can read more about this issue in the Pro Git book.
You can change the way Git recognizes and encodes line endings using the core.autocrlf
command to git config
. The following settings are recommended:
Linux and MacOS
$ git config --global core.autocrlf input
Windows
$ git config --global core.autocrlf true
Text Editor
To set your preferred text editor, find the correct configuration command from this table:
Editor | Configuration command |
---|---|
Atom | $ git config --global core.editor "atom --wait" |
nano | $ git config --global core.editor "nano -w" |
BBEdit (Mac, with command line tools) | $ git config --global core.editor "bbedit -w" |
Sublime Text (Mac) | $ git config --global core.editor "/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n -w" |
Sublime Text (Win, 32-bit install) | $ git config --global core.editor "'c:/program files (x86)/sublime text 3/sublime_text.exe' -w" |
Sublime Text (Win, 64-bit install) | $ git config --global core.editor "'c:/program files/sublime text 3/sublime_text.exe' -w" |
Notepad (Win) | $ git config --global core.editor "c:/Windows/System32/notepad.exe" |
Notepad++ (Win, 32-bit install) | $ git config --global core.editor "'c:/program files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin" |
Notepad++ (Win, 64-bit install) | $ git config --global core.editor "'c:/program files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin" |
Kate (Linux) | $ git config --global core.editor "kate" |
Gedit (Linux) | $ git config --global core.editor "gedit --wait --new-window" |
Scratch (Linux) | $ git config --global core.editor "scratch-text-editor" |
Emacs | $ git config --global core.editor "emacs" |
Vim | $ git config --global core.editor "vim" |
VS Code | $ git config --global core.editor "code --wait" |
It is possible to reconfigure the text editor for Git whenever you want to change it.
Default Branch Name
Git (2.28+) allows configuration of the name of the branch created when you initialize any new repository. We want to set this to main
so it matches the cloud service we will eventually use.
$ git config --global init.defaultBranch main
The five commands we just ran above only need to be run once: the flag --global
tells Git to use the settings for every project, in your user account, on this computer.
Text Editor Git Setup
Let’s review those settings and test our core.editor
right away:
$ git config --global --edit
Let’s close the file without making any additional changes. Since typos in the config file will cause issues, it’s safer to view the configuration with:
$ git config --list
And alter the configuration via the command line. You can re-run the commands above as many times as you want to change your configuration. The discussion page has details on more recommended settings.
Keypoints
- Use
git config
with the--global
option to configure a user name, email address, editor, and other preferences once per machine.