This document discusses various Git concepts and commands. It begins with an introduction to Git and SCM concepts like branches and merging. It then demonstrates how to initialize a Git repository, add files, and commit changes. Different Git states like staged, unstaged, and clean are explained. Commands shown include git add, commit, status, log, and gui tools like gitk and gitgui. Branching, merging, rebasing, cherry-picking and resolving conflicts are demonstrated. The document concludes by discussing reverting and resetting changes in Git, noting the dangers of losing commits if not used carefully.
1. GitLuca MilanesioAndroid programmingSang ShinThe Productive ProgrammerNeal FordThe Power of RetrospectionLinda RisingIntroduction to ScalaHubert PlociniczakMain sponsor
2. AgendaSCM and Git ConceptsGit quick startBranching and mergingResolving conflictsReverting changesWorking with tagsGit remotesGit on the serverGit peer-to-peerGit democracy
3. Who’s that guy ?Luca MilanesioLMIT Limited – Director / co-founder of GitEnterprise.comJenkins (formerly Hudson) contributor since 2007founder of hudson-mobi.comOver 18 years of experience in Software and Services Development and Application LifecycleWorked for major UK, EU and US CustomersBanking, Retailers, Industry, Finance, Telecoms, Utilities, GovernmentGit enthusiast since 2009 and innovator in Vodafone Group Services Ltd
4. About SCM … remember ?SCM = Source Code ManagementMultiple revisions of files
10. … and much more Picture courtesyofglobalnerdy.com - Allrightskindlyreserved
11. Brief history of OpenSourceSCMsLocal SCMs (versions kept on local filesystem)SCCS (1972) … I was not yet born, don’t remember RCS (1982) the most widely used on UnixServer-based SCMs (central repository server)CVS (1990) first widely used SCM serverSubversion (2000) first widely Internet SCM… and then let’s to go distributed …DCVS (2002) who has ever used it ?Mercurial and Git (2005)
12. Centralised vs. distributed ?How many of you are using central repositories ?CVS-boys … raise your hands !SVN-guys … it’s your turn !How many are for distributed repositories ?Mercurial-scientists ... raise your hands !Git-explorers … brave men !
13. Who’s right ?Central SCMUnique “source of truth”Central back-upSeamless alignment for all developersSecurity and access controlDistributed SCMCommunity code-baseNo single-point-of-failurePeer-to-peer alignmentContinuous branching and merging
14. Why GIT ?It’s all about BitKeeper fault: they broke up with LinusTorvalds … and I’m not kidding Story:Linux Kernel SCM: BitKeeperApr 2005 – Linus writes PERL scripts for Linux Kernel SCMJul 2005 … Git 0.99 is out !Git principles:Allow the BitKeeper SCM workflowUse CVS as the “not-to-do” exampleCheck and prevent corruptionMake it FFF … FAST FAST FAST !
17. Git installation (Ver. >=1.6)Linux (Gitfavourite of course !)Ubuntu: sudo apt-get install git-coreother Linux ? … best from source codehttp://git-scm.com/Mac OSXhttp://code.google.com/p/git-osx-installer/Windowshttp://code.google.com/p/msysgit/(sucks … but it’s your fault not using Unix)CygwinHIGHLY RECOMMENDED (mandatory IMHO)
34. Git stores the whole fileGit is different from SVN: no diffs, just whole filesGit stores changed files between snapshots(BitKeeperdocet)Picture courtesyofProGit.org
35. Git object types: blobsGit stores all files (and their versions) as objects (blobs)Each object has a SHA-1 160-bit identifierSHA-1 provides:Unique Global IDIntegrity checkExample:File content: “You're GIT too”SHA-1: bbecf72783dfba9e0243e13dbb5fb04ed39ed4e4 (Hex)Track content (not files)Automatically detect renames … cool !
36. SHA-1 ? WTF …Why LinusTorvalds has chosen SHA-1 hashing ?Need for track content globallySHA-1 collision probability is 1/251What happens if two files have same SHA-1 ?BOOM !What is the probability of it ?World’s population (7 BN people)sharing files of 10 times Linux Kernel Possible ? More likely to be hit by a 15 KM asteroid NOW !!!
37. Git object types: commits and treesGit tree identifies a snapshot (set of files)Git commit identifiesAuthor / CommitterCommit messageTimestampTreePicture courtesyofProGit.org
38. Git history: graph of commitsEvery commit points to its predecessorSeries of commits make Git repository historyPicture courtesyofProGit.org
39. Where are Git objects ?Git objects are in .git/objectsSHA-1 identify directory / file
40. Curious about Git objects ?Git objects are compressedUse git show to display content
41. Getting lost ?How to remember SHA-1 hashing codes ?How Git stores the “pointers” to Commit graph ?Git references are the solution !Head of the Git historyIntermediate tagsBranch pointsRelative points
42. Git referencesReferences: “labels” for Git SHA-1 commit IDsStored as files under .git/refsReference types:TagsHeads (branches)HEAD is a special ref:always points tohead of currentbranch.
43. How Git commits graph looks like ?ReferenceCommitTreePicture courtesyofProGit.org
44. What is a branch for Git ?Git named branch = reference to a commit ID (head of branch)Git supports “network” of commits, with named and unnamed branches… don’t know why Git reminds me some “underground” branches
47. Wear “life jacket” firstGet Git bash extensions source git/contrib/completion/git-completion.bashRedefine promptexport PS1='$(__git_ps1 " (%s)") ’... and your current branch is visible on your prompt: you will not get lost
52. Git graph after mergeLet’s have a look on the result with gitkMerge-type applied: Fast-forward (move refs in history)Branch has been “flattened”Experimental just another ref to master
53. Git recursive-mergeLet’s create some divergenceChanges on both master and experimentalFast-forward merge = move branch ref to another commit ID
54. Git diverging branchesUse gitk --all to display all branchesNOTE: no args displays just current branchexperimental is really diverging from master
55. Git recursive mergeLet’s merge again with masterThis is a real merge folks !NOTE: Merge is a Git commit: you can associate a comment, or revert it later ! Don’t be scared by Git-managed merge
57. Git rebase in actionLet’s diverge again between master and experimentalMagic ! … rebase flattens the branching history
58. Git graph after rebaseExperimental is no more a diverging branchNOTE: Marconi’s test is on “unnamed branch” and experimental branch history has changed !
75. Git is powerful and dangerousGit has full control on historyAmend existing commitsRemove commitsRevert changesBe careful: you could destroy your history !Be even more careful: history revert is unrecoverableSCARY !!!!!
76. Change existing commitsGit commit support the “amend” option to overwrite committed dataWhat can be amendedFile changes Author / commentDateThe original commit will disappear: amend is NOT revertible (but just amended again)
79. Git reset: back to the pastGit reset allows to:Put committed data back to the working dir soft resetRemove completely committed data hard resetGit reset is NOT revertibleYou want to reset ?Do you really need it ?Do you really want it ?What do you want to achieve ?… and then think again …
80. Soft resetRemove the last commit and put changes back to workdirHint:master~1 = reference to “one commit before master head”Commit is lost, but you still have the changes in workdir
81. Hard resetRemove commit and all the changes associatedCommit is lostFOREVER: there is no way to restore the data
82. Git revertGit revert allows to:Revert the changes and to workdirRevert the changes and create a “reverted commit”Git revert is revertibleRevert = negative commit (eliminates effect of reverted commit)
87. Importance of Git tagsWhy using tags ? … yeah, you know it Why is MORE IMPORTANT in Git than in SVN ?Git commit IDs is SHA-1 hashing (WTF $!#@$!%@^!)Tags = reference to a commit (zero payload)Type of Git tagsLightweight tags (simple Git ref to a commit ID)Annotated tags (author, description, signature)
89. Fully annotate tagsFully annotated tags contains meta-data:TimestampAuthor name and e-mailDescriptionCommit IDGPG Digital SignatureCreate your private GPG Key-pair firstGPG Public Key identify your userGPG Private Key is used to sign content (tags, commits)Exchange GPG Public Key with your peers
93. Git nature: peer-to-peer distributedGit designed to be distributedGlobal unique IDs for files and commits (SHA-1)Completely disconnected operationsRich set of merging capabilitiesCompression and integrity checkNatural way of using it is peer-to-peer… the Linus way, yeah
94. Remote Git repositoriesPoints to other’s people repositoryRemote Git servers / locationRemote Git branchesExample: clone GIT source code repository via “remote”git clone git://git.kernel.org/pub/scm/git/git.git
95. Inspecting remote pointersList of remote Git repositoriesName “origin” refers to remote Git repositoryList of remote Git branches
96. Push changes to remote Git repositoryAdd pointer to remote Git repositoryPush all local branches to remote Git repository
101. Public Git Server: githubMany choices … but github is the best !Create your SSH Key-pairssh-keygen -trsa -b 2048Create your free acount on: https://github.com/signup/freeCreate your repository on:https://github.com/repositories/newAdd your remotegit remote add origin git@github.com:lucamilanesio/33degree.git
102. Private Git Server: GitEnterpriseMany choices … but this is the best for FREE Create your SSH Key-pair (optional: you can use HTTP/basic auth, firewall frendly)ssh-keygen -trsa -b 2048Create your free acount on: https://gitent-scm.com/gitent/users/SignUp.gitCreate your repository on:https://gitent-scm.com/gitent/repository/RepositoryCreation.gitAdd your remotegit remote add origin ssh://lmilanesio@gitent-scm.com/git/gitentdevelopment/33degree
103. 3rd choice: make your own !Installed on your network, running on your hardwareNOTE: make daily backups … GIT is dangerous !Gitosis (http://eagain.net/gitweb/?p=gitosis.git)Users / Groups / KeysRepository managementEverything managed with Git Management Repository Gerrit (http://code.google.com/p/gerrit/)Full Web-based InterfaceUsers / Groups integrated with LDAP, OpenID, …Full repository and security managementFull code-review lifecycle management
108. Developers fetch/pull, dictator pushesgit pushDevelopers pull, Lieutenants integrateDictator get integration branches together: he is the only one that PUSH to GitPicture courtesyofProGit.org
109. GitHub variantgit pushDevelopers pull from “blessed” and have their own public GitIntegration manager is the DictatorRevolution allowed: developer nominates himself “new dictator”Picture courtesyofProGit.org
111. Unique central repositoryEverybody can push / pull from shared Git RepositoryCentral repository dies elections of new repositoryPicture courtesyofProGit.org
112. Back to centralisation ? WTF ?Does it seems like SVN ? … much more guys Git Democracy vs. SVNHorizontal collaboration between developers (P2P)Continuous branching / merging“promotions” of changes through voting (Gerrit model)Control over integration / releaseDoes it seems like Git is mature for the Enterprise ?
114. Working with topic branchesDevelopers work on topicsCode-review / votes promote them to masterTopics branches removed after mergeNOTE: better rebase than mergePicture courtesyofProGit.org
118. Release manager: merge integrationRelease manager decides about releasing integration branchNOTE: No conflicts are generated all merges are from integration branch
122. BOF: Hack your companyJakub NabrdalikBOF: Web framework shootoutBłażej Bucko, Tomasz Dziurko, Wojciech Erbetowski, Łukasz Kuczera, Paweł SzulcBOF: Future of Java EEAlexis Moussine-PouchkineBOF: Those broken, broken class loadersJevgeniKabanovMain sponsor