Top Banner
Source Code Management wih git Matthieu Herrb December 2019 https://homepages.laas.fr/matthieu/cours/pi2-git.pdf
104

Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Aug 18, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Source Code Management wih git

Matthieu Herrb

December 2019

httpshomepageslaasfrmatthieucourspi2-gitpdf

License

This work is licensed under a Creative Commons Attribution-ShareAlike 30 UnportedLicenseTo get a copy of the license use the following addresshttpcreativecommonsorglicensesby-sa30

December 2019 284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 484

WTF is a version control system

Software that manages history of changes in a set of documents

Typically source code

But alsodocumentationweb sitesconfiguration filesetc

December 2019 584

Basic functionalities

keep an history of changesmake it possible to work in teamsallow parallel workprovide security (integrity availability confidentiality)

December 2019 684

Diff amp patch

patch

patch

V2 V1 V1

V2

-+

December 2019 784

Merge

Applying a patch to a slightly different version

patch

patch

V2 V1 Vn

Vn+1

- +

May fail rarr conflictDecember 2019 884

Text diff

A patch represents the changes between 2 versions of a text file

diff -u produces a patch--- asrcserverc+++ bsrcserverc -2227 +2229 reset_log(void)ifdef HAVE_SS_LENdefine sockaddr_len(s) sss_lenelse-define sockaddr_len(s) sizeof(s)+define sockaddr_len(s) (sss_family == AF_INET6 + sizeof(struct sockaddr_in6) + sizeof(struct sockaddr_in))endif

void

December 2019 984

The patch command

patch applies a patch to a file to produce the new version

Example patch -p1 -E lt diff

patch can handle small inconsistencies thanks to the context

December 2019 1084

Version Control concepts (1)

RepositoryStorage area that keeps the history of modificationsRevisionUnique identifier of Each state of the source filesAlso called commit as language shortcut

A B C

Ordered sequence arrows point to the ancestor

rarr Marketing project version = VCS revision

December 2019 1184

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 2: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

License

This work is licensed under a Creative Commons Attribution-ShareAlike 30 UnportedLicenseTo get a copy of the license use the following addresshttpcreativecommonsorglicensesby-sa30

December 2019 284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 484

WTF is a version control system

Software that manages history of changes in a set of documents

Typically source code

But alsodocumentationweb sitesconfiguration filesetc

December 2019 584

Basic functionalities

keep an history of changesmake it possible to work in teamsallow parallel workprovide security (integrity availability confidentiality)

December 2019 684

Diff amp patch

patch

patch

V2 V1 V1

V2

-+

December 2019 784

Merge

Applying a patch to a slightly different version

patch

patch

V2 V1 Vn

Vn+1

- +

May fail rarr conflictDecember 2019 884

Text diff

A patch represents the changes between 2 versions of a text file

diff -u produces a patch--- asrcserverc+++ bsrcserverc -2227 +2229 reset_log(void)ifdef HAVE_SS_LENdefine sockaddr_len(s) sss_lenelse-define sockaddr_len(s) sizeof(s)+define sockaddr_len(s) (sss_family == AF_INET6 + sizeof(struct sockaddr_in6) + sizeof(struct sockaddr_in))endif

void

December 2019 984

The patch command

patch applies a patch to a file to produce the new version

Example patch -p1 -E lt diff

patch can handle small inconsistencies thanks to the context

December 2019 1084

Version Control concepts (1)

RepositoryStorage area that keeps the history of modificationsRevisionUnique identifier of Each state of the source filesAlso called commit as language shortcut

A B C

Ordered sequence arrows point to the ancestor

rarr Marketing project version = VCS revision

December 2019 1184

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 3: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 484

WTF is a version control system

Software that manages history of changes in a set of documents

Typically source code

But alsodocumentationweb sitesconfiguration filesetc

December 2019 584

Basic functionalities

keep an history of changesmake it possible to work in teamsallow parallel workprovide security (integrity availability confidentiality)

December 2019 684

Diff amp patch

patch

patch

V2 V1 V1

V2

-+

December 2019 784

Merge

Applying a patch to a slightly different version

patch

patch

V2 V1 Vn

Vn+1

- +

