Gitting It Under (Version) Control

61
Gitting it under (version) control

Transcript of Gitting It Under (Version) Control

Page 1: Gitting It Under (Version) Control

Gitting it under(version) control

Page 2: Gitting It Under (Version) Control

The Plan

What is Version Control?

Git Basics

Workflows

Other fun stuff

••••

Page 3: Gitting It Under (Version) Control

What is VersionControl?

It’s exactly what it sounds like.

Sometimes called SCM (source controlmanagement).

One codebase, one project.

Lots of people changing it to get thingsdone at the same time.

How do we track those changes?

How do we make sure I don’t step over your toes?

Page 4: Gitting It Under (Version) Control

Different Approaches

Distributed vs. Centralized

Delta vs. Directed Acyclic Graph (DAG)

••

Page 5: Gitting It Under (Version) Control

Muscle Memory FTW

# 0. Get a copy of the repositorygit clone [email protected]:user/repo# 1. Before you do any work...git pull# 2. Do work, edit files, but when you want to share it...git commit -am "commit message"# 3. Now send it to everyone elsegit push# GOTO Step 1.

Page 6: Gitting It Under (Version) Control

Setting Up

Download Installerfrom http://git-scm.com/

A few commands youwant to run right away:

git config --global user.name "Ankur Oberoi"git config --global user.email "[email protected]"git config --global color.ui true

Page 7: Gitting It Under (Version) Control

git commandsPorcelain (high-level, “nice” for users)

Plumbing (low-level, deals with database & filesystem)

git−add git−commit git−log git−reset git−branch git−diff git−mv git−status git−checkout git−fetch git−merge git−push git−clone git-init git−pull git−tag git-archive git-bisect git-bundle git-cherry-pickgit-citool git-clean git-describe git-format-patch... ... ... ...

git−apply git−checkout−index git−commit−tree git−hash−objectgit−index−pack git−init−db git−merge−index git−mktaggit−mktree git−pack−objects git−prune−packed git−read−treegit−repo−config git−unpack−objects git−update−index git−write−treegit−cat−file git−describe git−diff−index git−diff−filesgit-diff-index git-diff-tree git-for-each-ref git-ls-files... ... ... ...

Page 8: Gitting It Under (Version) Control

Getting a repository

One project, one .git (hidden) directory

Start it from initial code locally

git init

Clone it from somewhere elsegit clone URL

••

••

Page 9: Gitting It Under (Version) Control

Local Repository Operations

Working Directory“Working Tree”

Staging Area“Index”

Repository“Database”

Page 10: Gitting It Under (Version) Control

Local Repository Operations

Working Directory“Working Tree”

Staging Area“Index”

Repository“Database”

* AwesomeProj *

AwesomeProjdoes x and y.

Readme.md

Page 11: Gitting It Under (Version) Control

Local Repository Operations

Working Directory“Working Tree”

Staging Area“Index”

Repository“Database”

* AwesomeProj *

AwesomeProjdoes x and y.

Readme.md

git add Readme.md

Page 12: Gitting It Under (Version) Control

Local Repository Operations

Working Directory“Working Tree”

Staging Area“Index”

Repository“Database”

* AwesomeProj *

AwesomeProjdoes x and y.

Readme.md

git add Readme.md git commit -m “commit mess”

Page 13: Gitting It Under (Version) Control

Local Repository Operations

Readme.md

Working Directory“Working Tree”

Staging Area“Index”

Repository“Database”

git status

# On branch masternothing to commit (working directory clean)

Page 14: Gitting It Under (Version) Control

Readme.md

Local Repository Operations

Working Directory“Working Tree”

Staging Area“Index”

Repository“Database”

* AwesomeProj *

AwesomeProjdoes x, y and z.

Readme.md

git status

# On branch master# Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in workingdirectory)## modified: Readme.md#no changes added to commit (use "git add" and/or "git commit -a")

Page 15: Gitting It Under (Version) Control

Readme.md

Local Repository Operations

Working Directory“Working Tree”

Staging Area“Index”

Repository“Database”

* AwesomeProj *

AwesomeProjdoes x, y and z.

Readme.md

git add Readme.md# On branch master# Changes to be committed:# (use "git reset HEAD <file>..."to unstage)## modified: Readme.md#

git status

Page 16: Gitting It Under (Version) Control

Local Repository Operations

Working Directory“Working Tree”

Staging Area“Index”

Repository“Database”

* AwesomeProj *

AwesomeProjdoes x, y and z.

Readme.md

git commit -m “new feat”

Readme.md[master ad9e3bf] new feat1 files changed, 1 insertions(+), 1 deletions(-)

Page 17: Gitting It Under (Version) Control

