O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Introduction to Git Commands and Concepts

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
Introduction to Git and Github
Introduction to Git and Github
Carregando em…3
×

Confira estes a seguir

1 de 50 Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (20)

Quem viu também gostou (20)

Anúncio

Semelhante a Introduction to Git Commands and Concepts (20)

Mais de Carl Brown (20)

Anúncio

Mais recentes (20)

Introduction to Git Commands and Concepts

  1. 1. Intro to GitIntro to Git Carl BrownCarl Brown
  2. 2. SVN has one masterSVN has one master Nothing happens in subversion-land without checking with theNothing happens in subversion-land without checking with the master firstmaster 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 mistakes areForces Lowest-Common-Demoninator features because mistakes are public and fatalpublic and fatal
  3. 3. All GIT repos are equalAll GIT repos are equal 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 other users outCan perform complex operations without locking all other users out for an extended timefor an extended time Allows more risk because you can only hurt yourselfAllows more risk because you can only hurt yourself
  4. 4. Git ConceptsGit Concepts
  5. 5. A "commit"A "commit" Just a diff file plus someJust a diff file plus some metadatametadata Basic unit of work in gitBasic unit of work in git
  6. 6. Commits in git are identifiedCommits in git are identified by fingerprintby fingerprint SHA-1 hashSHA-1 hash If anything in the commitIf anything in the commit changes, then SHA changes, sochanges, then SHA changes, so it’s a new commit (Q.E.D.it’s a new commit (Q.E.D. commits are immutable)commits are immutable) You can usually use only theYou can usually use only the first few characters, as long asfirst few characters, as long as they're uniquethey're unique Removes need forRemoves need for authoritative server source forauthoritative server source for issuing IDsissuing IDs Identifies corrupted commitsIdentifies corrupted commits
  7. 7. All git commits contain theirAll git commits contain their parents IDsparents IDs So you can trace from anySo you can trace from any commit back to the verycommit back to the very first commitfirst commit Changing the order ofChanging the order of commits isn't possiblecommits isn't possible without changing thewithout changing the commit IDscommit IDs Commits don't know theirCommits don't know their children though (notchildren though (not known when they'reknown when they're created)created)
  8. 8. Branches and tagsBranches and tags Just pointersJust pointers If you point at a single commit, youIf you point at a single commit, you specify the entire history of the commitspecify the entire history of the commit This makes branching a trivial operationThis makes branching a trivial operation A single commit can be the HEAD ofA single commit can be the HEAD of dozens of branchesdozens of branches Branch early and oftenBranch early and often Prolific branches allow git merges to beProlific branches allow git merges to be explicit instead of implicit (like 'svnexplicit instead of implicit (like 'svn update' is implicit)update' is implicit) my “foo” branch may point to a differentmy “foo” branch may point to a different commit than your “foo” branch, but allcommit than your “foo” branch, but all “foobar” tags have to point to the exact“foobar” tags have to point to the exact same commitsame commit
  9. 9. ““HEAD”HEAD” The tip commit ofThe tip commit of whatever branch you’rewhatever branch you’re currently working with orcurrently working with or talking abouttalking about Although you can useAlthough you can use “HEAD” in any place that“HEAD” in any place that would normally take awould normally take a branch name, if it’s in anybranch name, if it’s in any way ambiguous, even inway ambiguous, even in your mind, use the branchyour mind, use the branch name insteadname instead
  10. 10. "origin""origin" This is a convention in git asThis is a convention in git as an alias for the repo youran alias for the repo your repository was cloned fromrepository was cloned from While this is often theWhile this is often the organization’s authoritativeorganization’s authoritative server, it isn’t alwaysserver, it isn’t always It’s just an alias for a URL in aIt’s just an alias for a URL in a config fileconfig file If you clone from Mark, and IIf you clone from Mark, and I clone from you, then yourclone from you, then your repo’s “origin” points at Mark’srepo’s “origin” points at Mark’s repo, but my “origin” points atrepo, but my “origin” points at your repoyour repo
  11. 11. "master""master" This a convention in git for aThis a convention in git for a "trunk" like branch."trunk" like branch. Some commands may break if itSome commands may break if it doesn't existdoesn't exist What you use it for is a policyWhat you use it for is a policy matter, not a technological onematter, not a technological one In Studio, origin/master is alwaysIn Studio, origin/master is always “the thing QA released most“the thing QA released most recently”recently” Your local master can be whateverYour local master can be whatever Pick something that isn’t likely toPick something that isn’t likely to confuse you, thoughconfuse you, though
  12. 12. ““origin master”origin master” (used in “push” and “pull” commands)(used in “push” and “pull” commands) (used in “push” and “pull” commands)(used in “push” and “pull” commands) That tree - way over thereThat tree - way over there The default branch of theThe default branch of the repo you cloned fromrepo you cloned from replace “origin” with anyreplace “origin” with any repo name and “master”repo name and “master” with any branch namewith any branch name
  13. 13. ““origin/master”origin/master” (“merge”,“reset” & “branch” commands)(“merge”,“reset” & “branch” commands) (“merge”,“reset” & “branch” commands)(“merge”,“reset” & “branch” commands) A pointer to the commitA pointer to the commit that was the HEAD of thethat was the HEAD of the default branch of the repodefault branch of the repo you cloned from AS OFyou cloned from AS OF THE LAST TIMEYOUTHE LAST TIMEYOU FETCHED FROM THATFETCHED FROM THAT REPO.REPO. A picture of a tree is not aA picture of a tree is not a treetree
  14. 14. "index""index" (also called "cache" but really(also called "cache" but really "staging area""staging area")) )) Place where changes getPlace where changes get put when you're preparingput when you're preparing for a commitfor a commit
  15. 15. ““working set” orworking set” or “working directory”“working directory” ““working directory”working directory” The files in the directoryThe files in the directory that you are in the processthat you are in the process of browsing, editing,of browsing, editing, compiling, running and/orcompiling, running and/or testingtesting The set of source codeThe set of source code files that Windowsfiles that Windows Explorer displaysExplorer displays
  16. 16. Interacting with otherInteracting with other reposrepos
  17. 17. Most git commandsMost git commands affect only youaffect only you Except for:Except for: PushPush PullPull FetchFetch CloneClone (SVN/CVS/etc.)(SVN/CVS/etc.) Obscure plumbingObscure plumbing
  18. 18. git clonegit clone Creates a new repositoryCreates a new repository on your local machineon your local machine The new repo is a copy ofThe new repo is a copy of the repository you clonedthe repository you cloned fromfrom The alias “origin” isThe alias “origin” is assigned in your new localassigned in your new local repo to point to the onerepo to point to the one you cloned fromyou cloned from
  19. 19. git fetchgit fetch Gets all changes from remoteGets all changes from remote reporepo Puts those changes in yourPuts those changes in your local repolocal repo Does not affect your workingDoes not affect your working tree or staging areatree or staging area Should not be any way youShould not be any way you can get a conflict herecan get a conflict here If there is a conflict, someoneIf there is a conflict, someone did something unwisedid something unwise
  20. 20. git pullgit pull 'git fetch' plus 'git merge' in one "easier'git fetch' plus 'git merge' in one "easier to type" command lineto type" command line I don’t use this, I recommend you don’tI don’t use this, I recommend you don’t eithereither NEVER pull without specifying a repoNEVER pull without specifying a repo and branch, or git will "guess"and branch, or git will "guess" You wouldn't merge from "whereverYou wouldn't merge from "wherever the source control software feels like",the source control software feels like", you shouldn't pull that way eitheryou shouldn't pull that way either Don’t pull it if you don’t know youDon’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 haveprefer “fetch + merge” if you have changes or “fetch + reset” if you don’tchanges or “fetch + reset” if you don’t
  21. 21. git pushgit push Take all changes associated with theTake all changes associated with the specified branch and give thespecified branch and give the specified remote server any of themspecified remote server any of them it is missingit is missing If someone else doesn’t see yourIf someone else doesn’t see your changes, you forgot to do thischanges, you forgot to do this Always specify remote alias andAlways specify remote alias and branchbranch Like with the "pull" command, youLike with the "pull" command, you shouldn’t let the software "guess"shouldn’t let the software "guess" opposite of fetch (NOT the oppositeopposite of fetch (NOT the opposite of pull)of pull) never push to a repo that contains anever push to a repo that contains a working directoryworking directory
  22. 22. Working in your repoWorking in your repo
  23. 23. git addgit add Puts changes into thePuts changes into the “staging area”“staging area”
  24. 24. git commitgit commit puts staged changes intoputs staged changes into the local repositorythe local repository I try to do this every timeI try to do this every time I compileI compile When in any doubt,When in any doubt, commit first!commit first!
  25. 25. git statusgit status Shows changes in yourShows changes in your working directory andworking directory and staging areastaging area
  26. 26. git loggit log Shows you the history ofShows you the history of where you arewhere you are Usually, this is the place toUsually, this is the place to use a GUI instead, thoughuse a GUI instead, though How did I get here?How did I get here?
  27. 27. git checkoutgit checkout Put the files in yourPut the files in your working directory in aworking directory in a known stateknown state Almost alwaysAlmost always corresponding to acorresponding to a particular local branchparticular local branch You can use the ‘-b’ flag toYou can use the ‘-b’ flag to checkout to make a newcheckout to make a new local branch and switchlocal branch and switch your working directory toyour working directory to it at the same timeit at the same time
  28. 28. git branchgit branch Make a new (local) branchMake a new (local) branch List existing (local orList existing (local or cached) branchescached) branches Delete a (local) branchDelete a (local) branch
  29. 29. git reset /git reset / git cleangit clean git cleangit cleanremove changes youremove changes you (hopefully) don’t want from(hopefully) don’t want from your wayyour way puts the working directoryputs the working directory and/or the staging area back inand/or the staging area back in a known statea known state I use this a lot to sync up to aI use this a lot to sync up to a branch I don’t have changes inbranch I don’t have changes in This can lose data - BEThis can lose data - BE CAREFUL!!CAREFUL!! When in any doubt, commitWhen in any doubt, commit first!first!
  30. 30. git mergegit merge merge 2 or more branchesmerge 2 or more branches uses 3-way mergesuses 3-way merges works REALLY wellworks REALLY well any merge info will get lost ifany merge info will get lost if you push the result to SVNyou push the result to SVN No tool substitutes for aNo tool substitutes for a lack of coordination!lack of coordination! git mergetool helps with thisgit mergetool helps with this
  31. 31. git rebasegit rebase rearrange commitsrearrange commits (illustration coming up(illustration coming up later)later) do NOT do this ifdo NOT do this if someone else is sharingsomeone else is sharing your changesyour changes do NOT share changes ifdo NOT share changes if you’re going to do thisyou’re going to do this
  32. 32. git helpgit help fairly thorough, but oftenfairly thorough, but often dense documentationdense documentation Other resources:Other resources: http://git.or.cz/course/svn.htmlhttp://git.or.cz/course/svn.html http://utsl.gen.nz/talks/gihttp://utsl.gen.nz/talks/gi t-svn/intro.htmlt-svn/intro.html
  33. 33. Some things you probablySome things you probably shouldn’t doshouldn’t do ‘‘git revert’ probablygit 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 yourselfclean up after yourself as soon as you canas soon as you can git push -fgit push -f if you think you need toif you think you need to do this - something baddo this - something bad hopefully happened.hopefully happened. Should not be routine.Should not be routine.
  34. 34. Everyday workflowEveryday workflow
  35. 35. Pro Tips and BestPro 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 push)don’t worry if it’s not good enough (no one can see it until you push) you might want to get back here (the next thing you do might break)you might want to get back here (the next thing you do might 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)
  36. 36. Simplified Dev WorkflowSimplified Dev Workflow (For Larger Teams)(For Larger Teams) (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 beforeMake your changes as before (hopefully committing more often)(hopefully committing more often) Check inYour Changes LocallyCheck inYour 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 CandidatesLater, merge feature branches together to make Release Candidates
  37. 37. Making ReleaseMaking Release Candidate BranchesCandidate Branches
  38. 38. Three Different CombiningThree Different Combining OptionsOptions MergeMerge Keeps HistoryKeeps History Should Use --no-ffShould Use --no-ff Harder to trace later in logsHarder to trace later in logs RebaseRebase Simplifies History in LogsSimplifies History in Logs Better for things withoutBetter for things without common recent parentcommon recent parent Loses some history infoLoses some history info Cherry-PickCherry-Pick Just like rebase but for singleJust like rebase but for single commitscommits
  39. 39. MergingMerging ““git checkout ${destinationgit checkout ${destination branch}”branch}” ““git merge ${feature branch}”git merge ${feature branch}” ““git mergetool” if conflictsgit mergetool” if conflicts ““git checkout --ours” orgit checkout --ours” or “git checkout --theirs” if“git checkout --theirs” if you want to pick oneyou want to pick one ““git merge ${second featuregit merge ${second feature branch}”branch}”
  40. 40. RebaseRebase ““git checkout ${feature branch}”git checkout ${feature branch}” ““git checkout -b ${new name}”git checkout -b ${new name}” to copyto copy ““git rebase ${destinationgit rebase ${destination branch}”branch}” if you want every commitif you want every commit back to common point, orback to common point, or ““git rebase --ontogit 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
  41. 41. Rebasing ExampleRebasing Example initial stateinitial state Figures adapted fromFigures adapted from Pro GitPro Git by Scott Chaconby Scott Chacon C1C1C1C1 mastemaste rr mastemaste rr C2C2C2C2 C5C5C5C5 C6C6C6C6 C3C3C3C3 C4C4C4C4 C7C7C7C7 C8C8C8C8 C9C9C9C9serverserverserverserver clientclientclientclient
  42. 42. Rebasing ExampleRebasing Example git checkout servergit checkout server git rebase mastergit rebase master Figures adapted fromFigures adapted from Pro GitPro Git by Scott Chaconby Scott Chacon C1C1C1C1 mastemaste rr mastemaste rr C2C2C2C2 C5C5C5C5 C6C6C6C6 C3C3C3C3 C4C4C4C4 C7C7C7C7 C8C8C8C8 C9C9C9C9serverserverserverserver clientclientclientclient C3C3C3C3 C4C4C4C4 C7C7C7C7
  43. 43. Rebasing ExampleRebasing Example git checkout servergit checkout server git rebase mastergit rebase master Figures adapted fromFigures adapted from Pro GitPro Git by Scott Chaconby Scott Chacon C1C1C1C1 mastemaste rr mastemaste rr C2C2C2C2 C5C5C5C5 C6C6C6C6 C3C3C3C3 C4C4C4C4 C7C7C7C7 C8C8C8C8 C9C9C9C9 serverserverserverserver clientclientclientclient C3C3C3C3 C4C4C4C4 C7C7C7C7
  44. 44. Rebasing ExampleRebasing Example git checkout servergit checkout server git rebase mastergit rebase master Figures adapted fromFigures adapted from Pro GitPro Git by Scott Chaconby Scott Chacon C1C1C1C1 mastemaste rr mastemaste rr C2C2C2C2 C5C5C5C5 C6C6C6C6 C3C3C3C3 C4C4C4C4 C7C7C7C7 C8C8C8C8 C9C9C9C9 serverserverserverserver clientclientclientclient C3’C3’C3’C3’ C4’C4’C4’C4’ C7’C7’C7’C7’
  45. 45. Rebasing ExampleRebasing Example initial stateinitial state Figures adapted fromFigures adapted from Pro GitPro Git by Scott Chaconby Scott Chacon C1C1C1C1 mastemaste rr mastemaste rr C2C2C2C2 C5C5C5C5 C6C6C6C6 C3C3C3C3 C4C4C4C4 C7C7C7C7 C8C8C8C8 C9C9C9C9serverserverserverserver clientclientclientclient
  46. 46. Rebasing ExampleRebasing Example git rebase --onto master server clientgit rebase --onto master server client Figures adapted fromFigures adapted from Pro GitPro Git by Scott Chaconby Scott Chacon C1C1C1C1 mastemaste rr mastemaste rr C2C2C2C2 C5C5C5C5 C6C6C6C6 C3C3C3C3 C4C4C4C4 C7C7C7C7 C8C8C8C8 C9C9C9C9serverserverserverserver clientclientclientclient
  47. 47. Rebasing ExampleRebasing Example git rebase --onto master server clientgit rebase --onto master server client Figures adapted fromFigures adapted from Pro GitPro Git by Scott Chaconby Scott Chacon C1C1C1C1 mastemaste rr mastemaste rr C2C2C2C2 C5C5C5C5 C6C6C6C6 C3C3C3C3 C4C4C4C4 C7C7C7C7 C8C8C8C8 C9C9C9C9 serverserverserverserver clientclientclientclient
  48. 48. Rebasing ExampleRebasing Example git rebase --onto master server clientgit rebase --onto master server client Figures adapted fromFigures adapted from Pro GitPro Git by Scott Chaconby Scott Chacon C1C1C1C1 mastemaste rr mastemaste rr C2C2C2C2 C5C5C5C5 C6C6C6C6 C3C3C3C3 C4C4C4C4 C7C7C7C7 C8’C8’C8’C8’ C9’C9’C9’C9’ serverserverserverserver clientclientclientclient
  49. 49. Staying current with SVNStaying current with SVN git svn fetchgit svn fetch Doesn’t update workingDoesn’t update working tree, but gets branchestree, but gets branches git svn rebasegit svn rebase puts SVN revision IDs inputs SVN revision IDs in git commentsgit comments have to do this to stay inhave to do this to stay in syncsync Loses git history, so makeLoses git history, so make copied branches in git firstcopied branches in git first
  50. 50. Pushing back to SVNPushing back to SVN git svn dcommitgit svn dcommit simplest and safest on SVN sidesimplest and safest on SVN side requires you rebase in latest SVNrequires you rebase in latest SVN changeschanges Make a copied branch in git to doMake a copied branch in git to do this onthis on May be preceded by “git resetMay be preceded by “git reset --hard $git_branch”--hard $git_branch” git svn set-treegit svn set-tree Overwrites SVN ignorantly (withOverwrites SVN ignorantly (with extreme prejudice)extreme prejudice) nuking the site from Orbitnuking the site from Orbit

×