May fail rarr conflictDecember 2019 884

Text diff

A patch represents the changes between 2 versions of a text file

diff -u produces a patch--- asrcserverc+++ bsrcserverc -2227 +2229 reset_log(void)ifdef HAVE_SS_LENdefine sockaddr_len(s) sss_lenelse-define sockaddr_len(s) sizeof(s)+define sockaddr_len(s) (sss_family == AF_INET6 + sizeof(struct sockaddr_in6) + sizeof(struct sockaddr_in))endif

void

December 2019 984

The patch command

patch applies a patch to a file to produce the new version

Example patch -p1 -E lt diff

patch can handle small inconsistencies thanks to the context

December 2019 1084

Version Control concepts (1)

RepositoryStorage area that keeps the history of modificationsRevisionUnique identifier of Each state of the source filesAlso called commit as language shortcut

A B C

Ordered sequence arrows point to the ancestor

rarr Marketing project version = VCS revision

December 2019 1184

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 4: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 484

WTF is a version control system

Software that manages history of changes in a set of documents

Typically source code

But alsodocumentationweb sitesconfiguration filesetc

December 2019 584

Basic functionalities

keep an history of changesmake it possible to work in teamsallow parallel workprovide security (integrity availability confidentiality)

December 2019 684

Diff amp patch

patch

patch

V2 V1 V1

V2

-+

December 2019 784

Merge

Applying a patch to a slightly different version

patch

patch

V2 V1 Vn

Vn+1

- +

May fail rarr conflictDecember 2019 884

Text diff

A patch represents the changes between 2 versions of a text file

diff -u produces a patch--- asrcserverc+++ bsrcserverc -2227 +2229 reset_log(void)ifdef HAVE_SS_LENdefine sockaddr_len(s) sss_lenelse-define sockaddr_len(s) sizeof(s)+define sockaddr_len(s) (sss_family == AF_INET6 + sizeof(struct sockaddr_in6) + sizeof(struct sockaddr_in))endif

void

December 2019 984

The patch command

patch applies a patch to a file to produce the new version

Example patch -p1 -E lt diff

patch can handle small inconsistencies thanks to the context

December 2019 1084

Version Control concepts (1)

RepositoryStorage area that keeps the history of modificationsRevisionUnique identifier of Each state of the source filesAlso called commit as language shortcut

A B C

Ordered sequence arrows point to the ancestor

rarr Marketing project version = VCS revision

December 2019 1184

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 5: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

WTF is a version control system

Software that manages history of changes in a set of documents

Typically source code

But alsodocumentationweb sitesconfiguration filesetc

December 2019 584

Basic functionalities

keep an history of changesmake it possible to work in teamsallow parallel workprovide security (integrity availability confidentiality)

December 2019 684

Diff amp patch

patch

patch

V2 V1 V1

V2

-+

December 2019 784

Merge

Applying a patch to a slightly different version

patch

patch

V2 V1 Vn

Vn+1

- +

May fail rarr conflictDecember 2019 884

Text diff

A patch represents the changes between 2 versions of a text file

diff -u produces a patch--- asrcserverc+++ bsrcserverc -2227 +2229 reset_log(void)ifdef HAVE_SS_LENdefine sockaddr_len(s) sss_lenelse-define sockaddr_len(s) sizeof(s)+define sockaddr_len(s) (sss_family == AF_INET6 + sizeof(struct sockaddr_in6) + sizeof(struct sockaddr_in))endif

void

December 2019 984

The patch command

patch applies a patch to a file to produce the new version

Example patch -p1 -E lt diff

patch can handle small inconsistencies thanks to the context

December 2019 1084

Version Control concepts (1)

RepositoryStorage area that keeps the history of modificationsRevisionUnique identifier of Each state of the source filesAlso called commit as language shortcut

A B C

Ordered sequence arrows point to the ancestor

rarr Marketing project version = VCS revision

December 2019 1184

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 6: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Basic functionalities

keep an history of changesmake it possible to work in teamsallow parallel workprovide security (integrity availability confidentiality)

December 2019 684

Diff amp patch

patch

patch

V2 V1 V1

V2

-+

December 2019 784

Merge

Applying a patch to a slightly different version