Local Repository Operations

Working Directory“Working Tree”

Staging Area“Index”

Repository“Database”

Readme.md

git status

# On branch masternothing to commit (working directory clean)

Page 18: Gitting It Under (Version) Control

Local Repository Operations

Working Directory“Working Tree”

Staging Area“Index”

Repository“Database”

git add Readme.md git commit -m “commit mess”

git reset HEAD Readme.mdgit reset HEAD Readme.md

Page 19: Gitting It Under (Version) Control

Local Repository Operations

Working Directory“Working Tree”

Staging Area“Index”

Repository“Database”

git add Readme.md git commit -m “commit mess”

git reset HEAD Readme.mdgit reset HEAD Readme.md

Xgit checkout -- Readme.mdgit checkout -- Readme.md

Xgit checkout HEAD -- Readme.mdgit checkout HEAD -- Readme.mdgit reset --hard [HEAD]git reset --hard [HEAD]

Page 20: Gitting It Under (Version) Control

Branching & Merging

What is a commit?

An object that points to trees/blobs in the .git directory

addressed by a SHA1 hash (ad9e3b)

contains info about author/commiter

also contains pointers to “parent commits”

•••••

ad9e3b

Page 21: Gitting It Under (Version) Control

Branching & Merging

What is a branch?

A pointer to a commit

logically, a separate concern in development

What is HEAD?

A pointer to a branch

Usually represents whats in the WT *now*

•••

•••

ad9e3b master HEAD

Page 22: Gitting It Under (Version) Control

Branching & Merging

ad9e3b master HEAD

git commit -m “new feature”

Page 23: Gitting It Under (Version) Control

Branching & Merging

ad9e3b master HEAD

git commit -m “new feature”

Page 24: Gitting It Under (Version) Control

Branching & Merging

ad9e3b

master HEAD

git commit -m “new feature”

c88da7

Page 25: Gitting It Under (Version) Control

Branching & Merging

ad9e3b

master HEAD

git branch issue8

c88da7

issue8

New branch for a separate logical concern

Page 26: Gitting It Under (Version) Control

Branching & Merging

ad9e3b

master

HEAD

git checkout issue8

c88da7

issue8

Checkout actually changes the files in your WorkingTree

but... in this case WT is identical

Page 27: Gitting It Under (Version) Control

Branching & Merging

ad9e3b

master

HEAD

# Make changes in editor, add changes to staging...git commit -m “redoing data structure”

c88da7

issue8b9711f

New commits have pointers to parent(s)

Page 28: Gitting It Under (Version) Control

Branching & Merging

ad9e3b

master HEAD

git checkout master

c88da7

issue8b9711f

Checkout actually changes the files in your WorkingTree

Page 29: Gitting It Under (Version) Control

Branching & Merging

ad9e3b

master HEAD

# Making more commits...

c88da7 issue8b9711f

f327e3

Page 30: Gitting It Under (Version) Control

Branching & Merging

ad9e3b

master HEAD

# Now we need to get that fix for issue8 into mastergit merge issue8

c88da7 issue8b9711f

f327e3

Page 31: Gitting It Under (Version) Control

Branching & Merging# Now we need to get that fix for issue8 into mastergit merge issue8

ad9e3b

c88da7 issue8b9711f

f327e3

master HEADa77106

This is known as a “merge commit”

Page 32: Gitting It Under (Version) Control

Sharing Code withRemotes

How do we share code with others?

git has stored URLs with aliases called “remotes”

a few operations to move code from your local repositoryto and from a remote

you get local copies of remote branches in your repo

*branch related operations happen locally

•••

••

Page 33: Gitting It Under (Version) Control

Remotes

By default, you always get one remotewhen you clone, it is called “origin”

You can add more remotes using:

•git remote add github [email protected]:aoberoi/testproj.git

This URL can be ssh:// (most common),http://, https://, git://, or even file:///

Page 34: Gitting It Under (Version) Control

Using Remotesmaster HEADf327e3

Page 35: Gitting It Under (Version) Control

Using Remotes

master HEAD

f327e3

origin/master HEAD

Now what if someone else adds commits to origin?

Page 36: Gitting It Under (Version) Control

Using Remotes

master HEAD

f327e3

origin/master HEAD3602ea

This new commit only exists on the server...git fetch origin

Page 37: Gitting It Under (Version) Control

Using Remotes

master HEAD

f327e3

origin/master HEAD3602ea

Now we have a local copy.

How do we continue working where the latestfrom the server left off?

Page 38: Gitting It Under (Version) Control

Using Remotes

master HEAD

f327e3

origin/master HEAD3602ea

How do we continue working where the latestfrom the server left off?

git merge origin/master

