Git & GitHub

Ha Khanh Nguyen

What is Git?

  • A version control system.
  • Its original purpose was to help groups of developers work collaboratively on big software projects.
  • Git manages the evolution of a set of files – called a repository – in a sane, highly structured way.

GitHub Account

  • First, register a GitHub account.
  • Please choose a username that you would feel comfortable showing it to your boss later!
    • coolcoder, freakingawesome, etc. are probably not good ideas.
    • use your real names (so people can recognize you)
  • Sign up for GitHub Education
    • Free pro plan after verificating student’s status

Let’s Get Started!

  • There are a lot of different ways to use Git:
  • Now, introduce yourself to Git
    • if you’re using the command line terminal or through a client (RStudio), you have to enter the following commands in the terminal.
git config --global user.name 'Jane Doe'
git config --global user.email 'jane@example.com'
git config --global --list

Connect to GitHub

Make a repo on GitHub

  • Go to https://github.com and make sure you are logged in.
  • Click green New button.
    • Or, if you are on your own profile page, click on Repositories, then click the green New button.
  • How to fill this in:
    • Repository name: myrepo (or whatever you wish, we’ll delete this soon anyway).
    • Description: “testing my setup” (or whatever, but some text is good for the README).
    • Public.
    • YES Initialize this repository with a README.
    • For everything else, just accept the default.
  • Click big green button Create repository.

Clone the repo to your local computer

  • Very easy using GitHub Desktop. (shown in lecture)
  • A bit more difficult but good to know is how to do it in the command line terminal.
    • First, know where you are when you open up the terminal. Use the following command to display your location in the file system.
pwd
  • Move yourself to the location you want, then clone the git repo using the following command:
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git
  • Now, check out your local copy of the git repo:
cd myrepo
ls
head README.md

Make a local change, commit, and push

  • Add a line to your README file:
echo "A line I wrote on my local computer" >> README.md
  • Use git status to check the status of your local changes:
git status
  • Stage the change:
git add -A
  • Commit the change (always include a commit message)
git commit -m "A commit from my local computer"
  • Push the change to the remote (non-local) repo on GitHub
git push origin master

Confirm the local change on the GitHub remote repo

  • Now, go to back to your web browser and refresh the page of your new git repo.

  • You should see the new “A line I wrote on my local computer” in the README.
  • If you click on commits, you should see one with the message “A commit from my local computer.”

Git Basics

Three States in Git

  • Git has three main states that your files can reside in:
    • modified
    • staged
    • committed
  • Modified means that you have changed the file but have not committed it to your database yet.
  • Staged means that you have marked a modified file in its current version to go into your next commit snapshot.
  • Committed means that the data is safely stored in your local database.

Three Main Sections of a Git Project

  • This leads us to the three main sections of a Git project:
    • the working tree/directory
    • the staging area
    • the Git directory (repository)
  • The working tree/directory is a single checkout of one version of the project.
    • These files are pulled out of the compressed database in the Git directory and placed on disk for you to use or modify.
  • The staging area is a file, generally contained in your Git directory, that stores information about what will go into your next commit.

  • The Git directory is where Git stores the metadata and object database for your project. This is the most important part of Git.

Basic Git Workflow

  • The basic Git workflow goes something like this:
    • You modify files in your working tree.
    • You selectively stage just those changes you want to be part of your next commit, which adds only those changes to the staging area.
    • You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.
    • When you’re ready to update the remote repo, push the commit(s).

References