First Things To Configure Before Using Git

Updated

Git is one of the most popular distributed version control systems, and when you run my Ruby on Mac script, you automatically get the latest version of Git. On top of that, the “Ultimate” version of Ruby on Mac automates everything in this guide and more in a few seconds. It also makes it easy to customize your Git username, email, and editor with a YAML file that looks like this:

user:
  name: "Rosie Revere"
  email: "rosie@example.com"

# Possible values are: atom, bbedit, brackets, emacs, sublime, vim, vscode
# Let me know if your favorite editor is missing.
editor: "vim"

Once Git is installed, the first thing to do is to set your name, email, and preferred editor. Every time you make a commit, your name and email are attached to the commit. The four main parts of a commit are the hash, which is a unique string (created from various parts of the commit) that identifies the commit, the Author, which shows your name and email, the Date, and the message. Here is an example when I run git log in the repo for this website:

commit 2ae1475442238ab95e29891d15cd99039872f1bc
Author: Moncef Belyamani <moncef@example.com>
Date:   Thu Dec 24 11:26:57 2020 -0500

    Start post on Jekyll and GitHub Actions

If you use GitHub and want it to track your work (so you can see your activity and your contributions graph), you’ll need to set your email to one that’s registered to your GitHub account. Here are the example commands to set your name and email:

git config --global user.name "Your Name"
git config --global user.email you@example.com

If you haven’t set your preferred Git editor, writing a new commit message with git commit will use the computer’s default editor. On a Mac, it’s the vi editor in the terminal. If you’ve never used vi or vim before, and you find yourself stuck there, you can get back to the command line by typing :wq, which stands for write and quit. To set your preferred editor, use a command like this one, which sets Sublime Text as the default editor:

git config --global core.editor "subl -n -w"

For VS Code, it would be this command:

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

If you use another editor, let me know and I’ll add it to this guide.

Two other options I recommend setting are the default branch name for new repos, and a global .gitignore file. As of Git version 2.29.2, the name of the default branch when creating new repos is master, but the tech community has recognized that the term “master” is not appropriate and inclusive. In response, GitHub has changed the default branch for new repos from master to main.

You can set the default branch name for any new repos you create locally to main by running this command:

git config --global init.defaultBranch main

As for the global .gitignore file, you can create it like this:

touch ~/.gitignore_global

And then open it:

open ~/.gitignore_global

And paste the following in it:

# Logs and databases #
######################
*.log
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db

The terminology *.log means any file that has the .log extension. The asterisk is known as a wildcard which will match all characters except slashes. Similarly, ._* means any file that starts with a period and an underscore.

To tell Git to use this .gitignore_global file, configure it like this:

git config --global core.excludesfile ~/.gitignore_global

This tells Git to ignore those types of files in any repo you’re working on. It’s easier to define things you always want to ignore in a single file as opposed to in every repo.

Finally, I’ll mention two more settings, one that’s nice to have, and one that you might not need all the time but doesn’t hurt to set just in case.

The first one adds some color to your terminal:

git config --global color.ui true

By default, when you run git commands like git status, the output will be all in the same color, but when you turn on the color setting, you will see untracked and modified files in red and new files in green.

The last setting deals with line endings, and is only an issue if you’re developing on Mac (or Linux) and you collaborate on the same repo with people who work on Windows, or vice-versa. If you’re on a Mac or Linux, set it like this:

git config --global core.autocrlf input

If you’re on Windows, replace input with true. You can read more about this in the official Git configuration documentation.

I hope you found this post useful. If I missed any essential settings, let me know on Twitter or send me an email.