Page 39: Gitting It Under (Version) Control

Using Remotes

master HEADf327e3

origin/master HEAD3602ea

git merge origin/master

Fast-forward merge: my commit is in the history ofwhat is to be merged in

Page 40: Gitting It Under (Version) Control

Using Remotes

master HEADf327e3

origin/master HEAD3602ea

Pull is a shortcut:git pull = git fetch + git merge

No need to specify the argument to merge if you areon a “tracking branch”

Page 41: Gitting It Under (Version) Control

Creating a TrackingBranch

If theres a new branch available on theserver and you want to participate indeveloping on that “logical concern”

git checkout -t origin/next-big-thing

You are not checking out a remotebranch,

you are creating a new one

Page 42: Gitting It Under (Version) Control

Pushing Changes

Pushing can only be fast-forward commits,this prevents you from abandoningsomeone else’s changes

git push origin master

Page 43: Gitting It Under (Version) Control

Inspection and History

git log --pretty=oneline --graph

git diff [--cached]

git status

git remote show

••••

Page 44: Gitting It Under (Version) Control

Other Neat Tricks

git add -u

git stash

git push -u origin master

git push origin :badfeature

git log feature ^master

git add -i

••••••

Page 45: Gitting It Under (Version) Control

Gotchas

Binary files? Images? Videos? Databases?

Detached HEAD

non fast-forward

git rm / git mv

••••

Page 46: Gitting It Under (Version) Control

Workflows

Page 47: Gitting It Under (Version) Control

master is alwaysdeployable

Good general convention

Keep actual development on ‘topic’branches

When topic is complete and code istested → merge (or pull request)

••

•master

issue-8

Page 48: Gitting It Under (Version) Control

issue-8master is always

deployableGood general convention

Keep actual development on ‘topic’branches

When topic is complete and code istested → merge (or pull request)

••

•master

Page 49: Gitting It Under (Version) Control

Local Development

Common pieces to share:

unit tests

graphics

utility scripts

Keep your own preferences on your localmachine and don’t commit them

••••

Page 50: Gitting It Under (Version) Control

.gitignore

A file with filename patterns that youdon’t want to become part of your index(nor your commits)

See samples for all types of platforms:https://github.com/github/gitignore/

Page 51: Gitting It Under (Version) Control

Mark versions withTags

Tags are like “unmovable” branches

This makes it a good bookmark to keeptrack of code that was released

“I need to patch a bug that my customer sees, but whatcode did I deploy?”

••

Page 52: Gitting It Under (Version) Control

Github Pull-Requests

You don’t always have access to push ifyou are contributing to open source, send apull-request

Pull-requests work from branch to branch

use this for collaborative development

code review

http://www.confreaks.com/videos/706-rubyconf2011-how-github-uses-github-to-

•••

Page 53: Gitting It Under (Version) Control

Github Forks

Just a “clone” that you initialize on aremote server, instead of on your machine

Holds a little bit of history for the projectso you can track relationships in a“network”

Page 54: Gitting It Under (Version) Control

IDE and GUI ToolsXcode

Page 55: Gitting It Under (Version) Control

IDE and GUI Tools

Eclipse Egit

Page 56: Gitting It Under (Version) Control

IDE and GUI Tools

Platform Specific Tools

Tower for Mac

TortoiseGit for Windows

Cross Platform

gitk

•••

••

Page 57: Gitting It Under (Version) Control

Feeling more “at home”

Bash Completion

Merge Tool

Commit Message Editor

Aliases

••••

Page 58: Gitting It Under (Version) Control

Left as an exercise forthe readers

cherry-picking

“rewriting” history

git-svn

git modules

environment variables and $GIT_DIR

Treeishes by ‘x’spec

Writing your own git hooks

bare repositories

Page 59: Gitting It Under (Version) Control

ResourcesGit Cheatsheet (visual)

http://ndpsoftware.com/git-cheatsheet.html

Github Helphttp://help.github.com/git-cheat-sheets/

Pro Githttp://progit.org/book/

Git Community Bookhttp://book.git-scm.com/

Google Tech Talk with Linus Torvaldshttp://www.youtube.com/watch?

Page 60: Gitting It Under (Version) Control

Erasing anyPreconceived Notions

If you came from svn, you have a separateunderstanding of version control

commits happen locally (actually almosteverything happens locally)

version numbers are not monotonicallyincreasing

branches are not global (pollution ofnamespace?)

merging is (relatively) cheap, can be doneby mere mortals

Page 61: Gitting It Under (Version) Control

Why distributed overcentralized?

Offline development

Easy to do experimental work

... code review

... other workflowy specific purposes(flexibility)

“commit access” politics are avoided

backups

speed