patch

patch

V2 V1 Vn

Vn+1

- +

May fail rarr conflictDecember 2019 884

Text diff

A patch represents the changes between 2 versions of a text file

diff -u produces a patch--- asrcserverc+++ bsrcserverc -2227 +2229 reset_log(void)ifdef HAVE_SS_LENdefine sockaddr_len(s) sss_lenelse-define sockaddr_len(s) sizeof(s)+define sockaddr_len(s) (sss_family == AF_INET6 + sizeof(struct sockaddr_in6) + sizeof(struct sockaddr_in))endif

void

December 2019 984

The patch command

patch applies a patch to a file to produce the new version

Example patch -p1 -E lt diff

patch can handle small inconsistencies thanks to the context

December 2019 1084

Version Control concepts (1)

RepositoryStorage area that keeps the history of modificationsRevisionUnique identifier of Each state of the source filesAlso called commit as language shortcut

A B C

Ordered sequence arrows point to the ancestor

rarr Marketing project version = VCS revision

December 2019 1184

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 7: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Diff amp patch

patch

patch

V2 V1 V1

V2

-+

December 2019 784

Merge

Applying a patch to a slightly different version

patch

patch

V2 V1 Vn

Vn+1

- +

May fail rarr conflictDecember 2019 884

Text diff

A patch represents the changes between 2 versions of a text file

diff -u produces a patch--- asrcserverc+++ bsrcserverc -2227 +2229 reset_log(void)ifdef HAVE_SS_LENdefine sockaddr_len(s) sss_lenelse-define sockaddr_len(s) sizeof(s)+define sockaddr_len(s) (sss_family == AF_INET6 + sizeof(struct sockaddr_in6) + sizeof(struct sockaddr_in))endif

void

December 2019 984

The patch command

patch applies a patch to a file to produce the new version

Example patch -p1 -E lt diff

patch can handle small inconsistencies thanks to the context

December 2019 1084

Version Control concepts (1)

RepositoryStorage area that keeps the history of modificationsRevisionUnique identifier of Each state of the source filesAlso called commit as language shortcut

A B C

Ordered sequence arrows point to the ancestor

rarr Marketing project version = VCS revision

December 2019 1184

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 8: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Merge

Applying a patch to a slightly different version

patch

patch

V2 V1 Vn

Vn+1

- +

May fail rarr conflictDecember 2019 884

Text diff

A patch represents the changes between 2 versions of a text file

diff -u produces a patch--- asrcserverc+++ bsrcserverc -2227 +2229 reset_log(void)ifdef HAVE_SS_LENdefine sockaddr_len(s) sss_lenelse-define sockaddr_len(s) sizeof(s)+define sockaddr_len(s) (sss_family == AF_INET6 + sizeof(struct sockaddr_in6) + sizeof(struct sockaddr_in))endif

void

December 2019 984

The patch command

patch applies a patch to a file to produce the new version

Example patch -p1 -E lt diff

patch can handle small inconsistencies thanks to the context

December 2019 1084

Version Control concepts (1)

RepositoryStorage area that keeps the history of modificationsRevisionUnique identifier of Each state of the source filesAlso called commit as language shortcut

A B C

Ordered sequence arrows point to the ancestor

rarr Marketing project version = VCS revision

December 2019 1184

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 9: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Text diff

A patch represents the changes between 2 versions of a text file

diff -u produces a patch--- asrcserverc+++ bsrcserverc -2227 +2229 reset_log(void)ifdef HAVE_SS_LENdefine sockaddr_len(s) sss_lenelse-define sockaddr_len(s) sizeof(s)+define sockaddr_len(s) (sss_family == AF_INET6 + sizeof(struct sockaddr_in6) + sizeof(struct sockaddr_in))endif

void

December 2019 984

The patch command

patch applies a patch to a file to produce the new version

Example patch -p1 -E lt diff

patch can handle small inconsistencies thanks to the context

December 2019 1084

Version Control concepts (1)

RepositoryStorage area that keeps the history of modificationsRevisionUnique identifier of Each state of the source filesAlso called commit as language shortcut

A B C

Ordered sequence arrows point to the ancestor

rarr Marketing project version = VCS revision

