Git Basics: Climbing the Totem Pole

Preview:

DESCRIPTION

A basic overview of the Git and GitHub workflow - intended for beginners, but could also be helpful for git users who are shaky on the fundamentals

Citation preview

© Matthew Salerno, 2012

Git Basics: Climbing the Totem Pole

by Matthew SalernoGithub: https://github.com/seldomatt

Twitter:@seldomattLinkedin: http://www.linkedin.com/pub/matthew-salerno/9/62b/584

© Matthew Salerno, 2012

Git vs. Github

© Matthew Salerno, 2012

Git vs. Github

GIT• Revision control and source

code management system

© Matthew Salerno, 2012

Git vs. Github

GIT• Revision control and source

code management system • Created by Linus Torvalds

(Linux) in 2005

© Matthew Salerno, 2012

Git vs. Github

GIT• Revision control and source

code management system • Created by Linus Torvalds

(Linux) in 2005

GITHUB• Hosting service for software

projects that use Git

© Matthew Salerno, 2012

Git vs. Github

GIT• Revision control and source

code management system • Created by Linus Torvalds

(Linux) in 2005

GITHUB• Hosting service for software

projects that use Git• Started in 2008, now 1m+

users

© Matthew Salerno, 2012

Git vs. Github

• GitHub – has a graphical user interface

© Matthew Salerno, 2012

Git vs. Github

• GitHub

© Matthew Salerno, 2012

Git vs. Github

• Git – run through the command line

© Matthew Salerno, 2012

Git vs. Github

• Git – run through the command line– Tracks changes to a file or directory by storing

commits (versions) to a local repository

© Matthew Salerno, 2012

Git vs. Github

• Git – run through the command line– Tracks changes to a file or directory by storing

commits (versions) to a local repository– Local changes can be pushed to remote repository

(GitHub)

© Matthew Salerno, 2012

Git vs. Github

• Git – run through the command line– Tracks changes to a file or directory by storing

commits (versions) to a local repository– Local changes can be pushed to remote repository

(GitHub)– Fork/clone projects from remote repositories to

local repos, and much more…

© Matthew Salerno, 2012

Git vs. Github

• Git

© Matthew Salerno, 2012

Part of being a programmer isbreaking down complexity into more manageable parts

© Matthew Salerno, 2012

We’re going to try to swallow a manageable, bite-sized portion of Git.

© Matthew Salerno, 2012

What We Will Cover

• Init – creating a local repository

© Matthew Salerno, 2012

What We Will Cover

• Init – creating a local repository• Add – staging changes

© Matthew Salerno, 2012

What We Will Cover

• Init – creating a local repository• Add – staging changes • Commit – commit changes (saves a version)

© Matthew Salerno, 2012

What We Will Cover

• Init – creating a local repository• Add – staging changes • Commit – commit changes (saves a version)• Status/Log – see staged/unstaged changes,

commit history

© Matthew Salerno, 2012

What We Will Cover

• Init – creating a local repository• Add – staging changes • Commit – commit changes (saves a version)• Status/Log – see staged/unstaged changes,

commit history• Push – create a remote repo and push

changes from local repo (GitHub)

© Matthew Salerno, 2012

What We Will Cover

• Init – creating a local repository• Add – staging changes • Commit – commit changes (saves a version)• Status/Log – see staged/unstaged changes,

commit history• Push – create a remote repo and push

changes from local repo (GitHub)• Fork/Clone – work on someone else’s project

© Matthew Salerno, 2012

What We Won’t Cover

• Installing Git

© Matthew Salerno, 2012

What We Won’t Cover

• Installing Git • Branching, merging, and a host of other

operations that would be central to using git to manage professional projects

© Matthew Salerno, 2012

GIT: BUILDING THE TOTEM POLE

© Matthew Salerno, 2012

WORKING TREE

© Matthew Salerno, 2012

WORKING TREE

• Files and directories that the user alters in an editor or otherwise (example.rb, ‘rails_app’ directory)

© Matthew Salerno, 2012

WORKING TREE

