EEG Annotation

icon picker
Version Control with Git and GitHub

This source provides information on a course focused on Version Control with Git and GitHub, offered by Software Carpentry. The course page can be accessed at , where potential attendees can find details about the workshop scheduled for April 19, 2024. The page is hosted on GitHub, indicating that the course may also involve practical exercises in a hands-on learning environment.
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. So here is how Dracula sets up his new laptop:

BASH

$ git config --global user.name "Vlad Dracula"
$ git config --global user.email "vlad@tran.sylvan.ia"
Please use your own name and email address instead of Dracula’s. This user name and email will be associated with your subsequent Git activity, which means that any changes pushed to , , or another Git host server after this lesson will include this information.
For this lesson, we will be interacting with and so the email address used should be the same as the one used when setting up your GitHub account. If you are concerned about privacy, please review .

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 .
You can change the way Git recognizes and encodes line endings using the core.autocrlf command to git config. The following settings are recommended:
On macOS and Linux:

BASH

$ git config --global core.autocrlf input
And on Windows:

BASH

$ git config --global core.autocrlf false

Dracula also has to set his favorite text editor, following this table:
Table 1
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"
There are no rows in this table

EXITING VIM

Note that Vim is the default editor for many programs. If you haven’t used Vim before and wish to exit a session without saving your changes, press Esc then type :q! and hit Enter or ↵ or on Macs, Return. If you want to save your changes and quit, press Esc then type :wq and hit Enter or ↵ or on Macs, Return.

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 called master when you create a new repository with git init (as explained in the next Episode). This term evokes the racist practice of human slavery and the 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 to main. 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 to master.


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.
Let’s review those settings and test our core.editor right away:

BASH

Let’s close the file without making any additional changes. Remember, since typos in the config file will cause issues, it’s safer to view the configuration with:

BASH


GIT HELP AND MANUAL

Always remember that if you forget the subcommands or options of a git command, you can access the relevant list of options typing git <command> -h or access the corresponding Git manual by typing git <command> --help, e.g.:

BASH

While viewing the manual, remember the : is a prompt waiting for commands and you can press Q to exit the manual.

More generally, you can get the list of available git commands and further resources of the Git manual typing:

BASH

KEYPOINTS

Use git config with the --global option to configure a user name, email address, editor, and other preferences once per machine.

First, let’s create a new directory in the Desktop folder for our work and then change the current working directory to the newly created one:

BASH

Then we tell Git to make planets a -- a place where Git can store versions of our files:

BASH

It is important to note that git init will create a repository that can include subdirectories and their files—there is no need to create separate repositories nested within the planets repository, whether subdirectories are present from the beginning or added later. Also, note that the creation of the planets directory and its initialization as a repository are completely separate processes.
If we use ls to show the directory’s contents, it appears that nothing has changed:

BASH

But if we add the -a flag to show everything, we can see that Git has created a hidden directory within planets called .git:

BASH

OUTPUT

. .. .git
Git uses this special subdirectory to store all the information about the project, including the tracked files and sub-directories located within the project’s directory. If we ever delete the .git subdirectory, we will lose the project’s history.
Next, we will change the default branch to be called main. This might be the default branch depending on your settings and version of git. See the for more information on this change.

BASH

OUTPUT

Switched to a new branch 'main'
We can check that everything is set up correctly by asking Git to tell us the status of our project:

BASH

OUTPUT

On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)
If you are using a different version of git, the exact wording of the output might be slightly different.

PLACES TO CREATE GIT REPOSITORIES

Along with tracking information about planets (the project we have already created), Dracula would also like to track information about moons. Despite Wolfman’s concerns, Dracula creates a moons project inside his planets project with the following sequence of commands:

BASH

$ cd ~/Desktop # return to Desktop directory
$ cd planets # go into planets directory, which is already a Git repository
$ ls -a # ensure the .git subdirectory is still present in the planets directory
$ mkdir moons # make a subdirectory planets/moons
$ git init # make the moons subdirectory a Git repository
$ ls -a # ensure the .git subdirectory is present indicating we have created a new Git repository
Is the git init command, run inside the moons subdirectory, required for tracking files stored in the moons subdirectory?

PAGING THE LOG

When the output of git log is too long to fit in your screen, git uses a program to split it into pages of the size of your screen. When this “pager” is called, you will notice that the last line in your screen is a :, instead of your usual prompt.
To get out of the pager, press Q.
To move to the next page, press Spacebar.
To search for some_word in all pages, press / and type some_word. Navigate through matches pressing N.

DIRECTORIES

Two important facts you should know about directories in Git.
Git does not track directories on their own, only files within them. Try it for yourself:

BASH

Note, our newly created empty directory spaceships does not appear in the list of untracked files even if we explicitly add it (via git add) to our repository. This is the reason why you will sometimes see .gitkeep files in otherwise empty directories. Unlike .gitignore, these files are not special and their sole purpose is to populate a directory so that Git adds it to the repository. In fact, you can name such files anything you like.
If you create a directory in your Git repository and populate it with files, you can add all files in the directory at once by:

BASH

Try it for yourself:

BASH

Before moving on, we will commit these changes.

BASH

$ git commit -m "Add some initial thoughts on spaceships"
To recap, when we want to add changes to our repository, we first need to add the changed files to the staging area (git add) and then commit the staged changes to the repository (git commit):
The Git Commit Workflow

CHOOSING A COMMIT MESSAGE

Which of the following commit messages would be most appropriate for the last commit made to mars.txt?
“Changes”
“Added line ‘But the Mummy will appreciate the lack of humidity’ to mars.txt”
“Discuss effects of Mars’ climate on the Mummy”
Show me the solution
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.