DCU Library | Digital Humanities Workshops | Intro to Version Control with Git

Getting Started

When we use Git on a new computer for the first time, we need to configure a few things.

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 information which may be needed for the verb.

Open Git Bash from wherever you installed it. There is a GUI for Git and you may have installed it but for this lesson we will be working mostly in the command line.

Account setup:

First we’ll configure your name and email address.

git config --global user.name "<username>"
git config --global user.email "<email address>"

This user name and email account will be associated with your subsequent Git activity. Any changes pushed to GitHub or another Git host server after this lesson will include this information. In this lesson, we will be interacting with GitHub and so the email address used should be the same as the one used when setting up your GitHub account.

📌 Keeping your email private: If you are concerned about privacy, you can read here how to keep your email address private.

Branch naming:

All file changes are associated with a “branch.” For now, it’s enough to know that branches exist. By default, Git creates a branch called master when you create a new repository with git init (as explained in the next Episode).

In 2020, most Git code hosting services transitioned to using main as the default branch. Any new repository in GitHub and GitLab defaults to main. However, Git has not yet made the same change. To change the default on Git to match, enter the following command:

$ git config --global init.defaultBranch main

Note that if this value is unset in your local Git configuration, the init.defaultBranch value defaults to master. You can check your settings at any time:

$ git config --list

📌 Line endings: As with other keys, when you hit 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:

📌 Text editors: We’ll be working within our Git Bash window for most of this lesson but for some operations Git needs to open an associated text editor. Vim is the default on most installs.

If you haven’t used Vim before and wish to exit a session without saving your changes, press Esc then type :q! and hit Return. If you want to save your changes and quit, press Esc then type :wq and hit Return.

Or we can also set our favorite text editor if you didn’t select one on your Git install:

Editor Configuration command
Atom $ git config –global core.editor “atom –wait”
Nano (Mac) $ git config –global core.editor “nano -w”
Notepad (Win) $ git config –global core.editor “notepad”
Notepad++ (Win, 64-bit install) $ git config –global core.editor “‘c:/program files/Notepad++/notepad++.exe’ -multiInst -notabbar -nosession -noPlugin”
Sublime Text (Win, 64-bit install) $ git config –global core.editor “‘c:/program files/sublime text 3/sublime_text.exe’ -w”

I use atom so I will switch to that:

$ git config --global core.editor "atom --wait"

📌 Shell Commands: As well as Git commands we’ll be using some shell commands in this lesson:

We have a cheat sheet pdf with more shell and git commands here


Creating a repository

A Git repository is a data structure used to track changes to a set of project files over time.

Repositories are stored within the same directory as these project files, in a hidden directory called .git. We can create a new git repository either remotely via our GitHub account, or locally via the command line. Let’s use the command line.

We have an empty folder. Now create an empty Git repository using git init (short for initialise)

$ git init
  Initialized empty Git repository in <your file path>/gitlesson/.git/

📌 When we use Git via the command line, we preface each command with git, so that the computer knows we are trying to get Git to do something, rather than some other program.

We’ll do a couple more shell commands now to see what has happened

$ ls 			

Our folder still shows as empty. If we add the -a flag to the ls command it will include hidden files, which the .git folder is.

$ ls –a
  .	..	.git

Now the response includes the .git folder is. That is where all of the revision history for this repository will live)

Now that our folder is a Git project we can check its current status (we’ll be doing this a lot).

$ git status
  On branch main
  Initial commit
  Nothing to commit

If yours shows branch master enter the following command to switch it to main. This will be important later.

git checkout -b main

💡 Key Points:

✅ Use git config with the --global flag to configure user name, email address, editor once per machine

git init initializes a repository

✅ Git stores all of its repository data in the .git directory

Back
Next