50
Intro to Git Intro to Git Carl Brown Carl Brown

Introduction to Git Commands and Concepts

Embed Size (px)

DESCRIPTION

Introduction to git commands and concepts, primarily for subversion users

Citation preview

Page 1: Introduction to Git Commands and Concepts

Intro to GitIntro to GitCarl BrownCarl Brown

Page 2: Introduction to Git Commands and Concepts

SVN has one masterSVN has one master

Nothing happens in subversion-land without checking Nothing happens in subversion-land without checking with the master firstwith the master first

Clients are second-class denizensClients are second-class denizens

This is SlowThis is Slow

Limits options because of global lockingLimits options because of global locking

Forces Lowest-Common-Demoninator features because Forces Lowest-Common-Demoninator features because mistakes are public and fatal mistakes are public and fatal

Page 3: Introduction to Git Commands and Concepts

All GIT repos are All GIT repos are equalequal

A "master repo" is a policy decision, not a technical oneA "master repo" is a policy decision, not a technical one

Much fasterMuch faster

More feature-richMore feature-rich

Can perform complex operations without locking all Can perform complex operations without locking all other users out for an extended timeother users out for an extended time

Allows more risk because you can only hurt yourselfAllows more risk because you can only hurt yourself

Page 4: Introduction to Git Commands and Concepts

Git ConceptsGit Concepts

Page 5: Introduction to Git Commands and Concepts

A "commit"A "commit"

Just a diff file plus Just a diff file plus some metadatasome metadata

Basic unit of work in Basic unit of work in gitgit

Page 6: Introduction to Git Commands and Concepts

Commits in git are Commits in git are identified by identified by fingerprintfingerprintSHA-1 hashSHA-1 hash

If anything in the commit If anything in the commit changes, then SHA changes, then SHA changes, so it’s a new changes, so it’s a new commit (Q.E.D. commits are commit (Q.E.D. commits are immutable)immutable)

You can usually use only the You can usually use only the first few characters, as long first few characters, as long as they're uniqueas they're unique

Removes need for Removes need for authoritative server source authoritative server source for issuing IDs for issuing IDs

Identifies corrupted Identifies corrupted commitscommits

Page 7: Introduction to Git Commands and Concepts

All git commits contain All git commits contain their parents IDstheir parents IDs

So you can trace from So you can trace from any commit back to any commit back to the very first committhe very first commit

Changing the order of Changing the order of commits isn't possible commits isn't possible without changing the without changing the commit IDscommit IDs