• Files and directories that the user alters in an editor or otherwise (example.rb, ‘rails_app’ directory)

• ACTIONS:– WRITE CODE– DELETE CODE– SAVE (LOCALLY)

© Matthew Salerno, 2012

Local Repository

© Matthew Salerno, 2012

Local Repository

The local repository is where git stores versions, or commits, of your working tree

© Matthew Salerno, 2012

Let’s create a local git repository

• From the command line, navigate to our working tree directory

© Matthew Salerno, 2012

Let’s create a local git repository

• From the command line, navigate to our working tree directory

• Run ‘git init’ command/working tree $ git init .

© Matthew Salerno, 2012

Working Tree

© Matthew Salerno, 2012

Working Tree

Local Repository

© Matthew Salerno, 2012

Working Tree

Local Repository

What’s Missing?

© Matthew Salerno, 2012

Index/Staging Area

© Matthew Salerno, 2012

Index/Staging Area

Index is a collection of changes to the working tree waiting to be saved to the repository as a

commit

© Matthew Salerno, 2012

Index/Staging Area

Index is a collection of changes to the working tree waiting to be saved to the repository as a

commit

(the On-Deck circle of working tree changes)

© Matthew Salerno, 2012

Index/Staging Area

When we make changes to the working tree, we add them to the index(staging area) with the ‘git

add’ command

© Matthew Salerno, 2012

Git Add

• Make some changes to the working tree

© Matthew Salerno, 2012

Git Add

• Make some changes to the working tree• /working tree $ git add .

© Matthew Salerno, 2012

Git Add

Our index is now a snapshot of our working tree in it’s current state

© Matthew Salerno, 2012

Git Add

We can keep ‘git add’-ing changes to update the index

© Matthew Salerno, 2012

Git Add

• Added changes are ‘staged’. Changes to the working tree that have not been added to the index are ‘unstaged’

© Matthew Salerno, 2012

Git Add

• Added changes are ‘staged’. Changes to the working tree that have not been added to the index are ‘unstaged’

• $ git status will show us what changes have been staged and which have not

© Matthew Salerno, 2012

Eventually, we’ll come to a point where we want to save a version of our working tree

in it’s current state.

© Matthew Salerno, 2012

COMMIT

© Matthew Salerno, 2012

COMMIT

• A commit is a saved version (snapshot) of the working tree

© Matthew Salerno, 2012

COMMIT

• A commit is a saved version (snapshot) of the working tree

• when git commit is executed, all ‘staged changes’, i.e. the current index, are saved to the local repo.

© Matthew Salerno, 2012

COMMIT

• A commit is a saved version (snapshot) of the working tree

• when git commit is executed, all ‘staged changes’, i.e. the current index, are saved to the local repo.

• the index is then cleared out, making room for future changes to be staged and saved to the local repo as future commits

© Matthew Salerno, 2012

COMMIT

• $ git status . to make sure all changes are staged

© Matthew Salerno, 2012

COMMIT

• $ git status . to make sure all changes are staged

• $ git commit –m ‘commit message’

© Matthew Salerno, 2012

COMMIT

If we leave off the –m tag, git will open VIM, PICO, or whatever editor it can find in our bash

settings to enter a commit message

© Matthew Salerno, 2012

COMMIT

If we leave off the –m tag, git will open VIM, PICO, or whatever editor it can find in our bash

settings to enter a commit message

Don’t forget the –m tag!

© Matthew Salerno, 2012

COMMIT

• git commit records the snapshot(index) of all staged content to the local repository

© Matthew Salerno, 2012

COMMIT

• git commit records the snapshot(index) of all staged content to the local repository

• Each commit is uniquely identified

© Matthew Salerno, 2012

COMMIT

• git commit records the snapshot(index) of all staged content to the local repository

• Each commit is uniquely identified • The commit can now be compared, shared, or

reverted to if necessary

© Matthew Salerno, 2012

RECAP

© Matthew Salerno, 2012

RECAP

Working Tree • Write, Delete, Save• Local Drive

© Matthew Salerno, 2012

RECAP

