Intro to Git and Github

Learning Objectives

  • Explain basic git commands like init, add, commit, push, pull and clone
  • Distinguish between local and remote repositories
  • Create, copy, and delete repositories locally, or on Github
  • Fork and clone remote repositories

What is Git?

  • A distributed version control system
  • Manages a set of files as they change over time
  • A codebase in Git is referred to as a repository or repo

Why Use Git?

  • Reverse and track changes
  • Collaborate with others

Workflow

  1. You modify files in your working directory.
  2. You stage the files, adding snapshots of them to your staging area.
  3. You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.

Common Git Commands

  • init
  • add
  • status
  • commit
  • reset
  • checkout
  • diff
  • merge
  • log
  • push
  • pull
  • clone

git init

Creates a new git repository

git add

Add files that have changed to staging area

git status

Shows the tracked files that have changed and files that have been added to staging

git commit

Saves a snapshot of the staged files

git reset

Revert back to a previous commit (snapshot)

git checkout

Switch between commits or branches

git diff

View list of changes between two commits

git merge

Combines two branches together

git log

Displays a list of your commits

List the common Git commands that we've talked about.

Codealong

Activity

  1. Create a directory called "cheatsheets".
  2. Initialize the directory as a git repository
  3. Inside the directory create a file called "git.md".
  4. Open the file in your text editor and write out all the git commands that we discussed with a brief description of what they do in your own words.
  5. Commit the changes

What is Github?

  • Github is a web-based Git repository hosting service
  • It saves your Git repos to the cloud like Dropbox saves your files to the cloud (it doesn't save them automagically though!)

Why Use Github?

  • Share and collaborate with others!
  • https://github.com/explore

What you need to know

  • Fork
  • Clone
  • Push
  • Pull
  • Pull Request

Fork

  • Create a personal copy of someone else's project

Clone

  • Copy a repository to your computer locally

git clone <url>

Push

  • Updates your local changes to your remote repository so they are the same.

git push <repo> <branch>

git push origin master

Pull

  • Download changes from a remote repository.

git pull <repository> <branch>

git pull upstream master

Pull Request

  • Suggest changes to other repos (not the same at git pull)

Separate list of git commands into two groups. Commands used with Github and those that aren't.

Codealong

Activity

  1. Create a new repository in Github called "cheatsheets"
  2. Use the code snippet provided by Github to associate your local git repository with the remote repository
  3. Push your local changes up to Github

 

Bonus:

  1. Fork your neighbors cheatsheet
  2. Clone the repo locally
  3. Make an update to their file and push it to your fork
  4. Make a pull request to their repo

Intro to Git and Github

By Matthew Gutierrez

Intro to Git and Github

  • 294