Transcription

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasGIT for BeginnersAnthony BaireUniversité de Rennes 1 / UMR IRISAJune 2, 2021This tutorial is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 France License1 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasObjectives Understand the basics about version control systems Getting started with GIT working with a local repository synchronising with a remote repository setting up a server2 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSummary1. About Version Control Tools2. Overview of GIT3. Working locally4. Branching & merging5. Interacting with a remote repository6. Administrating a server7. Extras3 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasPart 1.About Version Control Tools Definition Use cases Base concepts History4 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasWhat is a version control system ?From: http://en.wikipedia.org/wiki/Revision controlRevision control [.] is the management of changes to documents,computer programs, large web sites, and other collections of information.Changes are usually identified by a number or letter code, termed the”revision number” [.]. For example, an initial set of files is ”revision 1”.When the first change is made, the resulting set is ”revision 2”, and soon.Each revision is associated with a timestamp and the person making thechange.Revisions can be compared, restored, and with some types of files,merged.5 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasUse case 1: keeping an historyThe life of your software/article is recorded from the beginning at any moment you can revert to a previous revision1 the history is browseable, you can inspect any revision 2when was it done ?who wrote it ?what was changed ?why ?in which context ? all the deleted content remains accessible in the history12let’s say your not happy with your latest changesthis is useful for understanding and fixing bugs6 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasUse case 2: working with othersVC tools help you to: share a collection of files with your team merge changes done by other users ensure that nothing is accidentally overwritten know who you must blame when something is broken7 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasUse case 3: branchingYou may have multiple variants of the same software, materialisedas branches, for example: a main branch a maintainance branch (to provide bugfixes in older releases) a development branch (to make disruptive changes) a release branch (to freeze code before a new release)VC tools will help you to: handle multiple branches concurrently merge changes from a branch into another one8 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasUse case 4: working with external contributorsVC tools help working with third-party contributors: it gives them visibility of what is happening in the project it helps them to submit changes (patches) andit helps you to integrate these patches forking the development of a software and merging it backinto mainline33decentralised tools only9 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasUse case 5: scalingSome metrics4 about the Linux kernel (developed with GIT): about 10000 changesets in each new version(every 2 or 3 months) 1000 unique contributors4source: the Linux Foundation10 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSome illustrations11 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasTaxinomyArchitecture: centralised everyone works on the same unique repository decentralised everyone works on his own repositoryConcurrency model: lock before edit (mutual exclusion) merge after edit (may have conflicts)History layout: tree (merges are not recorded) direct acyclic graphAtomicity scope: file vs whole treeGIT12 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasOther technical aspectsSpace efficiency: storing the whole history of a project requiresstorage space (storing every revision of every file) most VC tools use delta compression to optimise the space(except Git which uses object packing instead)Access method: A repository is identified with a URL. VC toolsoffer multiple ways of interacting with remote repositories. dedicated protocol (svn:// git://) direct access to a local repository (file://path or just path) direct access over SSH (ssh:// git ssh:// svn ssh://) over http (http:// https://)13 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasCreating new revisions14 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasCreating new revisions14 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasCreating new revisions14 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasCreating new revisions14 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasCreating new revisions14 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasCreating new revisions14 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasWhat shall be stored into the repository ?You should store all files that are not generated by a tool: source files (.c .cpp .java .y .l .tex . . . ) build scripts / project files (Makefile configure.inMakefile.am CMakefile.txt wscript .sln) documentation files (.txt README . . . ) resource files (images, audio, . . . )You should not store generated files(or you will experience many unneccessary conflicts) .o .a .so .dll .class .jar .exe .dvi .ps .pdf source files / build scripts when generated by a tool(like autoconf, cmake, lex, yacc)15 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasGuidelines for committing commit often commit independent changes in separate revisions in commit messages, describe the rationale behind of yourchanges (it is often more important than the change itself)16 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasHistory (Centralised Tools) 1st generation (single-file, local-only, lock-before-edit) 1972: SCCS 1982: RCS 1985: PVCS 2nd generation (multiple-files, client-server, merge-before-commit) 1986: CVS 1992: Rational ClearCase 1994: Visual SourceSafe 3rd generation ( repository-level atomicity) 1995: Perforce 2000: Subversion many others17 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasHistory (Decentralised tools)18 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasPart 2.Overview of GIT History Git’s design & features User interfaces19 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasHistory before 2005: Linux sources were managed with Bitkeeper(proprietary DVCS tool) 5 April 2005: revocation of the free-use licence(because of some reverse engineering) No other tools were enough mature to meet Linux’s devconstraints (distributed workflow, integrity, performance). Linus Torvald started developing Git June 2005: first Linux release managed with Git December 2005: Git 1.0 released5now open source! (since 2016)20 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasGit Design objectives distributed workflow (decentralised) easy merging (merge deemed more frequent than commit) integrity (protection against accidental/malicious corruptions) speed & scalability ease of use21 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasGit Design choices Easily hackable simple data structures (blobs, trees, commits, tags) no formal branch history(a branch is just a pointer to the last commit) low-level commands exposed to the user Integrity cryptographic tracking of history (SHA-1 hashes) tag signatures (GPG) Merging pluggable merge strategies staging area (index) Performance no delta encoding22 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasGit Commands23 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasGit GUIs: gitk browsing the history24 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasGit GUIs: git gui preparing commits25 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtras3rd party GUIs Turtoise git (Windows) GitUp, Gitx (MacOS-X) Smartgit (java, multiplatform) Eclipse git plugin26 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasPart 3.Working locally creating a repository adding & committing files the staging area (or index)27 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasCreate a new repositorygit init myrepositoryThis command creates the directory myrepository. the repository is located in myrepository/.git the (initially empty) working copy is located in myrepository/ pwd/tmp git init helloworldInitialized empty Git repository in /tmp/helloworld/.git/ ls -a helloworld/. . .git ls helloworld/.git/branches config description HEAD hooks info objects refs Note: The /.git/ directory contains your whole history,do not delete it66unless your history is merged into another repository28 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasCommit your first filesgit add filegit commit [ -m message ] cd helloworld echo 'Hello World!' hello git add hello git commit -m "added file 'hello'"[master (root-commit) e75df61] added file 'hello'1 files changed, 1 insertions( ), 0 deletions(-)create mode 100644 hello Note: “master” is the name of the default branch created bygit init29 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasThe staging area (aka the “index”)Usual version control systems provide two spaces: the repository(the whole history of your project) the working tree (or local copy)(the files you are editing and that will be in the next commit)Git introduces an intermediate space : the staging area(also called index)The index stores the files scheduled for the next commit: git add files copy files into the index git commit commits the content of the index30 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasThe staging area (aka the “index”)31 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasUpdate a file echo 'blah blah blah' hello git commit# On branch master# Changed but not updated:#(use "git add file ." to update what will be committed)#(use "git checkout -- file ." to discard changes in working directory)##modified:hello#no changes added to commit (use "git add" and/or "git commit -a") Git complains because the index is unchanged (nothing to commit) We need to run git add to copy the file into the index git add hello git commit -m "some changes"[master f37f2cf] some changes1 files changed, 1 insertions( ), 0 deletions(-) 32 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasBypassing the index7Running git add & git commit for every iteration is tedious.GIT provides a way to bypass the index.git commit file1 [ file2 . . . ]This command commits files (or dirs) directly from the workingtreeNote: when bypassing the index, GIT ignores new files: “git commit .” commits only files that were present in the last commit(updated files) “git add . && git commit” commits everything in the working tree(including new files)7also named “partial commit”33 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasBypassing the index34 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasDeleting filesgit rm file remove the file from the index and from the working copygit commit commit the index git rm hellorm 'hello' git commit -m "removed hello"[master 848d8be] removed hello1 files changed, 0 insertions( ), 3 deletions(-)delete mode 100644 hello 35 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasShowing differencesgit diff [ rev a [ rev b ] ][ -- path . . . ] shows the differences between two revisions rev a and rev b(in a format suitable for the patch utility) by default rev a is the index by default rev b is the working copygit diff --staged [ rev a ][ -- path . . . ] shows the differences between rev a and the index by default rev a is HEAD (a symbolic references pointing to thelast commit)36 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasAbout git diff and the index37 / 96