Working Tree • Write, Delete, Save• Local Drive

Index• Snapshot of the working tree • $ git add stages by updating the index

© Matthew Salerno, 2012

RECAP

Working Tree • Write, Delete, Save• Local Drive

Index• Snapshot of the working tree • $ git add stages by updating the index

Local Repository• Local repository stores commits

(versions) of the working tree• $ git commit saves all staged changes

(index) to local repo

© Matthew Salerno, 2012

Now we want to share with the world…

© Matthew Salerno, 2012

Remote Repo

© Matthew Salerno, 2012

Remote Repo

Step #1 – Create a remote repository

© Matthew Salerno, 2012

Remote Repo

Github will give you a URL for that repository, i.e.

git@github.com:username/reponame.git

© Matthew Salerno, 2012

Remote Repo

Step #2 – synchronize between local and remote repo

© Matthew Salerno, 2012

Remote Repo

• $ git remote add

© Matthew Salerno, 2012

Remote Repo

• $ git remote add– Allows us to set up an alias for our remote repo

© Matthew Salerno, 2012

Remote Repo

• $ git remote add– Allows us to set up an alias for our remote repo– $ git remote add [alias] git@github.com:

[username]/[reponame].git

© Matthew Salerno, 2012

PUSH!

Step #3 – push commits from local repo to remote repo

© Matthew Salerno, 2012

PUSH!

• $ git push [alias] [branch]

© Matthew Salerno, 2012

PUSH!

• $ git push [alias] [branch]– We’ll use master as our branch, but you could

push from any number of branches

© Matthew Salerno, 2012

PUSH!

• $ git push [alias] [branch]– We’ll use master as our branch, but you could

push from any number of branches– $ git push basic master

© Matthew Salerno, 2012

PUSH!

We’ve now successfully pushed our commit from the local repo to the remote repo.

© Matthew Salerno, 2012

PUSH!

We’ve now successfully pushed our commit from the local repo to the remote repo.

© Matthew Salerno, 2012

What if we want to work on someone else’s code?

© Matthew Salerno, 2012

FORK/CLONE

© Matthew Salerno, 2012

FORK

© Matthew Salerno, 2012

FORK

In forking someone’s else’s project repo, you are creating a new remote repository with identical

contents…

© Matthew Salerno, 2012

FORK

…but a forked repo only exists on github. To work on the project, we need to clone the repo

to our local machine…

© Matthew Salerno, 2012

CLONE

© Matthew Salerno, 2012

CLONE

$ git clone https://github.com/username/reponame.git

© Matthew Salerno, 2012

CLONE

Now we can interact with the cloned repo as we would any other (adding, commiting, pushing)

© Matthew Salerno, 2012

CLONE

We can also configure remote aliases pointing to the original forked repo to keep track of updates

to the original project, pull from the original repo, merge with our own files, etc.

© Matthew Salerno, 2012

CONCLUSION/RESOURCESTHE TOTEM POLE IS COMPLETE…

© Matthew Salerno, 2012

BASIC WORKFLOW

© Matthew Salerno, 2012

BASIC WORKFLOW

• Creating a local repo (init)

© Matthew Salerno, 2012

BASIC WORKFLOW

• Creating a local repo (init)• Snapshotting (add)

© Matthew Salerno, 2012

BASIC WORKFLOW

• Creating a local repo (init)• Snapshotting (add)• Commits (commit)

© Matthew Salerno, 2012

BASIC WORKFLOW

• Creating a local repo (init)• Snapshotting (add)• Commits (commit) • Remote repo (remote add, push)

© Matthew Salerno, 2012

BASIC WORKFLOW

• Creating a local repo (init)• Snapshotting (add)• Commits (commit) • Remote repo (remote add, push)• Interaction (fork, clone)

© Matthew Salerno, 2012

THERE’S MUCH, MUCH MORE

• Resources– GitHub• http://help.github.com

– GitReference• http://gitref.org

– Code School – Try Git• http://try.github.com

© Matthew Salerno, 2012

GOOD LUCK!

Recommended