Commits don't know Commits don't know their children though their children though (not known when (not known when they're created)they're created)

Page 8: Introduction to Git Commands and Concepts

Branches and tagsBranches and tagsJust pointersJust pointers

If you point at a single commit, you If you point at a single commit, you specify the entire history of the specify the entire history of the commitcommit

This makes branching a trivial This makes branching a trivial operationoperation

A single commit can be the HEAD A single commit can be the HEAD of dozens of branchesof dozens of branches

Branch early and oftenBranch early and often

Prolific branches allow git merges Prolific branches allow git merges to be explicit instead of implicit to be explicit instead of implicit (like 'svn update' is implicit)(like 'svn update' is implicit)

my “foo” branch may point to a my “foo” branch may point to a different commit than your “foo” different commit than your “foo” branch, but all “foobar” tags have branch, but all “foobar” tags have to point to the exact same committo point to the exact same commit

Page 9: Introduction to Git Commands and Concepts

““HEAD”HEAD”The tip commit of The tip commit of whatever branch whatever branch you’re currently you’re currently working with or working with or talking abouttalking about

Although you can use Although you can use “HEAD” in any place “HEAD” in any place that would normally that would normally take a branch name, take a branch name, if it’s in any way if it’s in any way ambiguous, even in ambiguous, even in your mind, use the your mind, use the branch name insteadbranch name instead

Page 10: Introduction to Git Commands and Concepts

"origin""origin"This is a convention in git This is a convention in git as an alias for the repo your as an alias for the repo your repository was cloned fromrepository was cloned from

While this is often the While this is often the organization’s authoritative organization’s authoritative server, it isn’t alwaysserver, it isn’t always

It’s just an alias for a URL in It’s just an alias for a URL in a config filea config file

If you clone from Mark, and If you clone from Mark, and I clone from you, then your I clone from you, then your repo’s “origin” points at repo’s “origin” points at Mark’s repo, but my “origin” Mark’s repo, but my “origin” points at your repopoints at your repo

Page 11: Introduction to Git Commands and Concepts

"master""master"This a convention in git for a This a convention in git for a "trunk" like branch. "trunk" like branch.

Some commands may break Some commands may break if it doesn't existif it doesn't exist

What you use it for is a policy What you use it for is a policy matter, not a technological matter, not a technological oneone

In Studio, origin/master is In Studio, origin/master is always “the thing QA always “the thing QA released most recently”released most recently”

Your local master can be Your local master can be whateverwhatever

Pick something that isn’t Pick something that isn’t likely to confuse you, thoughlikely to confuse you, though

Page 12: Introduction to Git Commands and Concepts

““origin master”origin master”(used in “push” and “pull” commands)(used in “push” and “pull” commands)

That tree - way over That tree - way over therethere

The default branch of The default branch of the repo you cloned the repo you cloned fromfrom

replace “origin” with replace “origin” with any repo name and any repo name and “master” with any “master” with any branch namebranch name

Page 13: Introduction to Git Commands and Concepts

““origin/master”origin/master” (“merge”, “reset” & “branch” commands)(“merge”, “reset” & “branch” commands)

A pointer to the A pointer to the commit that was the commit that was the HEAD of the default HEAD of the default branch of the repo branch of the repo you cloned from AS you cloned from AS OF THE LAST TIME OF THE LAST TIME YOU FETCHED FROM YOU FETCHED FROM THAT REPO.THAT REPO.

A picture of a tree is A picture of a tree is not a treenot a tree

Page 14: Introduction to Git Commands and Concepts

"index""index" (also called "cache" but really (also called "cache" but really "staging area""staging area"))

Place where changes Place where changes get put when you're get put when you're preparing for a preparing for a commitcommit

Page 15: Introduction to Git Commands and Concepts

““working set” orworking set” or “working directory” “working directory”

The files in the The files in the directory that you are directory that you are in the process of in the process of browsing, editing, browsing, editing, compiling, running compiling, running and/or testingand/or testing

The set of source The set of source code files that code files that Windows Explorer Windows Explorer displaysdisplays

Page 16: Introduction to Git Commands and Concepts

Interacting with Interacting with other reposother repos

Page 17: Introduction to Git Commands and Concepts

Most git Most git commands commands

affect only youaffect only you

Most git Most git commands commands

affect only youaffect only you

Except for:Except for:PushPushPullPull

FetchFetchCloneClone

(SVN/CVS/etc.)(SVN/CVS/etc.)

Obscure plumbingObscure plumbing

Page 18: Introduction to Git Commands and Concepts

git clonegit cloneCreates a new Creates a new repository on your repository on your local machinelocal machine

The new repo is a The new repo is a copy of the copy of the repository you cloned repository you cloned fromfrom

The alias “origin” is The alias “origin” is assigned in your new assigned in your new local repo to point to local repo to point to the one you cloned the one you cloned fromfrom

Page 19: Introduction to Git Commands and Concepts

git fetchgit fetchGets all changes from Gets all changes from remote reporemote repo

Puts those changes in Puts those changes in your local repoyour local repo

Does not affect your Does not affect your working tree or staging working tree or staging areaarea

Should not be any way Should not be any way you can get a conflict hereyou can get a conflict here

If there is a conflict, If there is a conflict, someone did something someone did something unwiseunwise

Page 20: Introduction to Git Commands and Concepts

git pullgit pull'git fetch' plus 'git merge' in one 'git fetch' plus 'git merge' in one "easier to type" command line"easier to type" command line

I don’t use this, I recommend you I don’t use this, I recommend you don’t eitherdon’t either

NEVER pull without specifying a NEVER pull without specifying a repo and branch, or git will repo and branch, or git will "guess""guess"

You wouldn't merge from You wouldn't merge from "wherever the source control "wherever the source control software feels like", you shouldn't software feels like", you shouldn't pull that way eitherpull that way either

Don’t pull it if you don’t know you Don’t pull it if you don’t know you want to be stuck with itwant to be stuck with it

prefer “fetch + merge” if you prefer “fetch + merge” if you have changes or “fetch + reset” have changes or “fetch + reset” if you don’tif you don’t

Page 21: Introduction to Git Commands and Concepts

git pushgit pushTake all changes associated with Take all changes associated with the specified branch and give the specified branch and give the specified remote server any the specified remote server any of them it is missingof them it is missing

If someone else doesn’t see If someone else doesn’t see your changes, you forgot to do your changes, you forgot to do thisthis

Always specify remote alias and Always specify remote alias and branchbranch

Like with the "pull" command, Like with the "pull" command, you shouldn’t let the software you shouldn’t let the software "guess""guess"

opposite of fetch (NOT the opposite of fetch (NOT the opposite of pull)opposite of pull)

never push to a repo that never push to a repo that contains a working directorycontains a working directory

Page 22: Introduction to Git Commands and Concepts

Working in your Working in your reporepo

Page 23: Introduction to Git Commands and Concepts

git addgit add

Puts changes into the Puts changes into the “staging area”“staging area”

Page 24: Introduction to Git Commands and Concepts

git commitgit commit

puts staged changes puts staged changes into the local into the local repositoryrepository

I try to do this every I try to do this every time I compiletime I compile

When in any doubt, When in any doubt, commit first!commit first!

Page 25: Introduction to Git Commands and Concepts

git statusgit status

Shows changes in Shows changes in your working your working directory and staging directory and staging areaarea

Page 26: Introduction to Git Commands and Concepts

git loggit log

Shows you the Shows you the history of where you history of where you areare

Usually, this is the Usually, this is the place to use a GUI place to use a GUI instead, thoughinstead, though How did I get here?How did I get here?

Page 27: Introduction to Git Commands and Concepts

git checkoutgit checkoutPut the files in your Put the files in your working directory in a working directory in a known stateknown state

Almost always Almost always corresponding to a corresponding to a particular local branchparticular local branch

You can use the ‘-b’ You can use the ‘-b’ flag to checkout to flag to checkout to make a new local make a new local branch and switch branch and switch your working directory your working directory to it at the same timeto it at the same time

Page 28: Introduction to Git Commands and Concepts

git branchgit branch

Make a new (local) Make a new (local) branch branch

List existing (local or List existing (local or cached) branchescached) branches

Delete a (local) Delete a (local) branchbranch

Page 29: Introduction to Git Commands and Concepts

git reset /git reset / git clean git clean

remove changes you remove changes you (hopefully) don’t want (hopefully) don’t want from your wayfrom your way

puts the working directory puts the working directory and/or the staging area and/or the staging area back in a known stateback in a known state

I use this a lot to sync up I use this a lot to sync up to a branch I don’t have to a branch I don’t have changes inchanges in

This can lose data - BE This can lose data - BE CAREFUL!!CAREFUL!!

When in any doubt, When in any doubt, commit first!commit first!

Page 30: Introduction to Git Commands and Concepts

git mergegit mergemerge 2 or more merge 2 or more branchesbranches

uses 3-way mergesuses 3-way merges

works REALLY wellworks REALLY well

any merge info will get any merge info will get lost if you push the lost if you push the result to SVNresult to SVN

No tool substitutes for a No tool substitutes for a lack of coordination!lack of coordination!

git mergetool helps with git mergetool helps with thisthis

Page 31: Introduction to Git Commands and Concepts

git rebasegit rebase

rearrange commits rearrange commits (illustration coming (illustration coming up later)up later)

do NOT do this if do NOT do this if someone else is someone else is sharing your changessharing your changes

do NOT share do NOT share changes if you’re changes if you’re going to do thisgoing to do this

Page 32: Introduction to Git Commands and Concepts

git helpgit help

fairly thorough, but fairly thorough, but often dense often dense documentationdocumentation

Other resources:Other resources:

http://git.or.cz/courhttp://git.or.cz/course/svn.htmlse/svn.html

http://utsl.gen.nz/http://utsl.gen.nz/talks/git-svn/talks/git-svn/intro.htmlintro.html

Page 33: Introduction to Git Commands and Concepts

Some things you Some things you probably shouldn’t doprobably shouldn’t do

‘‘git revert’ probably git revert’ probably doesn’t do what you thinkdoesn’t do what you think

if you do have to use it, if you do have to use it, clean up after yourself clean up after yourself as soon as you canas soon as you can

git push -fgit push -f

if you think you need to if you think you need to do this - something bad do this - something bad hopefully happened. hopefully happened.

Should not be routine.Should not be routine.

Page 34: Introduction to Git Commands and Concepts

Everyday workflowEveryday workflow

Page 35: Introduction to Git Commands and Concepts

Pro Tips and Best Pro Tips and Best PracticesPractices

commit as often as you build/compilecommit as often as you build/compile

a good commit diff, like a good method, should fit on your screena good commit diff, like a good method, should fit on your screen

don’t worry if it’s not good enough (no one can see it until you don’t worry if it’s not good enough (no one can see it until you push)push)

you might want to get back here (the next thing you do might you might want to get back here (the next thing you do might break)break)

you can always squash lateryou can always squash later

push when you would have committed in cvs/svnpush when you would have committed in cvs/svn

only push when you won’t “break the build” or when tests passonly push when you won’t “break the build” or when tests pass

don’t forget to push when you want other people to see your workdon’t forget to push when you want other people to see your work

branch for every complete functional set/bug fix/featurebranch for every complete functional set/bug fix/feature

keeping functionality separate makes life easier laterkeeping functionality separate makes life easier later

feel free to branch retroactively (but not what svn means by that)feel free to branch retroactively (but not what svn means by that)

Page 36: Introduction to Git Commands and Concepts

Simplified Dev Simplified Dev WorkflowWorkflow

(For Larger Teams)(For Larger Teams)Get latestGet latest

git fetch origingit fetch origin

Create a new branch for each bug or featureCreate a new branch for each bug or feature

git checkout -b ${feature} origin/${oldbranch}git checkout -b ${feature} origin/${oldbranch}

Make your changes as before Make your changes as before

(hopefully committing more often)(hopefully committing more often)

Check in Your Changes LocallyCheck in Your Changes Locally

git commit -am “An Intelligent Comment”git commit -am “An Intelligent Comment”

Push changes back where others can find themPush changes back where others can find them

git push origin ${feature}git push origin ${feature}

Later, merge feature branches together to make Release Later, merge feature branches together to make Release CandidatesCandidates

Page 37: Introduction to Git Commands and Concepts

Making Release Making Release Candidate Candidate BranchesBranches

Page 38: Introduction to Git Commands and Concepts

Three Different Three Different Combining OptionsCombining Options

MergeMerge

Keeps HistoryKeeps History

Should Use --no-ffShould Use --no-ff

Harder to trace later in Harder to trace later in logslogs

RebaseRebase

Simplifies History in LogsSimplifies History in Logs

Better for things without Better for things without common recent parentcommon recent parent

Loses some history infoLoses some history info

Cherry-PickCherry-Pick

Just like rebase but for Just like rebase but for single commitssingle commits

Page 39: Introduction to Git Commands and Concepts

MergingMerging““git checkout $git checkout ${destination branch}”{destination branch}”

““git merge ${feature git merge ${feature branch}”branch}”

““git mergetool” if git mergetool” if conflictsconflicts

““git checkout --ours” git checkout --ours” or “git checkout --or “git checkout --theirs” if you want to theirs” if you want to pick onepick one

““git merge ${second git merge ${second feature branch}”feature branch}”

Page 40: Introduction to Git Commands and Concepts

RebaseRebase““git checkout ${feature git checkout ${feature branch}”branch}”

““git checkout -b ${new git checkout -b ${new name}” to copyname}” to copy

““git rebase ${destination git rebase ${destination branch}” branch}”

if you want every commit if you want every commit back to common point, orback to common point, or

““git rebase --onto git rebase --onto ${destination branch} ${destination branch} ${last commit not to take} ${last commit not to take} ${feature branch}”${feature branch}”

If you want a subsetIf you want a subset

Page 41: Introduction to Git Commands and Concepts

Rebasing ExampleRebasing Exampleinitial stateinitial state

Figures adapted from Figures adapted from Pro GitPro Git by Scott Chacon by Scott Chacon

C1C1C1C1

mastemasterr

mastemasterr

C2C2C2C2 C5C5C5C5 C6C6C6C6

C3C3C3C3 C4C4C4C4 C7C7C7C7

C8C8C8C8 C9C9C9C9serverserverserverserver

clientclientclientclient

Page 42: Introduction to Git Commands and Concepts

Rebasing ExampleRebasing Examplegit checkout servergit checkout servergit rebase mastergit rebase master Figures adapted from Figures adapted from Pro GitPro Git by Scott Chacon by Scott Chacon

C1C1C1C1

mastemasterr

mastemasterr

C2C2C2C2 C5C5C5C5 C6C6C6C6

C3C3C3C3 C4C4C4C4 C7C7C7C7

C8C8C8C8 C9C9C9C9serverserverserverserver

clientclientclientclient

C3C3C3C3 C4C4C4C4 C7C7C7C7

Page 43: Introduction to Git Commands and Concepts

Rebasing ExampleRebasing Examplegit checkout servergit checkout servergit rebase mastergit rebase master Figures adapted from Figures adapted from Pro GitPro Git by Scott Chacon by Scott Chacon

C1C1C1C1

mastemasterr

mastemasterr

C2C2C2C2 C5C5C5C5 C6C6C6C6

C3C3C3C3 C4C4C4C4 C7C7C7C7

C8C8C8C8 C9C9C9C9

serverserverserverserver

clientclientclientclient

C3C3C3C3 C4C4C4C4 C7C7C7C7

Page 44: Introduction to Git Commands and Concepts

Rebasing ExampleRebasing Examplegit checkout servergit checkout servergit rebase mastergit rebase master Figures adapted from Figures adapted from Pro GitPro Git by Scott Chacon by Scott Chacon

C1C1C1C1

mastemasterr

mastemasterr

C2C2C2C2 C5C5C5C5 C6C6C6C6

C3C3C3C3 C4C4C4C4 C7C7C7C7

C8C8C8C8 C9C9C9C9

serverserverserverserver

clientclientclientclient

C3’C3’C3’C3’ C4’C4’C4’C4’ C7’C7’C7’C7’

Page 45: Introduction to Git Commands and Concepts

Rebasing ExampleRebasing Exampleinitial stateinitial state

Figures adapted from Figures adapted from Pro GitPro Git by Scott Chacon by Scott Chacon

C1C1C1C1

mastemasterr

mastemasterr

C2C2C2C2 C5C5C5C5 C6C6C6C6

C3C3C3C3 C4C4C4C4 C7C7C7C7

C8C8C8C8 C9C9C9C9serverserverserverserver

clientclientclientclient

Page 46: Introduction to Git Commands and Concepts

Rebasing ExampleRebasing Examplegit rebase --onto master server clientgit rebase --onto master server client

Figures adapted from Figures adapted from Pro GitPro Git by Scott Chacon by Scott Chacon

C1C1C1C1

mastemasterr

mastemasterr

C2C2C2C2 C5C5C5C5 C6C6C6C6

C3C3C3C3 C4C4C4C4 C7C7C7C7

C8C8C8C8 C9C9C9C9serverserverserverserver

clientclientclientclient

Page 47: Introduction to Git Commands and Concepts

Rebasing ExampleRebasing Examplegit rebase --onto master server clientgit rebase --onto master server client

Figures adapted from Figures adapted from Pro GitPro Git by Scott Chacon by Scott Chacon

C1C1C1C1

mastemasterr

mastemasterr

C2C2C2C2 C5C5C5C5 C6C6C6C6

C3C3C3C3 C4C4C4C4 C7C7C7C7

C8C8C8C8 C9C9C9C9

serverserverserverserver

clientclientclientclient

Page 48: Introduction to Git Commands and Concepts

Rebasing ExampleRebasing Examplegit rebase --onto master server clientgit rebase --onto master server client

Figures adapted from Figures adapted from Pro GitPro Git by Scott Chacon by Scott Chacon

C1C1C1C1

mastemasterr

mastemasterr

C2C2C2C2 C5C5C5C5 C6C6C6C6

C3C3C3C3 C4C4C4C4 C7C7C7C7

C8’C8’C8’C8’ C9’C9’C9’C9’

serverserverserverserver

clientclientclientclient

Page 49: Introduction to Git Commands and Concepts

Staying current with Staying current with SVNSVN

git svn fetchgit svn fetch

Doesn’t update working Doesn’t update working tree, but gets branchestree, but gets branches

git svn rebasegit svn rebase

puts SVN revision IDs in puts SVN revision IDs in git commentsgit comments

have to do this to stay have to do this to stay in syncin sync

Loses git history, so Loses git history, so make copied branches make copied branches in git firstin git first

Page 50: Introduction to Git Commands and Concepts

Pushing back to SVNPushing back to SVNgit svn dcommitgit svn dcommit

simplest and safest on SVN simplest and safest on SVN side side

requires you rebase in requires you rebase in latest SVN changeslatest SVN changes

Make a copied branch in git Make a copied branch in git to do this onto do this on

May be preceded by “git May be preceded by “git reset --hard $git_branch”reset --hard $git_branch”

git svn set-treegit svn set-tree

Overwrites SVN ignorantly Overwrites SVN ignorantly (with extreme prejudice)(with extreme prejudice)

nuking the site from Orbitnuking the site from Orbit