01 - Git and GitHub - old
01 - Git and GitHub - old
● In this course, we’ll heavily use two tools, Git and GitHub, that are incredibly
popular in the software development world.
● It’s important to note that Git and GitHub are two separate tools, each with its
own purpose. We’ll review each of these tools here.
Git overview
● If you do any kind of serious, collaborative (or even individual) work as a software
engineer, you’ll probably use a version control system (VCS).
● A VCS is a tool (a program) for managing changes to your code and for making it
easier to work with many people on the same code.
○ Git is one of the most popular VCSs around.
● Git manages changes in your code by taking a snapshot of your entire codebase
every time you tell it to.
○ These snapshots are stored permanently in a repository, which you can
think of as a kind of database that lives on the computer where you’re
working.
○ Storing a snapshot in the repository like this is called committing your
code.
○ You can think of a commit as a milepost, of sorts. It’s a place in your work
you want to remember and make record of, something you don’t want to
lose.
○ Every new commit records a new version of your code.
○ Git maintains a history of all of the versions of a project ever recorded.
■ You can look at (and even revert to) your code at different points in
its history, and compare the differences between different points in
the history.
GitHub overview
● GitHub is a web application that does several things:
○ Hosts Git repositories on the cloud.
■ These typically serve as a central (master) remote repository for
one or more developers.
○ Provides a nice web interface for browsing code in a Git repository.
○ Provides nice web-based tools to collaborate on code (centered around
Git repos).
○ Provides tools to link code to external services (e.g. for building, testing, or
publishing code).
● In this class, we’ll mainly use GitHub mainly for its cloud-based repository hosting
service.
○ Specifically, we will use GitHub to host remote copies of the repositories in
which we store our programming assignment code.
2. You will use Git to make a copy of this repository on your development
machine. Specifically, you’ll use the command git clone.
■ Every repository on GitHub has a unique URL you can use to clone
that repository:
■ Note that GitHub provides two different kinds of clone URLs: one
that uses SSH for communication (like the one above) and one that
uses HTTPS for communication. Importantly, to use the SSH
URL, you must have SSH keys set up with GitHub. If you try to
use the SSH URL without SSH keys set up, you will receive an
error that says something about a public key being denied.
● If you don’t know what SSH keys are or don’t want to bother
with them, just use the HTTPS URL. When performing
remote Git operations (e.g. clone, push, pull) using a GitHub
HTTPS URL, you’ll simply be asked to enter your GitHub
username and password to authenticate yourself.
■ Using that URL, you can make a copy of the GitHub repository on
any other machine (including your development machine) with a
command like this:
git status
■ If you want to see the changes you’ve made to the code, listed line
by line (in unified diff format), you can use this command:
git diff
4. Once you’ve made a certain amount of progress (e.g. you got a function
working, you moved code to a new file, etc.), you’ll want to commit a
snapshot of your code into your local Git repo. First, you’ll have to stage
the files whose changes you want to include in the snapshot.
■ In Git, committing is a two-step process:
● First you stage (i.e. mark as ready for commit) the files you
want to commit.
● Then, you actually commit the staged files.
■ Git works this way so that you can pick and choose which files
actually get committed. In other words, you don’t have to commit
every file that’s been changed, only a subset.
■ You can stage a file with the git add command, like this:
■ As before, you can always call git status to see what files are
staged.
5. Once your files are staged, you can execute your commit with the git
commit command:
git log
6. Once you’ve made a commit (or a few commits), you’ll want to make sure
they’re saved on GitHub. To do this, you can use the git push
command:
git push