December 2019 1184

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 10: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

The patch command

patch applies a patch to a file to produce the new version

Example patch -p1 -E lt diff

patch can handle small inconsistencies thanks to the context

December 2019 1084

Version Control concepts (1)

RepositoryStorage area that keeps the history of modificationsRevisionUnique identifier of Each state of the source filesAlso called commit as language shortcut

A B C

Ordered sequence arrows point to the ancestor

rarr Marketing project version = VCS revision

December 2019 1184

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 11: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Version Control concepts (1)

RepositoryStorage area that keeps the history of modificationsRevisionUnique identifier of Each state of the source filesAlso called commit as language shortcut

A B C

Ordered sequence arrows point to the ancestor

rarr Marketing project version = VCS revision

December 2019 1184

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 12: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Version Control concepts (2)

Branches

A B C D I J

E F

G

H K

December 2019 1284

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 13: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches usage

Branches can be used to fix a bug in an released versiondevelop new ideas in parallelmanage a customized version of the softwaremerge back a version that diverged for some reasontrack local modifications to externally maintained sources

December 2019 1384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 14: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 1484

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 15: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

GIT

Distributed version control systemby opposition to CVS or SVN which are CentralizedDeveloped by Linus Torvalds for the Linux kernelSimilar to Monotone Darcs Mercurial Bazaar etc

December 2019 1584

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 16: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Git concepts (1)

repository all the history of the projectstored in the git directory

diff or patch differences between 2 versions of a filecommit (verb) action to register a version of a set of files

to the repositorycommit (noun) the result of a commit action represented by

a 128 hexadecimal SHA-1 hashbranch one line of development

by default all development is done in mastertag a symbolic identifier for a commit or a branch

December 2019 1684

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 17: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Git concepts (2)

Working tree the set of files being worked on currentlyIndex an object tracking modified added removed filesBlob binary data used to store files objects and other data

December 2019 1784

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 18: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Git Interfaces

Command lineGit GUIs

gitk (part of git distribution)gitg (Gnome project)git-cola httpgit-colagithubcomTortoiseGit (Windows)httpcodegooglecomptortoisegit

Editor pluginsAtom (built-in)Visual Studio Code(built-in)Eclipse (httpeclipseorgegit)Emacs (VC magit)

Web browsers cgit gitweb

December 2019 1884

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 19: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Git forges

Web sites dedicated to git projects hostinggithub httpsgithubcomgitlab httpsgitlabcomgogs httpsgogsioredmine with the git plugin

Include interesting features for collaboration

Better suited for distributed development than traditionalcentralized forges

December 2019 1984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 20: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 2084

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 21: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Initial setup

Sets defaults for commit messagesuser name amp emailpreferred text editor

$ git config --global --add username Matthieu Herrb$ git config --global --add useremail ltmatthieuherrblaasfrgt$ git config --global --add coreeditor emacs -nw

$ cat ~gitconfig[user]

name = Matthieu Herrbemail = ltmatthieuherrblaasfrgt

[core]editor = emacs -nw

December 2019 2184

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 22: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Creating a repository

git init creates an empty repository in the current directory$ mkdir git-tutorial$ cd git-tutorial$ git initInitialized empty Git repository in homemhgit-tuturialgit$ ls -l gittotal 24-rw-r--r-- 1 mh mh 23 Oct 26 0914 HEAD-rw-r--r-- 1 mh mh 111 Oct 26 0914 config-rw-r--r-- 1 mh mh 58 Oct 26 0914 descriptiondrwxr-xr-x 12 mh mh 408 Oct 26 0914 hooksdrwxr-xr-x 3 mh mh 102 Oct 26 0914 infodrwxr-xr-x 4 mh mh 136 Oct 26 0914 objectsdrwxr-xr-x 4 mh mh 136 Oct 26 0914 refs

December 2019 2284

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 23: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Adding files

git add adds new or modified files to the index$ echo Hello World gt filetxt$ git add

December 2019 2384

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 24: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Querying status

Shows the status of the repository and the index$ git status On branch master Initial commit Changes to be committed (use git rm --cached ltfilegt to unstage) new file filetxt

December 2019 2484

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 25: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Committing changes

