The most popular VCS used today is Git. There are many online tutorials on using git
(of varying quality); one of the most thorough tutorials is the video series Git Essential Training: The Basics. However, the complete course is quite involved for a complete VCS beginner who just needs to get some work done, and the course doesn't cover a few important things you'll need to know for coursework. This companion guide will highlight the most important pieces of Git Essential Training (and a followup course, Git: Branches, Merges, and Remotes). As you become more experienced with git
, you are encouraged to return to these courses and learn about the more advanced topics covered.
git
is the name of a VCS program that manages files within collections (called repositories). The git
program itself should not be confused with GitHub, which is a website that hosts git
repositories and makes them available over the internet (e.g., to you on multiple computers, or to collaborators). Since one of the major roles of a VCS is to facilitate seamless collaboration, a service like GitHub is invaluable for this purpose. However, git
can be used entirely independently of services like GitHub, and this video series does not discuss GitHub at all. Following this course, we will cover (very briefly) the essential aspects of GitHub that you will need to share your work.
git
specifically. You can skip The history behind Git.git
on your own machine. While you are welcome to do so, you will likely instead be completing your coursework using one of Bowdoin's Linux servers (accessed remotely over SSH). In this case, git
is already installed. However, you still need to run a couple of one-time configuration commands to set up your git
account. From the machine on which you will be working (which might be, e.g., hopper
, turing
, dover
, or foxcroft
), run the following three commands to set your username, email, and default editor (of course, substitute your actual name and Bowdoin email address):
git config --global user.name 'Jane Doe' git config --global user.email 'jdoe@bowdoin.edu' git config --global core.editor 'nano'If you would prefer to use another editor (e.g.,
vim
), you can set that as well. You can always come back and change these settings later.
git
on your own machine).git
, follow along with the videos and create your own local repository to work with.nano
) to create and edit files. Refer back to the Unix Crash Course if you need a refresher on using a command-line editor. It's a good idea to have two SSH windows open: one for running your git
commands and the other for running your editor.git
commands. Go through the whole thing.mkdir
(make a new directory), mv
(move or rename files), and rm
(remove files - be careful!). As before, the Unix Crash Course (or the man
pages) can give you more details.git mv
and git rm
commands -- these function very similarly to mv
and rm
(by design) but are distinct programs, and the former are only used in the context of a git
repository.git checkout -- <file>...
to discard working directory changes and git reset HEAD <file>...
to unstage files. Recent versions of git
have added a new git restore
command that is easier to use and can handle both scenarios. The basic syntax of git restore
is as follows:
To discard changes in the working directory:
git restore <file>...
To unstage files:
git restore --staged <file>...This usage is likely easier to remember than the versions of
git checkout
and git reset
demonstrated in the videos.
git
repository. This is not sufficient,
however, to share your repository with others (e.g., with other students in a group, or with your professor to submit work). To accomplish these
kinds of tasks, we need to have a repository that is available over the internet. The most common way to do so is by using an online git
host
like GitHub (there are others as well, but GitHub is the most popular). Using a service
like GitHub also provides the important advantage of easily backing up your work.
The basic idea of a GitHub repository is fairly simple. Instead of just a single git
repository that exists on your local machine, now there
will be two repositories: the one on your local machine, and a separate repository that exists on GitHub's server and is available to others. Several
new git
commands will be used to interact with the remote repository.
git init
command, this
command results in a new git
repository on your local machine, but clone
will download the data from a URL (e.g., on GitHub). The basic
command syntax is as follows:
git clone [URL]So, for example, you might run something like this (using the actual URL of a GitHub repository):
git clone https://github.com/some-organization/some-repo-nameThe above example (if the URL was valid) would create a new directory named
some-repo-name
(which is the cloned repository) in the current
directory (you can rename it to whatever you want if desired). Inside will be whatever files were downloaded from the remote repository. Remember
that cloning (like initializing a new repository) is generally a one-time step. Once you have a cloned (i.e., local) copy of the repository, you will
be working from within that directory and do not need to clone the remote repository again (unless you delete the local copy, etc).git pushIn the context of submitting an assignment, you might have a GitHub repository accessible by you and your professor. In order for your professor to view your completed work, you will need to run
git push
to push your completed local files to GitHub. However, it's also a good idea to
run git push
on a regular basis, as this will ensure that an up-to-date copy of your repository exists on GitHub's servers and
will not be inadvertently lost or deleted.
git pull
in the opposite direction, and uses the same syntax:
git pullFor a repository that you are editing by yourself, you are unlikely to need
git pull
with much frequency, since your local repository should rarely
be lacking any commits made to the remote repository. However, an example where you might need to pull is if your professor updates starter files
in the repository or adds new feedback files. In these cases, you would need to run git pull
to see the new or updated files in your local repository. Executing git pull
before each work session is a good idea as a matter of course to make sure that
you aren't missing anything from the remote repository.
git
is doing quite a bit more under-the-hood when you run these commands.
However, for a beginner working alone on a GitHub-hosted project, this understanding should be sufficient.
clone
command. To find it, go to your GitHub repository in a web browser and
make sure you're on the Code tab. On this page, there should be a bright green button also labeled Code. Click that, and it will show you a few different
options (e.g., HTTPS or SSH), which will specify different URLs. HTTPS is the simplest option, and you can copy/paste the corresponding URL into your git clone
command. Having done so, git
will then prompt you for your GitHub
username and password whenever you interact with the remote repository (e.g., push or pull). More convenient, however, is configuring GitHub access using an SSH key, which will
allow you to access GitHub without needing to type your username and password every time (consult the Unix Crash Course for a
refresher on the concepts of SSH keys).
jdoe-keypair.pub
and
private key like jdoe-keypair.pem
) inside the .ssh
folder in your home directory (i.e., the ~/.ssh
directory). Remember that this is the home directory on the
machine from which you are running git commands (i.e., most likely hopper
, turing
, dover
, or foxcroft
). If you are working on hopper
or
turing
, this step may already be done for you.
Next, go to the key settings page of your GitHub account. Click New SSH Key. Give the key some
descriptive name (like "My Bowdoin CS key") and then in the Key field, paste in the contents of your public key file (not your private key file!).
Here is a quick command to display the contents of your public key file, assuming that your public key is named ~/.ssh/jdoe-keypair.pub
:
cat ~/.ssh/jdoe-keypair.pubPaste the output of that command (using your actual filename) into the Key field of the GitHub settings page, then click Add SSH key.
Having done this, you are now all set to use SSH key authentication with GitHub. Go back to your GitHub repository code page and copy the
SSH URL (it will look something like git@github.com/some-organization/some-repo-name
- don't change any part of this). Use this URL in your initial clone command,
and you should never need to enter a username or password for GitHub. Note that you may need to enter "yes" to accept the key the first time you
run a git
command that uses the key (most likely the initial git clone
).
git
and are going to be working individually at first, I recommend against going through this material now. Instead, get some experience and familiarity using git
, and then come back later and complete this part when you are getting ready to work in a team.
The description of working with GitHub in the previous part is sufficient when only a single user is actively working in the repository. However, things get more complicated
when you have true collaboration (i.e., a team of two or more people working on the same project). The primary potential difficulty is that two people,
working independently on the same file in their (separate) local repositories, may make conflicting (local) commits and then try to push them to the remote repository.
The first person to do so will succeed (updating the remote repository). However, when the second person then attempts to sync with the remote repository, there will be an error due to the mismatch; git
cannot tell whether the first person's or the second person's conflicting edits should persist. This type of
scenario almost never happens with only a single user, but is much more likely to happen at some point when a team is working together.
To learn about how to deal with this type of situation, some knowledge of more advanced git
features is necessary. As with our initial introduction to git
,
we will draw on another video series that is a followup to the first series. As before, consult the guide below in order to refer to the most relevant videos;
there are still a number of advanced features covered that we don't need to worry about for the moment.
Several of the modules in this series are not directly relevant to the problem at hand, so we will be skipping them entirely. The modules and sections you should go through are given below.
git
, but you are unlikely to make much conscious use of it as a beginner.
However, working with remote repositories (such as on GitHub) involves implicit use of branching, and having a basic understanding of branching will be useful.
Watch the first section only (Branching overview) to get a basic sense of what branches are and why they're useful.git
is actually managing the
communication with a remote repository. Don't worry too much about the details of branching and merging in here; just focus on the big picture.git fetch
and git merge
commands, which you probably have never explicitly used, but are actually the two
subcomponents of the git pull
command that you've been using regularly. Again, try not to get bogged down in all the branching details.git pull
does (remember, pulling is a fetch plus a merge)!git
(particularly branching).