Tutorial 0 Introduction - McGill-ECSE429-Winter2022/tutorials GitHub Wiki

Git and GitHub

Use this tutorial to get started with Git! Credits to: Marton Bur

Installing Git

Install the Git version control system (VCS) from https://git-scm.com/downloads.

Logging in to GitHub

  1. Create an account on https://github.com/.

  2. Verify your e-mail, then log in using your credentials.

Creating a repository on GitHub

  1. Visit https://github.com/ then click on New repository (green buttom on the right).

  2. Set your user as the owner of the repository.

  3. Give a name for the repository (e.g., ecse429-tutorial-1), leave it public, then check Initialize this repository with a README. Click on Create repository afterwards. At this point the remote repository is ready to use.
    Creating a repository on GitHub

Cloning to a local repository

  1. Open up a terminal (Git bash on Windows).

  2. Navigate to the designated target directory (it is typical to use the git folder within the home directory for storing Git repositories, e.g., cd /home/username/git).

  3. Using a Git client, clone this repository to your local Git repository. First, get the repository URL (use HTTPS for now).
    Repository URL
    Then, issue git clone https://url/of/the/repository.git
    You should get an output similar to this:
    Cloning a repository

  4. Verify the contents of the working copy of the repository by ls -la ./repo-name. The .git folder holds version information and history for the repository.

Git basics

  1. Open up a terminal and configure username and email address. These are needed to identify the author of the different changes.
    Configuring username Configuring email
    Glossary — Part 1:

    • Git is your version control software

    • GitHub hosts your repositories

    • A repository is a collection of files and their history

    • A commit is a saved state of the repository

  2. Enter the working directory, then check the history by issuing git log. Example output:
    Example output for git log

  3. Adding and commiting a file: use the git add and git commit commands.
    git commit1 git commit2 git commit3
    The effect of these commands are explained on the figure below:
    git commit explained
    Glossary — Part 2:

    • Working Directory: files being worked on right now

    • Staging area: files ready to be committed

    • Repository: A collection of commits

  4. Checking current status is done with git status.
    git status

  5. Staging and unstaging files: use git reset to remove files from the staging area.
    git add git status add git reset
    Important: only staged files will be commited.
    git commit staged

  6. To display detailed changes in unstaged files use git diff, while use git diff --staged to show changes within files staged for commit.
    git diff

  7. Reverting to a previous version is done using git checkout.
    git checkout

  8. The commands git pull (or the git fetch + git rebase combination) and git push are used to synchronize local and remote repositories.
    git remote

Browsing commit history on GitHub

  1. You can browse pushed commits in the remote repository online using GitHub. You can select the commits menu for a repository.
    commits menu
    To get a link for a specific commit, click on the button with the first few characters of the hash of the commit.
    commits select

Linux commands cheat sheet:

  • cd: change/navigate directory

  • ls: list contents of a directory

  • ls -la: list all contents of a directory in long listing format

  • touch: create a file

  • cp: copy a file

  • mv: move a file

  • rm: remove a file

  • mkdir: create a directory

  • cp -r: copy a directory recursively with its contents

  • rmdir: remove a directory

  • rm -rf: force to recursively delete a directory (or file) and all its contents

  • cat: concatenate and print contents of files

  • nano: an easy-to-use text editor

The source for most of the images in the Git documentation: https://github.com/shabbir-hussain/ecse321tutorials/blob/master/01-githubTutorial1.pptx

⚠️ **GitHub.com Fallback** ⚠️