Git For Developers

Transcription

Git for DevelopersIntroduction1ENTERPRISECLOUD DEVELOPMENTCopyright 2013 CollabNet, Inc. All Rights Reserved.

Outline2 Version Control Centralized vs. Decentralized Parallel Development Deltified Storage vs. Snapshot Storage GitCopyright 2013 CollabNet, Inc. All Rights Reserved.

Version Control The purpose of version control (VC):– To enable parallel development, which could include:–– History – who / did what / to what / when / whyMultiple independent efforts (e.g. maintenance on prior release while building the next).Branching handles this type of parallel developmentMultiple people on one line of development (i.e., collaborative editing and sharing of data)Implementation flavors: centralized or distributedNOTEVersion control is a part of software configuration management.* There are many different definitions of Software Configuration Management (SCM)* Beyond version control, they typically include: build management, releasemanagement, defect tracking, configuration management and process automation3Copyright 2013 CollabNet, Inc. All Rights Reserved.

Centralized version control Repository 4History in “one“ repositoryClients only get a single revision percheckoutAll commits go into the “one“repositoryCopyright 2013 CollabNet, Inc. All Rights Reserved.

Distributed version control 5Each user has at least one copy(clone) of the repositoryEach 'user' repository holds the fullhistoryThere is a 'Main' repository only byconventionCopyright 2013 CollabNet, Inc. All Rights Reserved.

CentralizedCVS/SVNcheckoutWorking copyHEAD/trunksavesavecommitupdate6Copyright 2013 CollabNet, Inc. All Rights Reserved.

DistributedGitfetchcloneLocal.gitindexworking master stable-1.0savesaveaddmain repocommitpushCopyright 2013 CollabNet, Inc. All Rights Reserved.

Parallel development A fundamental challenge version control systems have to solve is: how to work inparallel while preventing one user from overwriting the work of another.Two solution flavors:––8copy-modify-merge (default)lock-modify-unlock(not supported by distributed versioncontrol systems)Copyright 2013 CollabNet, Inc. All Rights Reserved.

Copy – Modify - MergeTwo users clone a main repository, each work in their local repository.User A commits a new version to their localrepository User A pushes the new version to the mainrepository Repository User B commits a new version to their localrepositoryUser B tries to push the new version to the mainrepository, but it fails indicating he needs to updatethe local version firstUser B fetches the latest version of thethefilefilefromfromthe main repositoryUser B merges changes into his local version of thethefileUser B commits the combined changes into hislocal repositoryUser B pushes the merged version of the file to themain repositoryPushUser A9User BCopyright 2013 CollabNet, Inc. All Rights Reserved.

Deltified storage vs. Snapshot storageTwo ways to store your changes in a repository:–Deltified storage- encoding the representation of a chunk of data as a collection ofdifferences against some other chunk of data–Snapshot storage - stores the complete files changed by a commit along withreferences to files that were not changed by that commit10Copyright 2013 CollabNet, Inc. All Rights Reserved.

Deltified storageDeltas to the previous version are storedCommit 1Commit 2A1AABB1BCC111Commit 3BCCopyright 2013 CollabNet, Inc. All Rights Reserved.

Snapshot storageEach version holds the full source tree12Commit 1Commit 2Commit 3A1AA2AA2BB1B2BB3BCC1C1C2CCopyright 2013 CollabNet, Inc. All Rights Reserved.

GitGit is a distributed version control system:–Inspired by BitKeeper and Monotone–GPLv2–Initiated by Linus Torvalds (father of Linux)–13Strongly influenced by Linux kernel development initially and Android development morerecently–Strong support for non-linear development–Support for ssh, http(s), and git protocols–Simple design–Potentially complex tool to masterCopyright 2013 CollabNet, Inc. All Rights Reserved.

Git HistoryApril 2005First announcement of GitJune 2005First Git driven LinuxreleaseDecember 2005Git 1.0 releasedFebruary 2007Git 1.5 released200520062007January 2006Git 1.1 releasedApril 2006Git 1.3 releasedFebruary 2010Git 1.7 released20082009August 2008Git 1.6 releasedFebruary 2006Git 1.2 releasedJune 2006Git 1.4 released14Copyright 2013 CollabNet, Inc. All Rights Reserved.201020112012October 2012Git 1.8 released

Git GrowthWhat is the primary source code management system you typically use? (Choose one.)Eclipse Open Source Developer Report 2012 – lIBM Rational ClearCaseIBM Rational Team Concert0%10%20%30%40%50%Git increased to 27% from 13% clearly showing momentumSubversion decreased in 2012 but still #1 SCM15Copyright 2013 CollabNet, Inc. All Rights Reserved.60%70%