Version ControlGIT Intro Local GITBranchesRemote GITDiff exampleServer BazarExtras echo foo hello git add hello echo bar hello git diff--- a/hello b/[email protected]@ -1,2 1,3 @@Hello World!foo bar git diff --staged--- a/hello b/[email protected]@ -1 1,2 @@Hello World! foo git diff HEAD--- a/hello b/[email protected]@ -1 1,3 @@Hello World! foo bar 38 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasResetting changesgit reset [ --hard ] [ -- path . . . ]git reset cancels the changes in the index (and possibly in theworking copy) git reset drops the changes staged into the index8 , but theworking copy is left intact git reset --hard drops all the changes in the index and inthe working copy8it restores the files as they were in the last commit39 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasResetting changes in the working copygit checkout -- pathThis command restores a file (or directory) as it appears in theindex (thus it drops all unstaged changes) git diff HEAD--- a/hello b/[email protected]@ -1 1,3 @@Hello World! foo bar git checkout -- . git diff HEAD--- a/hello b/[email protected]@ -1 1,2 @@Hello World! foo 40 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasOther local commands git status show the status of the index and workingcopy git show show the details of a commit (metadata diff) git log show the history git mv move/rename a file9 git tag creating/deleting tags (to identify a particularrevision)9note that git mv is strictly equivalent to: “cp src dst && git rm src &&git add dst” (file renaming is not handled formally, but heuristically)41 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasExercises1. create a new repository2. create a new file, add it to the index and commit it3. launch gitk to display it. Keep the window open and hit F5 after eachcommand (to visualise the results of your commands)4. modify the file and make a new commit5. rename the file (either with git mv or git add git rm), do a git statusbefore committing (to ensure the renaming is correctly handled)6. delete the file and commit it7. create two new files and commit them. Then modify their content in theworking copy and display the changes with git diff8. add one file into the index but keep the other one. Display the changesbetween: the index and the working copy the last commit and the index the last commit and the working copy9. run git reset to reset the index10. run git reset --hard to reset the index and the working copy42 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasPart 4.Branching & merging How GIT handles its history Creating new branches Merging & resolving conflicts43 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasHow GIT handles its historyEach commit object has a list of parentcommits: 0 parents initial commit 1 parent ordinary commit 2 parents result of a merge This is a Direct Acyclic Graph44 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasHow GIT handles its history There is no formal “branch history” a branch is just a pointer on the latest commit.(git handles branches and tags in the same way internally) Commits are identified with SHA-1 hash (160 bits)computed from: the commited files the meta data (commit message, author name, . . . ) the hashes of the parent commits A commit id (hash) identifies securely and reliably itscontent and all the previous revisions.45 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasCreating a new branchgit checkout -b new branch [ starting point ] new branch is the name of the new branch starting point is the starting location of the branch (possibly acommit id, a tag, a branch, . . . ). If not present, git will usethe current location. git status# On branch masternothing to commit (working directory clean) git checkout -b developSwitched to a new branch 'develop' git status# On branch developnothing to commit (working directory clean) 46 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasSwitching between branchesgit checkout [-m] branch name git status# On branch developnothing to commit (working directory clean) git checkout masterSwitched to branch 'master' Note: it may fail when the working copy is not clean. Add -m torequest merging your local changes into the destination branch. git checkout mastererror: Your local changes to the following files would be overwritten by checkout: helloPlease, commit your changes or stash them before you can switch branches.Aborting git checkout -m masterMhelloSwitched to branch 'master' 47 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasMerging a branchgit merge other branchThis will merge the changes in other branch into the currentbranch. git status# On branch masternothing to commit (working directory clean) git merge developMerge made by recursive.dev 1 hello 4 2 files changed, 4 insertions( ), 1 deletions(-)create mode 100644 dev 48 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasNotes about merging The result of git merge is immediately committed(unless there is a conflict) The new commit object has two parents. the merge history is recorded git merge applies only the changes since the last commonancestor in the other branch. if the branch was already merged previously, then only thechanges since the last merge will be merged.49 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasBranching example50 / 96

Version ControlGIT IntroLocal GITBranchesRemote GITServer BazarExtrasBranching example50 / 96

Version ControlGIT IntroLocal