$ git commitCreated initial commit 0ba7bd8 Initial version1 files changed 1 insertions(+) 0 deletions(-)create mode 100644 filetxt$ echo Hello Matthieu gt filetxt$ git commit -aCreated commit 7fbf4cb Modif1 files changed 1 insertions(+) 1 deletions(-)

Opens a text editor to enter a commit messageand commits the change to the repository

December 2019 2584

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 26: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

The git index

Represents modifications pending commit2 stages

1 add modified files to the index (addrm)2 ldquoflushrdquo the index to the repository (commit)

Short-cuts chaining both operationsgit commit filegit commit dir (or git commit )git commit -a

December 2019 2684

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 27: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Commits

Adds a node at the end of the current branch

A B C

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 28: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Commits

Adds a node at the end of the current branch

A B C D

Includesthe patch from old to new revision for text filesthe full new revision for binary filesattributes of the committed file (access modes)name and e-mail address of the committera log messageoptionally a digital signature

December 2019 2784

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 29: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Interactive add

$ git add -p [files]

Enters an interactive session to pick up changes to be added tonext commit

Allows to have several unrelated un-committed modifications andstill do clean separate commits

December 2019 2884

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 30: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Looking back git log

Various ways to display the history of modifications$ git logcommit 7fbf4cb7c8977061fbfb609016f5414e833a3a1cAuthor Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122933 2014 +0100

Modif

commit 0ba7bd8b93ef9ddd8917814bde8cbdaaf9732559Author Matthieu Herrb ltmatthieuherrblaasfrgtDate Tue Oct 28 122838 2014 +0100

Initial version$ git log --stat$ git log -p

December 2019 2984

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 31: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Examining changes git diff

Display the changes between the working files and the indexor between the index and the repositoryecho Good bye gt filetxt$ git diffdiff --git afiletxt bfiletxtindex 6bd8f3cc0ee9ab 100644--- afiletxt+++ bfiletxt -1 +1 -Hello Matthieu+Good bye$ git add filetxt$ git diff --cached

December 2019 3084

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 32: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Marking a version git tag

Create a tag object containing a name and a commentOpens the text editor to enter the comment$ git tag -a git-tuto-10$ git tag -lgit-tuto-10

December 2019 3184

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 33: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Fixing mistakes reverting to a good version

Restore the working dir to a given committed versionloosing all local changes$ git reset --hard [commit-id]

If commit-id is missing defaults to HEAD

Revert a given commit$ git revert 03baceFinished one revertCreated commit c333ab5 Revert 3rd version1 files changed 1 insertions(+) 1 deletions(-)

December 2019 3284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 34: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 3384

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 35: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches

A B C

masterExisting history

Branch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 36: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches

A B C

master

mybranch

Existing historyBranch creation

commits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 37: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches

A B C

master

D

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 38: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches

A B C

master

D E

mybranch

Existing historyBranch creationcommits in the new branch

commits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 39: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches

A B C

D E

mybranch

F

masterExisting historyBranch creationcommits in the new branchcommits in master

merge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 40: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches

A B C

D E

mybranch

F G

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into master

further commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 41: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches

A B C

D E

F G

master

H

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branch

etc

December 2019 3484

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 42: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches

A B C

D E

F G

master

H I

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 43: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches

A B C

D E

F G

H I

mybranch

J

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 44: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches

A B C

D E

F G

H I

mybranch

J K

masterExisting historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 45: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Branches

A B C

D E

F G

H I

J K

master

L

mybranch

Existing historyBranch creationcommits in the new branchcommits in mastermerge the branch into masterfurther commits in the branchetc

December 2019 3484

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 46: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Switching branches

Create a new branch$ git checkout -b newbranch

Switch back to master$ git checkout master

December 2019 3584

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 47: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Listing available branches

$ git branch masternewbranch

December 2019 3684

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 48: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Merging changes from another branch

$ git merge branch

Merge commits from ldquobranchrdquo and commits the result

2 kinds of mergesfast forward no conflicts only new commits to add to yourversionnormal merge there are local changes - use a 3 way mergealgorithm

December 2019 3784

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 49: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Handling conflicts

