Git and Visual Studio Code Tutorial

By: Andrew Haberlandt and Adam Lis

Before we get started

Overview

Part 1: About Git

A “repository” consists of two parts:

(Note: This is a simplification. If you take CSE 3901/3902/3903 you will study Git in more detail.)

A “history” is a graph of commits. A commit (noun, also known as a “revision”) is a set of changes to files in your repository, accompanied by a message describing the changes. Here’s an example commit on GitHub.

If everyone just pushed to the master branch, you might have a purely linear history:

                        [master]
                           |
* -----> * -----> * -----> * 
A        B        C        D

But in reality, you have multiple people working on multiple different features – they will use different branches that can be merged together as features are finished:

                                      
* -----> * -----> * -----> * -------> * [master]
A \      C      / E \     F \         I
   \           /     \       \
    * ------> *       \       * -----> * [feature-2]
    B        D         \      H        J
                        \ 
                         \
                          * [feature-1]
                          G

This entire graph is stored in the “history”.

Your local computer and the server (We use GitHub, but there are many services) might have differing histories. To sync our history with the history stored by the server, we will use two operations:

You will never be able to directly push to master. Therefore, before pushing your code you will have to checkout a new local branch (this is covered in the later parts of this tutorial.)

Since we’re using VS Code’s Git interface, we won’t perform push and pull operations directly. Instead, we’ll click the “sync” button which performs a Git Pull and then a Git Push.

Part 2: Cloning a repository

Git has a nice command-line interface, but for the purposes of this tutorial we will be using the interface in Visual Studio Code.

You should see the files for the repository, as shown in the below screenshot:

Congratulations! You succesfully cloned our repository!

You can also view the repository for this tutorial on GitHub here. All of our repositories are available on our organization GitHub page.

Find the repository location in your operating system’s finder/explorer, and open index.html in your web browser of choice. You should see the following (with the images moving around the screen):

Part 3: Making modifications

Goal: Add yourself to our display of bouncing heads

Step 1: Create a new branch

By default, when you clone a repository, Git checks out the master branch. On your local machine, you can commit to the master branch. However, we have configured GitHub to refuse pushing to the master branch directly.

Why? It’s because for all of our projects, you will be pushing to separate branches for each feature you work on. You will then create a pull request in GitHub, requesting that your peers review your code before it gets merged into the master branch.

Step 2: Add your image

Add an image to the images/ directory. You can drag-and-drop from the file explorer on your operating system directly into VS Code.

Note: Your image does not have you be an actual picture of yourself, although it might help us learn each other’s names.

Part 4: Commit and push your changes

Things to note:

Step 1: “Stage” your changes

Switch to the Git pane, and you’ll see only the files with changes in your working copy.

Now, you should see:

Note: the “U” next to the image has changed to an “A” for Added, now that we have added this file to Git.

Step 2: Commit your changes

Add a commit message (this should briefly explain your changes):

Press the checkmark:

Step 3: Push your changes

Part 5: Pull Request

Example Pull Request

Step 1: Create your pull request

If you pull up the repository in a browser, you might see the following (with your branch name):

If so, you can click “Compare & Pull request” and it save you a few clicks.

Otherwise:

You’ll see something like this:

So,

For this repository, you need at least two approvals before you can merge your pull request into master.

Step 2: Review other pull requests

Please review multiple pull requests so everyone can merge their PR :)

Step 3: Merge your pull request

Hopefully, you now have two approvals on your PR

That’s it!

Now you should be able to see your changes live on https://code4community.github.io/git-tutorial/!

Feedback

If you have any feedback for this tutorial, please create a GitHub issue or talk to one of the leaders of C4C.