Git Introduction

Instructor Note

Make sure to start a Zoom meeting to record the session, and save the recording locally.

These are my notes for the Git and GitHub session that I did for the Robotics event at Western Technical College

What is Git and GitHub

Git is a program that you can install on your computer that keeps track of changes that you make to a project. Git can keep track of each change made to a file in your project's folder.

A Git repository is a folder in which the files are tracked by Git.

GitHub is a website that allows you share a Git repository and collaborate with other people.

For this activity, you will need the following software:

  1. A GitHub account
  2. VS Code installed
  3. Git for Windows must also be installed unless you have Windows Subsystem for Linux installed

Create a Repository on GitHub

  1. Sign up, or sign in to GitHub (if you are signing up, then you'll need to access your email to verify your signup)
  2. Create a new repository:
    1. Click the New button
    2. Name the repository Test Repo
    3. Make sure to check the box to include a Readme file
  3. Keep GitHub open in your browser (you should be at the page for the repository you just created)

Note that every repository should have a 'readme' file. This file shows up on the repsority page in GitHub, and it should describe the project and include any instructions that might be needed to get the project up and running, such as the software that must be installed for the project to work.

Readme files are written in 'markdown' code, which is very easy to learn. Here's a markdown reference, in case you are interested.

Using VS Code

You can connect VS Code to your GitHub account

  1. Click on the Accounts icon in the lower left corner of VS Code
  2. Uncheck all the boxes to sync your settings (you could leave them checked if you complete these steps on your home computer)
  3. Choose Sign in with GitHub
  4. A new browser tab should open, click Continue

Configuring Git

You should configure Git so that it knows your name and email (we'll see how it uses this info later). To do this:

  1. In VS Code, open a terminal by pressing Ctrl + ~
  2. Then enter these two commands (make sure to use your name and email):
git config --global user.name "Your Name"
git config --global user.email "Your Email"

Clone your Repository

To clone a repository means to copy it from GitHub down to your local computer.

Go back to your browser window that has your GitHub repository.

  1. Click on the green Code button
  2. Copy the URL of the repository

Then, in VS Code:

  1. Open a terminal by pressing Ctrl + ~, and make a note of the path in the terminal. This will be the location of your Git repository when you execute the next command.
  2. In the terminal, enter git clone followed by the URL of your GitHub repository. This will 'clone', or download the repository to your local computer.
  3. In VS Code chose File then Open Folder... and select the folder that you just 'cloned'

'Committing' Your Code Changes

You track the changes to a project by making a commit.

You can see all the commits made to a project by running this command:

git log

At this point you should just see one commit, which was automatically done for you when you created the repository on GitHub

In order to make a commit, there are actually two steps that you need to do:

  1. Add the files that you want to commit (this is known as 'staging' the files)
  2. Then committing the files and including a message when you do so.

Let's make a few changes, so that we have some files to commit:

  1. Update the readme file by adding a sub heading:
    1. To add a sub heading in markdown, simply put two hash tags and a space at the beginning of a line, and after the space put the text you want for the heading.
  2. Add a new file named sample1.txt to the repository folder
    1. Put any text you want inside the file.

You can see which files have changed in your project by running this command:

git status

Notice that the files we changed show up in red, this means that they have not been 'staged'.

To 'stage' the files, so that they will be included in the next commit, run this command:

git add .

The dot will 'stage' all files in the project folder that have changed. But you could just add files individually like so: git add sample1.txt

Before we actually commit the files, run the git status command again and note that the files now show up in green. Files that show up in green will be included in the next commit that you make.

Now go ahead and commit the files that we have 'staged' by running this command:

git commit -m "Added sample and updated readme"

When you run this command, you must add the -m option (which stands for 'message'), followed by a message that describes changes you made to the files your a commiting (note that the message should be enclosed in quotation marks).

Run the git log command again, and you should see that there are now two commits that have been made to the repository.

Pushing Your Commits

Right now, the repository on your local computer has changes that have not yet been synched to GitHub. You can 'push' the commits you make on your local computer to GitHub by running this command:

git push origin main

Let's talk more about branches in Git, but first refresh your repository's page in GitHub and note that the Readme file has been updated (to include a subheading) and note that the sample1.txt file should be there.

Branches in Git

In Git you can create different versions of your entire project by creating a branch. When you create a branch, Git will copy the files from the 'main' branch. Then all of the commits that will make will only be applied to the new branch.

Branches make it easy to collaborate with other people, because if everyone is working in the main branch, then you could have lots of people changing the same files, which could lead to confusion and conflicts.

When working on a project with other people, it's common for each person to work in their own branch. Then all of the commits they make will not affect other people, or the main branch.

Then, at some point you can merge your branch into the main branch.

To create a new branch, run this command:

git branch newbranch

Note that we have named the branch newbranch, in a real project you'd want to come up with a better name for your branch.

We are still working in the main branch, in order to switch to the new branch, run this command:

git checkout newbranch

Now all the changes and commits that you make will not affect the main branch. This is a great way to experiment without making a mess of the main branch.

Before we start making changes to the new branch, run this command to look at all or your branches:

git branch

Note that the asterisk indicates which branch you are currently working in.

Add a new file named sample2.txt.

Let's also make a change to the readme file by adding some bolded text (to make text bold in markdown, put two asterisks on each side of the text you wish to be bold).

Now add/stage the changes and commit them by running these two commands:

git add .
git commit -m "Added sample2 and updated readme"

We should 'push' the changes we made to newbranch:

git push origin newbranch

Note that you should always 'push' your changes so that they are backed up on GitHub, in case something goes wrong with your local computer.

Now go to GitHub and notice that you can look at different branches by using the drop down on the resporitory's page (near the left side).

If you look at the main branch you'll see that it does not include the commits we made to newbranch.

Let's assume that we are done working on newbranch, and now we are ready to 'merge' its commits to the main branch. In the VS Code terminal, run this command:

git checkout main

This will switch back to the main branch. Note that the changes you made on the other branch are not visible anymore (Git swapped the files for each branch when we did the checkout command).

If you run the git log command, you will not see the commits that you made on newbranch.

Run the command to merge the commits from newbranch into the main branch:

git merge newbranch

Now you should see the changes made on newbranch. Run the git log command to see them.

After merging a branch into the main branch, you should 'push' the main branch to synch the changes to GitHub:

git push origin main

Other things we could do