Conflicts happen when changes in a merged branch areincompatible with changes in the target branch

Files with conflicts contain conflict markersThey are not automatically added to the index

To proceed Resolve the conflict (ie choose the correct version)Add the manually merged files to the indexCommit the result

December 2019 3884

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 50: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Tools to help with merge

To solve conflicts git can use existing tools to help mergingmeld xxdiff opendiff DiffMerge

$ git config --global mergetool meld

$ git mergetool

December 2019 3984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 51: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 4084

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 52: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Working in teams

No locks on source codeEach developer has its own copy of the source and repositoryConflicts handling

First merge other peoplersquos contributionAutomated merges as much as possibleConflict detection rarr manual resolutionNo new commit before solving the conflict

December 2019 4184

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 53: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Centralized model

Pull

Pull

Push

Anne

Bernard Carole

Denis

Central repository

PullPull

Push

December 2019 4284

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 54: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Semi-Distributed model

Commit

Commitpull

pushpull

Geacuterard

pull

push

pull

Fabienne

Commit

pullHeacutelegravene

EricDecember 2019 4384

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 55: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Copying a repository

git clone repo

repo an url to the remote repository Can bea pathname to a local repository on the same file-systemssh[user]hostpath - use SSH with given usersshgithostpath - SSH with pubkey authentification(github redmine)githostpath - anonymous access with the GIT protocolhttpshostpath - anonymous access with HTTPSprotocol

December 2019 4484

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 56: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Remote repository

Remote repo A B C D master

December 2019 4584

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 57: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Remote repository

Remote repo A B C D master

Local repo A B C D originmaster

master

clone

December 2019 4584

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 58: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Updating from a remote repository

$ git pull

Pulls the remote branches to the local repositorymerges the default remote branch into the current one(Including commit)Can produce a conflict

Solve the conflictCommit the result

December 2019 4684

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 59: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Using rebase with remote repositories

git fetch fetches remote commits without merging them

Fetch and rebase at once$ git pull --rebase

equivalent to$ git fetch$ git rebase originmaster

December 2019 4784

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 60: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Remote branches

$ git branch -r

lists remote branches (originbranch)

Remote branches can be tracked (automatically mergedpushed)using$ git checkout -t -b newbranch originnewbranch

December 2019 4884

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 61: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Sending changes to a repository

$ git push

Sends local commits to remote tracked branchesProduces an error if not up-to-date (need to pull or rebase first)

Tags need to be pushed separately$ git push --tags

December 2019 4984

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 62: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 5084

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 63: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Git work flows

Work flows that help maintaining a consistent central masterbranchDevelopers use private repositoriesSeveral models of workflow exist using several tools

e-mail git send-email git amForge-provided tools (example Github pull request)Direct access to the private repositories

In all cases branches in the private repository allow to work onseveral changes until they are accepted

December 2019 5184

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 64: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Maintainer pull request work flow

Only the maintainer can push to masterDevelopers submit pull requests to the maintainerThe maintainer reviews merges and then pushes the result

December 2019 5284

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 65: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Pull requests

MarieEtienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

pu

ll requ

est

commit

clone

pull

merge

❶❷

❹ ❺

December 2019 5384

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 66: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Pull requests

Marie is the maintainer Dominique a developer and Etienne anend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Dominique sends a pull request to Marie5 Marie merges the pull request in her local repository6 Marie checks Dominiquersquos work7 If Marie is happy with the result pushes it to the main

repository8 Etienne can grab the result

December 2019 5484

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 67: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Reviews work flow

Push to master is openbut no one can push to master without a reviewDevelopers ask others for reviewsReviewers reply with an rsquoOK rsquoDeveloper amends the commit message to add rsquoReviewed-byrsquoheaders and pushes the result

December 2019 5584

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 68: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Reviews

Reacutegine Etienne

Dominique

gitopenrobotsorg

redminelaasfr

clone

pushpu

ll

push

clon

epu

ll

review req

uest

commit

reviewed

-by

clonepull

❶❷

❹ ❺❻

December 2019 5684

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 69: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Reviews

Regine is a reviewer Dominique a developer and Etienne theend-user

