Setting Up Git
Overview
Teaching: 30 min
Exercises: 0 minQuestions
How do I get set up to use Git?
Objectives
Install Git
Configure
git
the first time it is used on a computer.Understand the meaning of the
--global
configuration flag.
It’s time to install Git on your PC, so let’s take a detour over to the setup page and return once we’ve finished installing Git.
Now that Git is installed, we can open a terminal to interface with Git.
To open a terminal in MacOS, follow these instructions: https://support.apple.com/guide/terminal/open-or-quit-terminal-apd5265185d-f365-44cb-8b09-71a064a42125/mac
To open a terminal in Windows after installing Git for Windows, press the Windows key on your keyboard, type Git Bash, and press Enter.
For Linux, you will need to refer to the documentation for your Linux distribution.
When we use Git on a new computer for the first time, we need to configure a few things:
- 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).
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
. Here is how a user named John Smith might configure Git:
$ git config --global user.name "John Smith"
$ git config --global user.email "john.smith@example.com"
Please use your own name and email address instead of John’s. This user name and email will be associated with your Git activity, which means that any changes pushed to GitHub, BitBucket, GitLab or another Git host server after this lesson will include this information.
For this lesson, we will be using GitHub and so the email address used should be the same as the one you will use when setting up your GitHub account. If you are concerned about privacy, please review GitHub’s instructions for keeping your email address private.
Line Endings
As with other keys, when you hit 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 togit config
. The following settings are recommended:On macOS and Linux:
$ git config --global core.autocrlf input
And on Windows:
$ git config --global core.autocrlf false
If you have a favorite text editor, you can change the configuration to use it, following the table below. For today’s workshop, we will use the nano text editor. Copy the configuration command for nano from the table below, and run it in your terminal.
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.
Exiting Vim
Vim can be confusing and intimidating for new users. To exit without saving changes, press Esc then type
:q!
and hit Enter or ↵ or on Macs, Return. To save your changes and quit, press Esc then type:wq
and hit Enter or ↵ or on Macs, Return. You do not need to be a Shell power user or Vim expert to complete this workshop; all Vim commands will be provided for you.
Git (2.28+) allows configuration of the name of the branch created when you
initialize any new repository. We are going to change the default branch name to main
.
$ git config --global init.defaultBranch main
Default Git branch naming
Source file changes are associated with a “branch.” For new learners in this lesson, it’s enough to know that branches exist, and this lesson uses one branch.
By default, Git will create a branch calledmaster
when you create a new repository withgit init
(as explained in the next Episode). This term evokes the racist practice of human slavery and the software development community has moved to adopt more inclusive language.In 2020, most Git code hosting services transitioned to using
main
as the default branch. As an example, any new repository that is opened in GitHub and GitLab default tomain
. However, Git has not yet made the same change. As a result, local repositories must be manually configured have the same main branch name as most cloud services.For versions of Git prior to 2.28, the change can be made on an individual repository level. The command for this is in the next episode. Note that if this value is unset in your local Git configuration, the
init.defaultBranch
value defaults tomaster
.
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.
You can check your settings at any time:
$ git config --list
You can change your configuration as many times as you want: use the same commands to choose another editor or update your email address.
Key Points
Use
git config
with the--global
option to configure a user name, email address, editor, and other preferences once per machine.