92
Git Basics: Climbing the Totem Pole by Matthew Salerno Github: https://github.com/seldomatt Twitter:@seldomatt Linkedin: http://www.linkedin.com/pub/matthew-salerno/9/62b/584 © Matthew Salerno, 2012

Git Basics: Climbing the Totem Pole

Embed Size (px)

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

Page 1: Git Basics: Climbing the Totem Pole

© 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

Page 2: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git vs. Github

Page 3: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git vs. Github

GIT• Revision control and source

code management system

Page 4: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git vs. Github

GIT• Revision control and source

code management system • Created by Linus Torvalds

(Linux) in 2005

Page 5: Git Basics: Climbing the Totem Pole

© 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

Page 6: Git Basics: Climbing the Totem Pole

© 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

Page 7: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git vs. Github

• GitHub – has a graphical user interface

Page 8: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git vs. Github

• GitHub

Page 9: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git vs. Github

• Git – run through the command line

Page 10: Git Basics: Climbing the Totem Pole

© 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

Page 11: Git Basics: Climbing the Totem Pole

© 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)

Page 12: Git Basics: Climbing the Totem Pole

© 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…

Page 13: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git vs. Github

• Git

Page 14: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

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

Page 15: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

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

Page 16: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

What We Will Cover

• Init – creating a local repository

Page 17: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

What We Will Cover

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

Page 18: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

What We Will Cover

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

Page 19: Git Basics: Climbing the Totem Pole

© 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

Page 20: Git Basics: Climbing the Totem Pole

© 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)

Page 21: Git Basics: Climbing the Totem Pole

© 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

Page 22: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

What We Won’t Cover

• Installing Git

Page 23: Git Basics: Climbing the Totem Pole

© 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

Page 24: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

GIT: BUILDING THE TOTEM POLE

Page 25: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

WORKING TREE

Page 26: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

WORKING TREE

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

Page 27: Git Basics: Climbing the Totem Pole

© 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)

Page 28: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Local Repository

Page 29: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Local Repository

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

Page 30: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Let’s create a local git repository

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

Page 31: Git Basics: Climbing the Totem Pole

© 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 .

Page 32: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Working Tree

Page 33: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Working Tree

Local Repository

Page 34: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Working Tree

Local Repository

What’s Missing?

Page 35: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Index/Staging Area

Page 36: Git Basics: Climbing the Totem Pole

© 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

Page 37: Git Basics: Climbing the Totem Pole

© 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)

Page 38: Git Basics: Climbing the Totem Pole

© 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

Page 39: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git Add

• Make some changes to the working tree

Page 40: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git Add

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

Page 41: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git Add

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

Page 42: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git Add

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

Page 43: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Git Add

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

Page 44: Git Basics: Climbing the Totem Pole

© 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

Page 45: Git Basics: Climbing the Totem Pole

© 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.

Page 46: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

COMMIT

Page 47: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

COMMIT

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

Page 48: Git Basics: Climbing the Totem Pole

© 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.

Page 49: Git Basics: Climbing the Totem Pole

© 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

Page 50: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

COMMIT

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

Page 51: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

COMMIT

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

• $ git commit –m ‘commit message’

Page 52: Git Basics: Climbing the Totem Pole

© 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

Page 53: Git Basics: Climbing the Totem Pole

© 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!

Page 54: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

COMMIT

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

Page 55: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

COMMIT

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

• Each commit is uniquely identified

Page 56: Git Basics: Climbing the Totem Pole

© 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

Page 57: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

RECAP

Page 58: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

RECAP

Working Tree • Write, Delete, Save• Local Drive

Page 59: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

RECAP

Working Tree • Write, Delete, Save• Local Drive

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

Page 60: Git Basics: Climbing the Totem Pole

© 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

Page 61: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Now we want to share with the world…

Page 62: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Remote Repo

Page 63: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Remote Repo

Step #1 – Create a remote repository

Page 64: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Remote Repo

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

[email protected]:username/reponame.git

Page 65: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Remote Repo

Step #2 – synchronize between local and remote repo

Page 66: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Remote Repo

• $ git remote add

Page 67: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Remote Repo

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

Page 68: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

Remote Repo

• $ git remote add– Allows us to set up an alias for our remote repo– $ git remote add [alias] [email protected]:

[username]/[reponame].git

Page 69: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

PUSH!

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

Page 70: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

PUSH!

• $ git push [alias] [branch]

Page 71: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

PUSH!

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

push from any number of branches

Page 72: Git Basics: Climbing the Totem Pole

© 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

Page 73: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

PUSH!

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

Page 74: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

PUSH!

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

Page 75: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

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

Page 76: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

FORK/CLONE

Page 77: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

FORK

Page 78: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

FORK

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

contents…

Page 79: Git Basics: Climbing the Totem Pole

© 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…

Page 80: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

CLONE

Page 81: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

CLONE

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

Page 82: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

CLONE

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

Page 83: Git Basics: Climbing the Totem Pole

© 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.

Page 84: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

CONCLUSION/RESOURCESTHE TOTEM POLE IS COMPLETE…

Page 85: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

BASIC WORKFLOW

Page 86: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

BASIC WORKFLOW

• Creating a local repo (init)

Page 87: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

BASIC WORKFLOW

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

Page 88: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

BASIC WORKFLOW

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

Page 89: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

BASIC WORKFLOW

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

Page 90: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

BASIC WORKFLOW

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

Page 91: Git Basics: Climbing the Totem Pole

© 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

Page 92: Git Basics: Climbing the Totem Pole

© Matthew Salerno, 2012

GOOD LUCK!