1 Dominique clones the repository2 Dominique works on the code does some commit(s)3 Dominique pushes his commits to his private repository4 Doninique sends a review request to the community5 Regine picks up the request6 Regine accepts the change and sends a reviewed-by message

to Dominique7 Dominque amends his commit and pushes it to the main

repository8 Etienne can grab the result

December 2019 5784

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 70: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Using email

For small changes (patches) using email to interact withreviewersmaintainers is easierfaster

configure sendemailsmtpserver andsendemailsmtpuseruse git format-patch to generate the patches for thecommits to submit for review pulluse git send-email to send an email containing the patchesgenerated abovethe maintainer or reviewer can use git am to apply patchesfrom his mail client

See The advantages of an email-driven git workflow for moreinformation

December 2019 5884

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 71: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Managing remote repositories

git remote command

add name url add a remoteset-url name url changes the urlrename old new renamesrm name removes a remote

December 2019 5984

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 72: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Pushing to multiple remote repositories

git push remote branch push a given branch to a given remote

Example$ git push origin master same as git push$ git push github mybranch push branch to github

December 2019 6084

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 73: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6184

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 74: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Stashing local changes

Git refuses to merge a branch if there are un-commited changes

Solutionscommit local changes before mergingor stash local changes before merging

$ git stash$ git merge mybranch or git pull --rebase$ git stash pop

Itrsquos also possible to create a new branch with the stashed changes$ git stash branch newbranch

December 2019 6284

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 75: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Picking individual changes

Take one commit from another branch (bug fix)and apply it to the working branch

$ git cherry-pick SHA1_HASH

December 2019 6384

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 76: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Replaying changes from a branch

Merges create lots of unwanted links in the git data graphWhen a branch has only few local commits rebase is moreefficient

$ git rebase master

Only use rebase before pushing to a remote repository

December 2019 6484

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 77: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Rebase

A B

Existing commits

Development branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 78: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Rebase

A B

C D E

Existing commitsDevelopment branch

Commits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 79: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Rebase

A B

C D E

F G

Existing commitsDevelopment branchCommits in master

Start of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 80: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Rebase

A B F G

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branch

End of rebase recreate commits starting from HEAD demaster

December 2019 6584

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 81: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Rebase

A B F G

Crsquo Drsquo Ersquo

Existing commitsDevelopment branchCommits in masterStart of rebase remove commits from the branchEnd of rebase recreate commits starting from HEAD demaster

December 2019 6584

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 82: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Interactive rebase

Useful to re-arrange commits locally in order to clean up thehistory$ git rebase -i COMMITS

rarr opens a text editor with the list of commits specifiedRearrange the list according to instructions and save itrarr history will be re-written following the new list

Only use rebase before pushing to a remote repository

December 2019 6684

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 83: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

ldquoTreeishrdquo

Alternative ways to refer to objects or ranges of objectsfull sha-1 8f8aca4bd6c29048636966247aa582718559d871partial sha-1 8f8aca4b 8f8aca 8f8acbranch or tag name v10 master origintestingdate spec masteryesterday master1 month agoordinal spec master5carrot parent master^2tilde spec master~2tree pointer master^treeblob spec masterpathtofileranges 4c032a8faca4

See gitrevisions(1)December 2019 6784

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 84: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Examples

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 85: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Examples

master^

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 86: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Examples

master^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 87: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Examples

master~2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 88: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Examples

master~2^2 or master^^^2

18cae 2fbb3 4eadf 2f45e ce0e4 5ec47

c36ae df2fa a09c6 b3be1

master

December 2019 6884

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 89: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 6984

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 90: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Good practices - repositories

Do not abuse git as a generic cloud storageGit is good with text format files (source codeMarkdownLaTeX doccuments )Git is not useful on images PDF or Office documentsLarge binary files are wasting resources in a git repository

Keep repositories on topicOne repository per projectDo not store un-related files

Keep repository clean and minimaldo not add editor backup filesdo not add files that can be generated automaticallydo not add alternative versions as separate files Use branches

December 2019 7084

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 91: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Good practices - commits

Good commitss are hard but add a lot of value to the projectAlways provide informative commit messagesFollow the git commit convention

one line summaryone empty linedetailled information below (multiple lines)