Popular projects using goproject.comRuby on Railshttp://rubyonrails.orgLinux right 2013 CollabNet, Inc. All Rights Reserved.Androidhttp://android.git.kernel.orgGIT ojects

TerminologyTermDescriptionrepositoryCopy of your project with full historyremoteA repository on another computer that you cloned.gitDirectory where repository metadata (references, object store, etc.) is storedpathLocation of a file/directory in your repository or working treeindexStaging area to assemble the next commitworking treeFiles and directories you are working on (alternatively called working directory)SHA-1Hash algorithm used by Git to identify objects like commitsobjectGeneral term for all object types used by Git, identified by their SHA-1 hash type -ishAn object of the type or which can be peeled to the corresponding type17Copyright 2013 CollabNet, Inc. All Rights Reserved.

Terminology (Cont'd)TermDescriptioncommitObject representing a snapshot of your working tree at a certain point in time(also the operation that creates that snapshot)blobObject representing a file (specifically its contents)treeObject representing a directory (specifically its contents)tagMarks a specific objectlightweight tagLike an immutable branch, just a file (i.e., no associated metadata)annotated tagTag objectbranchA line of developmentmasterBy convention, name of the main branchHEADPointer to the currently checked out branch (or commit)18Copyright 2013 CollabNet, Inc. All Rights Reserved.

Key Features19 Distributed development Speed Commit early – commit often Strong support for non-linear development Easy merging with multiple strategies Simple object model Staging area Cryptographic authentication of history Efficient object storageCopyright 2013 CollabNet, Inc. All Rights Reserved.

Clients & Platforms Graphical Clients: IDE-IntegrationGit Eclipse Clientgitgui (Linux, Mac, Windows)Xcodemsysgit (Windows)gitk (Linux, Mac, Windows) Web Interfaces:TortoiseGit (Windows)cgitGitX (Mac)gitwebSmartGit (Linux, Mac, Windows)Tower (Mac) Web applications:Git Extensions (Windows)Gitbox (Mac)Git-cola (Linux, Mac, Windows)GitHub for WindowsGitHub for Mac20Copyright 2013 CollabNet, Inc. All Rights Reserved.GerritGitLabGitHub

Thank you!21Copyright 2013 CollabNet, Inc. All Rights Reserved.

About CollabNetCollabNet is a leading provider of Enterprise Cloud Development and Agile ALM products and services for softwaredriven organizations. With more than 10,000 global customers, the company provides a suite of platforms andservices to address three major trends disrupting the software industry: Agile, DevOps and hybrid clouddevelopment. Its CloudForge development-Platform-as-a-Service (dPaaS) enables cloud development through aflexible platform that is team friendly, enterprise ready and integrated to support leading third party tools. TheCollabNet TeamForge ALM, ScrumWorks Pro project management and SubversionEdge source code managementplatforms can be deployed separately or together, in the cloud or on-premise. CollabNet complements its technicalofferings with industry leading consulting and training services for Agile and cloud development transformations.Many CollabNet customers improve productivity by as much as 70 percent, while reducing costs by 80 percent.For more information, please visit www.collab.net.22Copyright 2013 CollabNet, Inc. All Rights Reserved.

CollabNet, Inc.8000 Marina Blvd., Suite 600Brisbane, CA 94005www.collab.net 1-650-228-2500 y/collabnet-inc23 2012 CollabNet, Inc., All rights reserved. CollabNet is atrademark or registered trademark of CollabNet Inc., in theUS and other countries. All other trademarks, brandnames, or product names belong to their respectiveholders.Copyright 2013 CollabNet, Inc. All Rights Reserved.

Git for DevelopersEssential Concepts 124ENTERPRISECLOUD DEVELOPMENTCopyright 2013 CollabNet, Inc. All Rights Reserved.

Outline Git Basics Typical Work Cycles– Working with Git Locally– Working with Git Remotely Branching Checkout vs. Reset Examining full history .gitignore Where to get help25Copyright 2013 CollabNet, Inc. All Rights Reserved.

Git Basics26Copyright 2013 CollabNet, Inc. All Rights Reserved.

The Git repository Essential parts of a Git repository– Repository metadata (.git) refs/objects/indexHEAD Working tree27Copyright 2013 CollabNet, Inc. All Rights Reserved.

Git repository structure (.git) ls -altotal w-r--r-drwxr-xr-x inistAdministAdminist.git . HEAD config description hooks index info logs objects packed-refs refsMakefileREADME.txtsrc bjectspacked-refsrefsThis file holds a reference to the branch (or commit) you currently have checked outThis is the main Git configuration file. It keeps specific Git options for your projectIt will show when you have viewed your repository or the list of all versioned repositories.Directory contains shell scripts that are invoked after the corresponding Git commandsThe Git index is used as a staging area between your working directory and your repositoryContains additional information about the repository.Keeps records of changes made to refs.In this directory the data of your Git objects are stored – all the contents of the files you have ever checked inThe file consists of packed heads and tags.This directory normally contains three subfolders – heads, remotes and tagsCopyright 2013 CollabNet, Inc. All Rights Reserved.

Types of repositories Bare, used for hosting,code exchange, etc.29 Non-bare, used bydevelopers and includes:– refs/– refs/– objects/– objects/– HEAD– HEAD– No Index– Index– No working tree– Working treeCopyright 2013 CollabNet, Inc. All Rights Reserved.

The HEAD HEAD is a 'pointer' to the tip of the currently checked outbranch– In a detached HEAD state, HEAD points directly to a commit Only one HEAD per repositoryFeature branchHEADLocalRemoteMaster30Copyright 2013 CollabNet, Inc. All Rights Reserved.

The Index Also called staging area or cache Used to 'compose' the next commit– Powerful and important feature of GitCommitCommitIndexAddWorking tree31Copyright 2013 CollabNet, Inc. All Rights Reserved.

The Index (Cont'd) A repository can hold multiple versions of a file as found in the:– Last commit or earlier in history– Index– Working tree A file is added to the object database when you stage it You can make multiple commits to your local repository beforepushing to a remote repository32Copyright 2013 CollabNet, Inc. All Rights Reserved.

The Index (Cont'd) Think of the index as a virtual working tree, tracking:– Permissions– SHA1 of blob objectefa23a– Current stage (important for merging)– Path (e.g. doc/install.txt)Index8efaf2 It is a one level undoMode bitsObject IDStage numberName ht 2013 CollabNet, Inc. All Rights Reserved.

The Index (Cont'd)git ls-files –-stage shows the currently staged files git ls-files --stage100644 f9264f7fbd31ae7a18b7931ed8946fb0aebb0af3 0100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0NOTEIt shows all tracked files in their current state.34Copyright 2013 CollabNet, Inc. All Rights Reserved.README.txtfoo/bar.txt

The working tree Working tree has all files and folders as found in your HEAD,plus the changes you made since your last commit There is only ONE working tree per repository (and only 1 .gitfolder as well) 35.git . HEAD index objects refsMakefileREADME.txtsrc watchdog.cCopyright 2013 CollabNet, Inc. All Rights Reserved.

RevisioningGit revisions are SHA1 hashes of commits, not revision numbers– A commit includes The hash of the root treeThe hash of the parent commit(s)Commit messageAuthorCommitterDate/time of the commit git log -1commit 5cf2b7013b1504c1a5e09e363e538c7bea82bf06Author: Alice alice@collab.net Date:Thu Dec 6 18:11:19 2012 0100README: fix typo36Copyright 2013 CollabNet, Inc. All Rights Reserved.

Revisioning Example – cat-file command shows the content of anobject: git cat-file -p HEADtree 4ddabe2b65a5f7529e556b36c18db308227e7092parent 494e2cb73ed6424b27f9766bf8a2cb29770a1e7eauthor alice alice@collab.net 1354809881 0100committer alice alice@collab.net 1354809881 0100Added jGit submodule Given a commit hash, we can verify both the full tree and thefull history, since we have– The hash of the tree (which references its subtrees)– The hash of the parent (which references its parents)37Copyright 2013 CollabNet, Inc. All Rights Reserved.

Why two user fields? Why both an author and a committer fields?– Allows developers and maintainers to preserve authorship By default, both author and committer are set to the configureduser name and emailAuthor38CommiterCopyright 2013 CollabNet, Inc. All Rights Reserved.

References Basic concept of references: a pointer to a Git object Usually a file in .git/refs/. points at an object –– Branches– Tags– Notes– .39Copyright 2013 CollabNet, Inc. All Rights Reserved.

Git history Git follows a snapshot model– Each commit is a snapshot of a given state A line of snapshots builds a directed acyclic graph called DAG(remember: each commit refers to its parents)ffb08aFix off-by-onecff346a0bc37Add READMEMerge fix-bugfe54d6Use snprintf40Copyright 2013 CollabNet, Inc. All Rights Reserved.

Git objectsThere are four different object types:– Annotated tag: a specific named pointer to a commit in history– Commit: a snapshot– Tree: representation of a directory (i.e., its content)– Blob: representation of a file (i.e., its content)tag41committreeCopyright 2013 CollabNet, Inc. All Rights Reserved.blob

Git objects (Cont'd) Git tracks content, not files– Two files with the same content result in only one blob in the objectdatabase (ODB)– Two identical subtrees result in only one tree object– As a result there is very little duplication in the ODB You cannot track empty folders with Git42Copyright 2013 CollabNet, Inc. All Rights Reserved.

Git objects (Cont'd)cff346aa1f99Tree cff346Parent 458f31cff3465ba6ff43Tree 3caa77Parent cff3465ba6ff8efaf23caa778efaf2Copyright 2013 CollabNet, Inc. All Rights Reserved.f996c35ba6ff8efaf2f996c3

Common Team Workflows44Copyright 2013 CollabNet, Inc. All Rights Reserved.

Centralized Workflow45Copyright 2013 CollabNet, Inc. All Rights Reserved.

Integrators Workflow46Copyright 2013 CollabNet, Inc. All Rights Reserved.

Dictator / Lieutenants Workflow47Copyright 2013 CollabNet, Inc. All Rights Reserved.

Gerrit Code Review Workflow48Copyright 2013 CollabNet, Inc. All Rights Reserved.

Typical Git Work Cycles49Copyright 2013 CollabNet, Inc. All Rights Reserved.

Overall work cycle50Copyright 2013 CollabNet, Inc. All Rights Reserved.

Git config With git config you can customize how git behaves Information is stored as hierarchical key-value pairs. Git hasthree different configuration files:– System wide /etc/gitconfig– Specific user /.gitconfig– Repository .git/config51Copyright 2013 CollabNet, Inc. All Rights Reserved.

Initial configuration At a minimum, you need to configure your name, email addressand editor By default, Git modifies the repository configuration file To modify the user configuration file, use --global(--system for the system configuration file and --localfor the repository configuration file) git config --global user.name “Alice” git config --global user.email “alice@collab.net” git config --global core.editor gedit ‘--global’ settings can be overwritten by the ‘local repository’settings52Copyright 2013 CollabNet, Inc. All Rights Reserved.

Local Git Work Cycle53Copyright 2013 CollabNet, Inc. All Rights Reserved.

Local work operationsgit init my-projectgit add file1 (git mv file2 file 4git rm file3)git status (git diff)git commit -m“Added file1”CommitLocalRepositoryUser’s MachineInitializeStaging AreaWorkingDirectoryExamine History54AddCopyright 2013 CollabNet, Inc. All Rights Reserved.

Create a local repositorygit init my-project– Creates directory my-project– Initializes repository metadata in my-project/.git indexHEADconfigetc. git init my-projectInitialized empty Git repository in C:/Users/sheta/my-project/.git/55Copyright 2013 CollabNet, Inc. All Rights Reserved.

Typical local work cycle56Copyright 2013 CollabNet, Inc. All Rights Reserved.

Step 1: Make your changes Create paths and modify files normally Stage your modifications57– Create or modifygit add file git add README.txt– Move or renamegit mv file file git mv foo.txt bar.txt– Removegit rm file git rm bar.txtCopyright 2013 CollabNet, Inc. All Rights Reserved.

Step 2: Examine your changesVerify the status of changes with git status:– Gives you detailed information about what is going on– Provides help on how to undo changes or how to continue #########58git statusOn branch masterInitial commitChanges to be committed:(use "git rm --cached file ." to unstage)new file:README.txtCopyright 2013 CollabNet, Inc. All Rights Reserved.

Git diff Examine your changes using git diff for unstaged changes git diffdiff --git a/README.txt b/README.txtindex e84566.1b7c705 100644--- a/README.txt b/README.txt@@ -1 1,2 @@README adding third line And git diff --cached to examine your staged changes git diff --cacheddiff --git a/README.txt b/README.txtnew file mode 100644index 0000000.e845566--- /dev/null b/README.txt@@ -0,0 1 @@ README You can also compare the HEAD of your local branch to the HEADof the remote branch git diff master origin/masterdiff --git a/README.txt b/README.txtindex bd5f0ef.e845566 100644--- a/README.txt b/README.txt@@ -1,2 1 @@README-Added something in README locally59Copyright 2013 CollabNet, Inc. All Rights Reserved.

Step 3: Commit your changesgit commit -m 'Add README file'‚ git commit -m 'Added README file'[master (root-commit) 9fbed8a] Added README file1 files changed, 1 insertions( ), 0 deletions (-)create mode 100644 README.txt Creates a new commit object based on the index Moves current branch to the new commit New tree and blob objects are created git cat-file -p 9fbed8afftree f79cbae241a835d49f493b38f564424058768013parent 494e2cb73ed6424b27f9766bf8s2cb29770a1e7eaut

Hash algorithm used by Git to identify objects like commits : object . General term for all object types used by Git, identified by their SHA -1 hash type -ish . An object of