Skip to content

Latest commit

 

History

History
255 lines (153 loc) · 4.88 KB

git.md

File metadata and controls

255 lines (153 loc) · 4.88 KB

Setup

  1. Download starter kit... http://bit.ly/intro-to-git-starter-kit
  2. Open a new Bash window and cd in to the starter kit's directory

Get Familiar with Bash

  • Use the previous markdown section
  • Navigate around, cat some files, etc

First, let's personalize Git by setting some configuration values...

git config --global user.name "Your Name"
git config --global user.email "[email protected]"


Local Workflow

Git will track the contents of every file within a directory.

  • What changed?
  • When did it change?
  • Who changed it?

As you're working on the files, you can create "save-points" (aka "revisions"; aka "commmits").

If you ever make a mistake, you can recover a previous state of a file.


Visual of Git Workflow


git init

  • Initializes a new "repository"
  • Tells git to keep track of changes in your current directory
  • ls -la .git

git status

  • Shows what has changed since our last commit.
  • Use this command constantly.
  • Top of your “how do I figure out what happened” tools
  • git status -s

Creating a save-point is a two-step process...

  • You "stage" your changes before "committing" your changes.
  • Staged files will be included in the next save-point.
  • Helps with organizing your files.

git add

  • Adds the specified files to the stage
    (i.e. tell Git to include this file in the next save-point).
  • git add .

git commit -m "initial commit."

  • Create a new save-point by committing our changes to the repository.
  • Use the -m flag to pass commit message inline.

git log

  • See all the save-points over the lifetime of this repository.
  • It's your safety net– you can always get back to anything.

Demo:

  1. Add new README.md and update index.html
  1. Make two separate commits to illustrate staging area.

(Checkout git log -p)


git reset <filename>

  • Remove a file from the stage.

git diff

  • Displays changes since the last save-point

In-class Exercise (5 minutes):

  1. Edit some files
  2. Stage your changes
  3. Commit your changes

Undoing the last commit:

git reset HEAD~1

Discarding local changes:

git checkout .


Remote Workflow

Why would you use remote repos?

  • Collaboration
  • Backups
  • Deployment
  • Automated Services

Each repository can have multiple "remotes", you refer to them with aliases.
(Github is just another remote repository.)


In-Class Exercise (5-minutes): Create a New Github Repo

  1. Login to Github.
  1. Create a new public repository.
  2. Follow on-screen instructions to add remote to your local repository.
  3. Push changes to Github.
  4. Checkout how you can clone.

git clone <url>

  • Creates a "clone" of a remote repository.
  • Copies the entire commit history to your computer.

git push <remote-alias> <branch-name>

  • Push code on specific branch to a remote repository.
  • Copies your entire commit history to the remote.

Demo:

  1. Edit a file on Github.
  2. Pull it's changes down.

git pull <remote-alias> <branch-name>

  • Copies updates from a remote repository to your computer.
  • Git will try to automatically merge your code.

Did You Know?
There are three ways to start a new project...

  1. git init
  2. git clone
  3. Fork a repository and git clone


Branches

  • Git automatically calls the main branch “master”.

Demo:

  1. Draw a linear-picture of what branches look like.

Create a new branch:

git branch <branch-name>

See all the branches:

git branch

Checkout a specific branch:

git checkout <branch-name>

Delete a branch:

git branch -D <branch-name>


Merging

git merge <branch-name>

  • Merging combines the histories of two branches.
  • Git will try to automatically merge your code.

Conflicts happen when Git doesn't know how to merge your code.

They look like this...

<<<<<<< HEAD
This was once a test, but is no longer.
=======
This is a test.
>>>>>>> some-branch-name

When you encounter a conflict, you need to edit the file by hand.


In-Class Exercise:

  1. Create a merge conflict by editing the same file on two branches.
  1. Merge the branches.
  2. Resolve the conflict.

Note: Branch BEFORE editing the files.



Forks and Pull-Requests

These are Github specific terms (though other platforms have adopted them).

  • A "fork" is clone of a repository to your Github account.
  • A "pull-request" is a formal request to merge forked code back into the original repo.

In-Class Exercise:

  1. Create a new fork of... gdidayton/memehub
  1. Clone the fork and add new animated gif the respository.
  2. Submit a pull-request.

A Successful Git Branching Model