Keep commits small and focusedDonrsquot blindly add changes Use git status and git diff toreview themUse git add -p to break-up large uncommited changes

Commit early and commit oftenUse amend or rebase to fix broken commits before pushing

December 2019 7184

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 92: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Good practices - branches

Keep the number of active branches smallUse self-documenting branches namesPrefer rebase to merge whenever possibleTry to keep a linear history on the main branch

December 2019 7284

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 93: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Good practices - distributed development

Ask for reviewsReview the patches that are sent to youAlways use pull --rebase when possible before pushingDo not push experimentaltest branches if not neededBe careful to push to the correct branchIn case of a mistake communicate with other developers

December 2019 7384

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 94: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 7484

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 95: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Identifying authors

$ git blame -- filetxt

for each line of the file shows the id and author of the lastmodification

December 2019 7584

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 96: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Making a release with git

(Alternative to automakersquos make dist)Commit all changes including the new macro revision numberTag the resultUse archive to produce a release

$ git tag -a foo-13$ git archive --prefix=foo-13 foo-13 | gzip -c - gt foo-13targz

December 2019 7684

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 97: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Binary search of bugs

$ git bisect start$ git bisect bad Current version is bad$ git bisect good v26-rc2 v26-rc2 was the last version

tested that was good$ git bisect reset back to initial state

With a script that can tell if the current code is good or bad$ git bisect run my_script arguments

my_script returns 0 rarr goodmy_script returns 1124 rarr bad

December 2019 7784

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 98: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Reflog

reflog is the safety net of gitrecords all changes done in the repositorykeeps track of commits not otherwise accessible anymoreallows to recover from some mistakeslocal only and expires after 90 days

Example$ git add footxt$ git commit$ git reset --hard ltolder versiongt OOPS $ git reflog$ git checkout HEADn

December 2019 7884

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 99: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Sub-modules

Sub-modules provide a way to glue several existing repositories intoa bigger project

git submodule add url pathadds a submodule at pathgit submodule initinit the sub-modulesgit submodule updateclone or pull the submodulesgit submodule statusdisplay information about submodule status

December 2019 7984

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 100: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

git-lfs Large Files

Extension to support large binary files in git repositoriesuses external storage (cloud)replaces actual files with a linkon checkout fetch the real file from the external storagesaves space in the repositorybut adds a dependency to an external service

httpsgit-lfsgithubcom

December 2019 8084

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 101: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

git-crypt - encrypt contents

Secrets (passwords application keys) should not be stored in(public) git repositoriesgit-crypt provides a way to store encrypted contents with GPG orwith simple shared keys

Create gitattributessecretfile filter=git-crypt diff=git-cryptkey filter=git-crypt diff=git-crypt

Use$ git-crypt init$ git-crypt add-gpg-user USER_ID$ git-crypt unlock

December 2019 8184

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 102: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

BFG Repo-Cleaner

Tool to clean up mistakes commited in a repository remove Huge binary files (executables images video)

either commited by mistakeor removed voluntarly by git rm but still occupying space fornothing in the repository

remove passwords or other kind of confidential data commitedby mistake

Warning this modifies the repository need to inform all usersbefore pushing

httpsrtyleygithubiobfg-repo-cleaner

December 2019 8284

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 103: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Agenda

1 Introduction2 Git concepts3 Individual developer4 Using branches5 Working in teams6 Git work flows7 Advanced branching8 Good Practices9 Other goodies10 Webography

December 2019 8384

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography
Page 104: Source Code Management wih git - LAAS-CNRS · Agenda 1 Introduction 2 Git concepts 3 Individual developer 4 Using branches 5 Working in teams 6 Git work flows 7 Advanced branching

Webography

httpsgit-scmcombookenv2The online Pro Git bookhttpswwwatlassiancomgittutorialshttplearngitbranchingjsorggraphical tutorial on branchesGit for computer scientists Tommi Virtanen june 2007Demystifying Git internals Pawan Rawal august 2016

December 2019 8484

  • Introduction
  • Git concepts
  • Individual developer
  • Using branches
  • Working in teams
  • Git work flows
  • Advanced branching
  • Good Practices
  • Other goodies
  • Webography