GIT AVANC DAVID PARSONS 20 JUIN 2017 1 THINGS TO KNOW DAVID - - PowerPoint PPT Presentation

git avanc
SMART_READER_LITE
LIVE PREVIEW

GIT AVANC DAVID PARSONS 20 JUIN 2017 1 THINGS TO KNOW DAVID - - PowerPoint PPT Presentation

GIT AVANC DAVID PARSONS 20 JUIN 2017 1 THINGS TO KNOW DAVID PARSONS - GIT AVANC - 2 - 2 20 JUIN 2017 J Take home message J Commit o.en Keep commits small and commit together only related changes Write clear and


slide-1
SLIDE 1

GIT « AVANCÉ »

DAVID PARSONS 20 JUIN 2017

slide-2
SLIDE 2

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 2
  • 2

THINGS TO KNOW

1

slide-3
SLIDE 3

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 3

J Take home message J

  • Commit o.en
  • Keep commits small and commit together only related

changes

  • Write clear and informa9ve logs
  • A log should enable its reader to:

1. Iden9fy at a glance the ra9onale behind the commit 2. Have detailed explana9on if needed

  • Template for logs (from hFp://git-scm.com/book/ch5-2.html)

Short (50 chars or less) summary of changes More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separa9ng the summary from the body is cri9cal (unless you omit the body en9rely).

slide-4
SLIDE 4

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 4

Configuring git

$ # Configure your name and e-mail address (almost mandatory) $ git config --global user.name "David Parsons" $ git config --global user.email david.parsons@inria.fr $ $ # Configure the editor git will open when needed $ git config --global core.editor nano $ $ # Setup a few aliases, either simple shorthands... $ git config --global alias.co checkout $ git config --global alias.ci commit $ git config --global alias.st status $ $ # ... or including options $ git config --global alias.lg "log --pretty=format:\"%h - %an : %s\"" $ $ # You can even create new commands $ git config --global alias.unstage "reset HEAD"

slide-5
SLIDE 5

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 5

Tree-ish

A commit can be iden9fied in mul9ple ways, e.g. :

  • Its SHA1 (possibly abbreviated)
  • A reference (e.g. HEAD)
  • An indirect reference : HEAD^, HEAD~, …
  • A branch name
  • A tag
slide-6
SLIDE 6

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 6

gitk

slide-7
SLIDE 7

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 7

git-gui

slide-8
SLIDE 8

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 8
  • 8

FROM THE WORKING COPY TO THE REMOTE REPOSOTORY

2

slide-9
SLIDE 9

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 9

Working copy + Local repo

Each user has (on his own computer):

  • His own local repository (stored in

the .git repository)

  • A working copy (a.k.a. checkout) of

the tracked files

slide-10
SLIDE 10

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 10

Remote repositories

Repositories can be synchronized with one another In the centralized workflow, each user synchronises his repository with a central repo (the remote) located on a server

slide-11
SLIDE 11

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 11

The Staging Area (a.k.a. Index)

slide-12
SLIDE 12

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 12

File Status Lifecycle

slide-13
SLIDE 13

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 13

The corresponding commands…

  • Add a file
  • Edit a file
  • Stage a file
  • Remove a file
  • Commit a file

git add git stage (or git add) git rm git commit

… are quite straigh_orward : NB : git rm actually deletes the file from your working copy. If you want to keep it as an untracked file, use the --cached

  • p9on
slide-14
SLIDE 14

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 14

Illustra9ons : diff

Log rev 1 Log rev 2 Log rev 3 Add file baz Modifs in index Modifs not in index

git diff git diff --staged git diff HEAD

master

slide-15
SLIDE 15

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 15
  • 15

WORKING WITH BRANCHES

3

slide-16
SLIDE 16

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 16

What is a branch ?

branch

Each branch contains an alterna9ve version of the same set of files Git is meant for an extensive use of branches « master » is the default name for the main branch, i.e. that which is checked out by default

master

slide-17
SLIDE 17

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 17

Branch types

branch

Git doesn’t know about branch types, it is the usage you make of a branch that determines its “type” A branch may remain local to a user’s repo or be published (pushed) to the central repo Common use cases for branches include:

  • Feature
  • Bugfix

master

slide-18
SLIDE 18

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 18

Merging branches

featureA

Here we have merged the branch featureA into the branch master. This means we have injected the cons9tuent modifica9on of featureA into master This merge is embodied by a commit (unless it is a fast- forward) that has two parents and that include the changes made to both branches (here 5 sets of changes) The branch featureA is untouched

master

slide-19
SLIDE 19

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 19

Branches

$ git branch * master $ git branch featureA

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar master v0.1

slide-20
SLIDE 20

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 20

Branches

$ git branch * master $ git branch featureA $

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz featureA Add content to bar v0.1 master

slide-21
SLIDE 21

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 21

Branches

$ git branch * master $ git branch featureA $ git branch featureA * master $

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz featureA Add content to bar v0.1 master

slide-22
SLIDE 22

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 22

Branches

$ git branch * master $ git branch featureA $ git branch featureA * master $ git checkout featureA $ git branch * featureA master $ Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz featureA Add content to bar v0.1 master

  • You can also create and checkout a new branch at once with

git checkout –b newBranch

slide-23
SLIDE 23

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 23

Branches

$ # Commit stuff in branch featureA $ echo “Blabla” >> foo $ git ci foo -m “Modify foo”

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz featureA Add content to bar v0.1 master Modifs not in index

slide-24
SLIDE 24

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 24

Branches

$ # Commit stuff in branch featureA $ echo “Blabla” >> foo $ git ci foo -m “Modify foo” $

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add […] bar v0.1 Modify foo featureA master

slide-25
SLIDE 25

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 25

Branches

$ # Checkout branch master and commit stuff in it $ git co master $

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add […] bar v0.1 Modify foo featureA master

slide-26
SLIDE 26

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 26

Branches

Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Modify foo featureA $ # Checkout branch master and commit stuff in it $ git co master $ echo “Blabla” >> bar $ git ci foo -m “Modify bar” $ Modify bar master

slide-27
SLIDE 27

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 27

Merge without conflicts

Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Modify foo featureA Modify bar master $ git merge featureA

slide-28
SLIDE 28

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 28

Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Modify bar Modify foo featureA Merge branch ‘featureA’ master

When there are no conflicts whatsoever, git merge automa9cally triggers the crea9on of the merge commit. It will open your core editor with a predefined log msg

$ git merge featureA $

  • Merge without conflicts
slide-29
SLIDE 29

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 29
  • 29

MANAGING CONFLICTS

4

slide-30
SLIDE 30

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 30

Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Modify bar featureA Modify bar $ git merge featureA

  • master

Managing conflicts

slide-31
SLIDE 31

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 31

Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Modify bar featureA Modify bar $ git merge featureA Auto-merging bar CONFLICT (content): Merge conflict in bar Automatic merge failed; fix conflicts and then commit the result. $

  • master

Managing conflicts

slide-32
SLIDE 32

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 32

Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Modify bar featureA Modify bar $ git merge featureA Auto-merging bar CONFLICT (content): Merge conflict in bar Automatic merge failed; fix conflicts and then commit the result. $ $ cat bar This is line 1 This is line 2 This is line 3 <<<<<<< HEAD This is the fourth line ======= This is line number 4 >>>>>>> featureA This is line 5 This is line 6 This is line 7 This is line 8 $

  • master

Managing conflicts

slide-33
SLIDE 33

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 33

Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Modify bar featureA Modify bar $ # You can edit the conflicting files directly and then stage and commit them, or you can use a merge tool $ $ git mergetool

  • master

Managing conflicts

slide-34
SLIDE 34

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 34

mergetool

slide-35
SLIDE 35

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 35

$ git config --global merge.tool meld $ git config --global mergetool.meld.cmd 'meld $LOCAL $MERGED $REMOTE' $ git config --global mergetool.meld.trustExitCode false $

Configuring a mergetool

slide-36
SLIDE 36

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 36
  • 36

OTHER THINGS TO KNOW

5

slide-37
SLIDE 37

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 37

Tags

$ # Create an annotated tag named "v0.1" $ git tag -a v0.1 -m “version 0.1”

  • $ # List tags

$ git tag v0.1 $ Log rev 3 Add content to foo Add file baz Add content to bar v0.1 master

A tag is typically used to mark a release point. There are 2 types of tags:

  • Lightweight (just a pointer to a specific commit)
  • Annotated (full object, contains addi9onal info, can be signed)
slide-38
SLIDE 38

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 38

Tags

Tags have to be pushed and fetched manually: git push origin <tagname> or git push origin --tags git fetch origin --tags

slide-39
SLIDE 39

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 39

Detached head ?

You are in a detached head state when you are not “on” any branch Most of the 9me, this happens when you checkout anything that is not a branch (e.g. a tag). You can also use git checkout –detach When in detached head, you can commit as you like ; but be wary of the garbage collector, it could very well erase your work if you don’t pay aFen9on ! But git is kind, it tells you that and what to do …

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.

  • If you want to create a new branch to retain commits you create, you may

do so (now or later) by using -b with the checkout command again. Example:

  • git checkout -b new_branch_name
slide-40
SLIDE 40

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 40

Stash

Literally “garder sous la coude”

# You’re working on something and you need to switch to something else $ git co something_else error: Your local changes to the following files would be overwritten by checkout $ git stash [save] Saved working directory and index state WIP on master: […] $ git stash list stash@{0}: WIP on master: […] $ git co something_else # Now we can (WD is clean) ! # Do things on something_else $ git co master # Back to master $ git stash pop # Back to initial state

slide-41
SLIDE 41

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 41
  • 41

INTERACTING WITH REMOTES

6

slide-42
SLIDE 42

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 42

Working in parallel

Scenario

slide-43
SLIDE 43

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 43

Working in parallel

clone clone

master

Central repo

slide-44
SLIDE 44

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 44

master r/o/master

Working in parallel

master master r/o/master

Central repo

slide-45
SLIDE 45

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 45

Working in parallel

master master master r/o/master r/o/master

Central repo

slide-46
SLIDE 46

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 46

Working in parallel

master remotes/origin/master master master

push

r/o/master

Central repo

slide-47
SLIDE 47

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 47

Working in parallel

master master master r/o/master r/o/master

Central repo

slide-48
SLIDE 48

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 48

Working in parallel

master master master r/o/master

push

r/o/master

Central repo

slide-49
SLIDE 49

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 49

Working in parallel

master master master r/o/master

push

r/o/master

Central repo

slide-50
SLIDE 50

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 50

Working in parallel

master master master r/o/master

fetch

r/o/master

Central repo

slide-51
SLIDE 51

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 51

Working in parallel

master master master r/o/master r/o/master

Central repo

slide-52
SLIDE 52

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 52

Working in parallel

master master r/o/master master r/o/master

Central repo

slide-53
SLIDE 53

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 53

Working in parallel

master master r/o/master master

push

r/o/master

Central repo

slide-54
SLIDE 54

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 54

Working in parallel

master r/o/master master master r/o/master

Central repo

slide-55
SLIDE 55

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 55

Working in parallel

master r/o/master master master

pull

r/o/master

Central repo

slide-56
SLIDE 56

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 56

Working in parallel

master master r/o/master master r/o/master

Central repo

slide-57
SLIDE 57

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 57

Working in parallel

Details of the corresponding Commands

slide-58
SLIDE 58

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 58

Remote Branches

Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz master Add content to bar v0.1 remotes/origin/master $ # Commit stuff $ echo "This is the content of bar" > bar $ git ci bar -m "Modify bar”

slide-59
SLIDE 59

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 59

Remote Branches

$ # Commit stuff $ echo "This is the content of bar" > bar $ git ci bar -m "Modify bar" $

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 remotes/origin/master master Modify bar

slide-60
SLIDE 60

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 60

Push

$ # Push your commits onto the remote repository $ git push

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 remotes/origin/master master Modify bar

slide-61
SLIDE 61

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 61

Push

$ # Push your commits onto the remote repository $ git push Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 318 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To /Users/dparsons/tmp/git-sandbox2/../git-sandbox 119dfba..a8f9783 master -> master $ Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 master Modify bar remotes/origin/master

slide-62
SLIDE 62

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 62

Other user’s viewpoint

$ # Commit stuff $ echo "This is bar’s content" > bar $ git ci bar -m "Change bar”

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz v0.1 master Add content to bar remotes/origin/master

slide-63
SLIDE 63

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 63

$ # Commit stuff $ echo "This is bar’s content" > bar $ git ci bar -m "Change bar" $

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 remotes/origin/master master Change bar

Other user’s viewpoint

slide-64
SLIDE 64

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 64

$ git push

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 remotes/origin/master master Change bar

Other user’s viewpoint

slide-65
SLIDE 65

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 65

$ git push To /Users/dparsons/tmp/git-sandbox/.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to ’[…]' hint: Updates were rejected because the tip of your current branch is hint: behind its remote counterpart. Integrate the remote changes hint: (e.g. 'git pull ...') before pushing again. See the 'Note about hint: fast-forwards' in 'git push --help' for details. $ Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 remotes/origin/master master Change bar

Other user’s viewpoint

slide-66
SLIDE 66

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 66

$ git fetch

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 remotes/origin/master master Change bar

Other user’s viewpoint

slide-67
SLIDE 67

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 67

$ git fetch From /Users/dparsons/tmp/git-sandbox/ 119dfba..a8f9783 master -> origin/master $

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Modify bar Change bar master remotes/origin/master

Other user’s viewpoint

slide-68
SLIDE 68

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 68

$ git merge origin/master Auto-merging bar CONFLICT (content): Merge conflict in bar Automatic merge failed; fix conflicts and then commit the result. $ git mergetool $ git ci $ Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Change bar Modify bar Merge remote-tracking branch 'origin/master' master remotes/origin/master

Other user’s viewpoint

slide-69
SLIDE 69

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 69

$ git push $

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Change bar Modify bar Merge remote-tracking branch 'origin/master' master remotes/origin/master

Other user’s viewpoint

slide-70
SLIDE 70

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 70

Pull

$ git pull

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 master Modify bar remotes/origin/master

slide-71
SLIDE 71

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 71

Pull

$ git pull […] $

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Change bar Modify bar Merge remote-tracking branch 'origin/master' master remotes/origin/master

slide-72
SLIDE 72

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 72

Working in parallel

More about remotes and remote branches

slide-73
SLIDE 73

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 73

About remotes

  • Remote branches are local references that you cannot

move (moved automa9cally on communica9on with the remote)

  • origin is the default name of the default remote
  • Add a remote:

git remote other add git://…

  • Fetch remote branches:

git fetch other

  • Push master to remotes/other/master

git push other master

slide-74
SLIDE 74

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 74

Remote tracking branches

  • Remote tracking branches are local branches that

have a direct rela9onship to a remote branch (useful to simplify opera9ons such as push, pull, merge, rebase, …)

  • Make current branch track remotes/other/master:

git branch –u other/master

  • List local branches and their remote counterpart:

git branch -vv

slide-75
SLIDE 75

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 75

Annoying things

  • Delete a remote branch:

git push origin --delete featA

  • Push a tag:

git push origin mytag or git push --tags

slide-76
SLIDE 76

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 76
  • 76

Partial commits

7

slide-77
SLIDE 77

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 77

Add -p

Works but… a liFle tedious !

$ git add -p diff --git a/file b/file index e72f11f..75ad869 100644

  • -- a/file

+++ b/file @@ -1,7 +1,7 @@ ... ... ...

  • Original line

+ Modified line ... ... ... Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]?

slide-78
SLIDE 78

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 78

git gui

slide-79
SLIDE 79

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 79
  • 79

Rewriting History (gentle intro)

8

slide-80
SLIDE 80

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 80

Rewri9ng History ?

Remember you have a local repository ? Everything you haven’t published (i.e. pushed) yet is strictly local to your repo. It is known by you and no one else. Since then, what prevents you from modifying it ? This is one of my favourite things about git, I can be stupid and appear not to be !

# You’ve just commited something and realize you forgot to add a file git add the_forsaken_file git commit --amend # No one saw you ;)

WARNING: Do not do that if you’ve pushed the faulty commit !!!!!

slide-81
SLIDE 81

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 81

Commit --amend

Let’s look at what our commit graph looks like

… Add new class Foo Add new class Foo master

Faulty commit missing e.g. a file This commit is not referred to by any ref, it will eventually be garbage collected Replacement commit

slide-82
SLIDE 82

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 82

Basic rebase

C3 C1 C2 master feature

Branch master has been updated since you started to write your feature. You would like to benefit from these updates (or perhaps have an up-to-date feature to propose as a pull-request ?)

$ git rebase master First, rewinding head to replay your work on top of it… Applying: … C1 C2 master feature C2 C3

Replacement commit Original commit

slide-83
SLIDE 83

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 83

Less basic rebase

Rebase literally means “set a new base for a branch” However, if it’s trivial to tell where a branch ends, it’s not that simple for where it starts... And what should be the new base for our branch is a similar maFer… So, given your commit tree, rebasing a branch is telling git to 1. “Pick” a branch 2. “Cut” it (but where ?) 3. “GraE” it (onto another part of the tree)

slide-84
SLIDE 84

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 84

Less basic rebase

If you specify nothing, you pick the current branch, cut it at its LCA (last common ancestor) with its remote tracking branch and graE it at this exact same loca9on (which means you’ve achieved to do nothing in a complicated way) The most common case is to give a single arg, i.e. git rebase master. In that case, you pick the current branch, cut it at its LCA with master and graE it at the 9p of master You can also specify where to cut with the op9on --root, where to graE with the op9on --onto and where to pick (last argument)

$ git rebase --root <commit> --onto <new-base> <branch-to-rebase> $ git rebase <upstream> --onto <new-base> <branch-to-rebase>

slide-85
SLIDE 85

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 85
  • 85

Merge or rebase ?

9

slide-86
SLIDE 86

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 86

Prefer fetch over pull

The pull command is most of the 9me equivalent to a fetch followed by a merge. So what you are really asking git to do when you pull is to merge your work with something you know nothing about (!) To come around this problem, start by fetching what’s new from the remote and have a look at it. If what you really want is a merge, you can do it. But this 9me, you do it knowingly

slide-87
SLIDE 87

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 87

Rebase

$ git fetch $ git rebase

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Modify bar Change bar master remotes/origin/master

slide-88
SLIDE 88

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 88

Rebase

$ git fetch $ git rebase … $ Log rev 1 Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar v0.1 Change bar Change bar master remotes/origin/master Modify bar

Replacement commit Original commit

slide-89
SLIDE 89

LIEU LOCALISATION

www.inria.fr

Antenne INRIA Lyon la Doua

www.inria.fr

Thank you

DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 89

  • Rebasing basically means re-wri9ng the history of

what happened. This is a very powerful feature. But as always, great power comes with great responsibility: DO NOT REBASE COMMITS THAT EXIST OUTSIDE YOUR REPOSITORY

Rebase only local commits

slide-90
SLIDE 90

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 90

Which one do you like most ?

Merge Rebase

Merge or rebase ?

slide-91
SLIDE 91

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 91

Merge or rebase ?

Or maybe this mixed one ?

Merge Rebase Mixed

slide-92
SLIDE 92

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 92
  • 92

Undoing things

10

slide-93
SLIDE 93

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 93

Unstage

Unstaging an en9re file is very easy, git tells you how to do it:

$ git status (use "git reset HEAD <file>..." to unstage) $ git reset HEAD <file>

Don’t want to have to remember this command ? Create an alias:

$ git config --global alias.unstage "reset HEAD" $ git unstage <file>

If you don’t want to unstage the en9re file but only some parts of it, the easiest solu9on is probably git gui (but you could also use git reset -p)

slide-94
SLIDE 94

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 94

Unmodify a file

Again, git is kind enough to prompt you for ac9ons you might want to do:

$ git status (use "git checkout -- <file>..." to discard changes …) $ git co -- <file>

And again you can create an alias:

$ git config --global alias.unmod ”checkout --" $ git unmod <file>

If you don’t want to unmodify a whole file but only some parts of it, the easiest solu9on is probably a ditool (but you could also use git co -p)

slide-95
SLIDE 95

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 95

Undo a commit

  • If the commit has not been published yet

$ git rebase –i ...

  • If it has (been published)

$ git revert <commit-to-undo>

This will create a new commit whose diff is the inverse of that of the commit to undo This way, you can thoroughly remove the faulty commit from the history

slide-96
SLIDE 96

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 96

git reset

Git reset can become very handy when things are beginning to get awry It allows you to make a branch point anywhere you want. Let’s say you have commiFed stuff in master when what you really wanted was to commit them in feature:

C1 C2 C3 master C0 feature

What you have What you want

C1 C2 C3 feature C0 master

slide-97
SLIDE 97

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 97

git reset

$ git co feature

  • C1

C2 C3 master C0 feature C1 C2 C3 feature C0 master

What you have What you want

slide-98
SLIDE 98

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 98

git reset

$ git co feature Switched to branch 'feature' $ # ?

  • C1

C2 C3 master C0 feature C1 C2 C3 feature C0 master

What you have What you want

slide-99
SLIDE 99

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 99

git reset

$ git co feature Switched to branch 'feature' $ git (merge | rebase | reset --hard) master

  • C1

C2 C3 feature C0 master

What you want

C1 C2 C3 master C0 feature

What you have

slide-100
SLIDE 100

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 100

git reset

$ git co feature Switched to branch 'feature' $ git (merge | rebase | reset --hard) master ... $

  • C1

C2 C3 master C0 feature C1 C2 C3 feature C0 master

What you have What you want

slide-101
SLIDE 101

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 101

git reset

$ git co feature Switched to branch 'feature' $ git (merge | rebase | reset --hard) master ... $ git co master Switched to branch 'master' $ # ?

  • C1

C2 C3 master C0 feature

What you have

C1 C2 C3 feature C0 master

What you want

slide-102
SLIDE 102

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 102

git reset

$ git co feature Switched to branch 'feature' $ git (merge | rebase | reset --hard) master ... $ git co master Switched to branch 'master' $ git reset --hard bdfa5a # ref-to-C0

  • C1

C2 C3 master C0 feature C1 C2 C3 feature C0 master

What you have What you want

slide-103
SLIDE 103

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 103

git reset

$ git co feature Switched to branch 'feature' $ git (merge | rebase | reset --hard) master ... $ git co master Switched to branch 'master' $ git reset --hard bdfa5a # ref-to-C0 HEAD is now at bdfa5ab C0 $

  • C1

C2 C3 feature C0 master C1 C2 C3 feature C0 master

What you have What you want

slide-104
SLIDE 104

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 104

git reset

$ git co feature Switched to branch 'feature' $ git (merge | rebase | reset --hard) master ... $ git co master Switched to branch 'master' $ git reset --hard bdfa5a # ref-to-C0 HEAD is now at bdfa5ab C0 $ git co feature $

  • C1

C2 C3 feature C0 master C1 C2 C3 feature C0 master

What you have What you want BINGO !

slide-105
SLIDE 105

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 105

soft, mixed or hard ?

There are 3 main op9ons to git reset : so., mixed and hard. There are also 3 steps that can be done: 1. Move the current branch 2. Update the staging area 3. Update the working directory

  • -so. does only 1.
  • -mixed does 1. and 2.
  • -hard goes all the way to 3.
slide-106
SLIDE 106

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 106

Cheat-sheet

slide-107
SLIDE 107

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 107

More undoing…

git-filter-branch is a very powerful tool, it allows you to apply “filters” on each revision of a branch. This can be useful e.g. when you’ve commiFed a file that should never have been added (binary file, confiden9al informa9on, …)

$ git filter-branch –tree-filter 'rm -f <file>’ HEAD

An alterna9ve to git-filter-branch for “cleansing bad data out of a git repo” is worth men9oning : BFG Repo-Cleaner (hFps://rtyley.github.io/bfg- repo-cleaner/)

slide-108
SLIDE 108

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 108
  • 108

Cherry picking

11

slide-109
SLIDE 109

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 109

Cherry picking

Cherry picking is similar to rebasing in the same way that checking out is similar to resevng. You can do the same kind of things except that you don’t reset anything Cherry picking is really easy and very handy. It allows you to apply the changes introduced by one or more commits on top

  • f HEAD.

C4 C1 C2 master feature C3 $ git cherry-pick <C3> C3 C1 C2 master feature C3 C4

slide-110
SLIDE 110

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 110
  • 110

Interactive rebase

12

slide-111
SLIDE 111

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 111

Interac9ve rebase

Remember what we did with git commit --amend? Now imagine you did the exact same thing but have already added a few commits on top of the faulty one. You can’t use commit --amend because it only allows to modify the very latest commit. This is when the interac9ve rebase becomes handy (even though this par9cular rewrite could be done with non-interac9ve rebase) Let’s see what the command looks like

$ git rebase --interactive

What you’re rebasing and onto what, follows the same rules as non- interac9ve rebase. The difference is that you will get to decide what to do with each commit to be replayed

slide-112
SLIDE 112

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 112

Interac9ve rebase

slide-113
SLIDE 113

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 113
  • 113

Other interesting features

13

slide-114
SLIDE 114

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 114

git bisect

You’ve just found a major bug and need to know when it was introduced. Don’t panic, git bisect is here to help you, it using dichotomy to locate the first “bad” commit quickly

$ git bisect start $ git bisect bad # Current version is bad $ git bisect good <a-good-old-version> Bisecting: 675 revisions left to test after this (roughly 10 steps) $ git bisect good|bad …

slide-115
SLIDE 115

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 115

git subrepo

Alterna9ves: submodules, subtrees Git subrepo allows you to work with embedded git repositories

# Clone an existing repo as a subrepo (in a subdirectory) $ git subrepo clone <url> [<subdir>]

  • # Create an embedded git repo

$ git subrepo init <subdir>

  • # Pull from upstream

$ git subrepo pull <subdir>

  • # Push to upstream

$ git subrepo push <subdir>

slide-116
SLIDE 116

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 116

git annex

Alterna9ves: lfs Git annex provides an interes9ng solu9on to version large files, it basically handles symlinks and provides tools to manage the actual files behind the links

# Prepare an existing git repo for git annex $ git annex init

  • $ git annex add <file>

$ git ci # => commits a symlink and stores the file in .git/annex

Git annex can be used with a wide variety of types of remote storage spaces (special remotes)

slide-117
SLIDE 117

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 117

Hooks

Hooks are custom scripts that can be automa9cally triggered when certain important ac9ons occur Client-side hooks: pre-commit, prepare-commit-msg, commit-msg, post-commit

pre-rebase, post-rewrite, post-checkout, post-merge, pre-push, pre-auto-gc

Server-side hooks:

pre-receive, update, post-receive

On pla_orms such as forges or gitlabs/github, some (many ?) predefined hooks can be set up in a maFer of minutes Hooks can be used e.g. to send no9fica9ons, run tests prior to commits, enforce any kind of policies, …

slide-118
SLIDE 118

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 118

Mul9ple remotes, push requests

Many prjects on github/lab allow third-party contribu9ons by a mechanism called pull-request. This mechanism can also be used internally for code- review Mul9ple remotes can be very usefull in the context of workflows including pull-requests but also in other cases (git annex, migra9ons, …)

slide-119
SLIDE 119

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 119

Rerere

One of the most annoying things that a git-user has to do is to resolve conflicts. Even more annoying would be to have to resolve the same conflict several

  • 9mes. Sadly, this happens. Mostly when relying heavily on rebase (?)

Rerere helps you avoiding this situa9on by reusing recorded resolu9ons

slide-120
SLIDE 120

LIEU LOCALISATION

www.inria.fr

Antenne INRIA Lyon la Doua

www.inria.fr

Thank you

slide-121
SLIDE 121

20 JUIN 2017 DAVID PARSONS - GIT « AVANCÉ »

  • 121
  • 121

Illustration of the Main Commands

A1

slide-122
SLIDE 122

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 122

Illustra9ons : status

$ ls foo bar $ git status On branch master

  • nothing to commit, working directory clean

$

  • Log rev 1

Log rev 2 Log rev 3 master

slide-123
SLIDE 123

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 123

Illustra9ons : untracked file

$ ls foo bar $ git status On branch master

  • nothing to commit, working directory clean

$ $ # Create a new file baz $ echo "A new file…" > baz $ $ git status On branch master Untracked files: (use "git add <file>…" to include in what will be committed

  • baz
  • nothing added to commit but untracked files

present (use "git add" to track) $ Log rev 1 Log rev 2 Log rev 3 master

slide-124
SLIDE 124

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 124

Illustra9ons : add

$ git add baz

  • Log rev 1

Log rev 2 Log rev 3 master

slide-125
SLIDE 125

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 125

Illustra9ons : add

$ git add baz $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)

  • new file: baz
  • $
  • Log rev 1

Log rev 2 Log rev 3 Modifs in index master

slide-126
SLIDE 126

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 126

Illustra9ons : commit

$ git add baz $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)

  • new file: baz
  • $ git commit
  • Log rev 1

Log rev 2 Log rev 3 Modifs in index master

slide-127
SLIDE 127

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 127

Illustra9ons : commit

$ git add baz $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)

  • new file: baz
  • $ git commit

[master 98aa7ff] Add file baz 1 file changed, 1 insertion(+) create mode 100644 baz $ git status On branch master nothing to commit, working directory clean $

  • Log rev 1

Log rev 2 Log rev 3 Add file baz master

slide-128
SLIDE 128

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 128

Illustra9ons : log

$ git log commit 579001a8a6f83cb6547440ba319f7210916ece6b Author: David Parsons <david.parsons@inria.fr> Date: Sat Jan 9 17:53:11 2016 +0100

  • Add file baz
  • commit e82a7786b5ebfc1d0e62eb00ae2101a328d3f0e4

Author: David Parsons <david.parsons@inria.fr> Date: Sat Jan 9 17:44:29 2016 +0100

  • Log rev3
  • commit f6813a3b9f081bb486975dfe5b6a426effabf61c

Author: David Parsons <david.parsons@inria.fr> Date: Sat Jan 9 17:43:39 2016 +0100

  • Log rev2
  • commit f667b6e8847d20c67968c2b2d8a85fac7076251f

: Log rev 1 Log rev 2 Log rev 3 Add file baz master

slide-129
SLIDE 129

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 129

Illustra9ons : log

$ git log --pretty=oneline 579001a8a6f83cb6547440ba319f7[…] Add file baz e82a7786b5ebfc1d0e62eb00ae210[…] Log rev3 f6813a3b9f081bb486975dfe5b6a4[…] Log rev2 f667b6e8847d20c67968c2b2d8a85[…] Log rev1 $ $ git log --pretty=format:"%h - %an : %s" 579001a - David Parsons : Add file baz e82a778 - David Parsons : Log rev3 f6813a3 - David Parsons : Log rev2 f667b6e - David Parsons : Log rev1 $

  • Log rev 1

Log rev 2 Log rev 3 Add file baz master

slide-130
SLIDE 130

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 130

$ # Add content to foo $ echo "Added content" >> foo

  • Log rev 1

Log rev 2 Log rev 3 Add file baz master

Illustra9ons : edit tracked file

slide-131
SLIDE 131

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 131

Illustra9ons : edit tracked file

$ # Add content to foo $ echo "Added content" >> foo $

  • Log rev 1

Log rev 2 Log rev 3 Add file baz Modifs not in index master

slide-132
SLIDE 132

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 132

Illustra9ons : edit tracked file

$ # Add content to foo $ echo "Added content" >> foo $ $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)

  • modified: foo
  • no changes added to commit (use "git add" and/
  • r "git commit -a")

$

  • Log rev 1

Log rev 2 Log rev 3 Add file baz Modifs not in index master

slide-133
SLIDE 133

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 133

Illustra9ons : stage

$ # Mark changes in foo as ‘to be committed’ $ git stage foo

  • Log rev 1

Log rev 2 Log rev 3 Add file baz Modifs not in index master

slide-134
SLIDE 134

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 134

Illustra9ons : stage

$ # Mark changes in foo as ‘to be committed’ $ git stage foo $ $ # The exact same thing could have been done with git add foo $ $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)

  • modified: foo
  • $
  • Log rev 1

Log rev 2 Log rev 3 Add file baz Modifs in index master

slide-135
SLIDE 135

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 135

Index and Working Copy

$ # Add content to bar $ echo "Content added to bar" >> bar

  • Log rev 1

Log rev 2 Log rev 3 Add file baz Modifs in index master

slide-136
SLIDE 136

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 136

$ # Add content to bar $ echo "Content added to bar" >> bar $ $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)

  • modified: foo
  • Changes not staged for commit:

(use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)

  • modified: bar
  • $
  • Log rev 1

Log rev 2 Log rev 3 Add file baz Modifs in index Modifs not in index master

Index and Working Copy

slide-137
SLIDE 137

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 137

Illustra9ons : diff

Log rev 1 Log rev 2 Log rev 3 Add file baz Modifs in index Modifs not in index

git diff git diff --staged git diff HEAD

master

slide-138
SLIDE 138

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 138

$ # Add more content to foo $ echo "more content" >> foo $ $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)

  • modified: foo
  • Changes not staged for commit:

(use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)

  • modified: bar

modified: foo

  • $
  • Log rev 1

Log rev 2 Log rev 3 Add file baz Modifs in index Modifs not in index master

Index and Working Copy

slide-139
SLIDE 139

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 139

Bypassing the index

$ # Commit working copy state of foo $ git commit foo

  • Log rev 1

Log rev 2 Log rev 3 Add file baz Modifs in index Modifs not in index master

slide-140
SLIDE 140

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 140

$ # Commit working copy state of foo $ git commit foo [master 5e60acc] Add content to foo 1 file changed, 2 insertions(+) $ $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)

  • modified: bar
  • $
  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Modifs not in index Add file baz master

Bypassing the index

slide-141
SLIDE 141

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 141

$ # Commit working copy state of all tracked files $ $ git commit -a -m "Add content to bar”

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Modifs not in index Add file baz master

Bypassing the index

slide-142
SLIDE 142

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 142

$ # Commit working copy state of all tracked files $ $ git commit -a -m "Add content to bar" [master 119dfba] Add content to bar 1 file changed, 1 insertion(+) $ git status On branch master nothing to commit, working directory clean $

  • Log rev 1

Log rev 2 Log rev 3 Add content to foo Add file baz Add content to bar master

Bypassing the index

slide-143
SLIDE 143

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 143

Configuring git

$ # Configure your name and e-mail address (almost mandatory) $ git config --global user.name "David Parsons" $ git config --global user.email david.parsons@inria.fr $ $ # Configure the editor git will open when needed $ git config --global core.editor nano $ $ # Setup a few aliases, either simple shorthands... $ git config --global alias.co checkout $ git config --global alias.ci commit $ git config --global alias.st status $ $ # ... or including options $ git config --global alias.lg "log --pretty=format:\"%h - %an : %s\"" $ $ # You can even create new commands $ git config --global alias.unstage "reset HEAD"

slide-144
SLIDE 144

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 144

Tree-ish

A commit can be iden9fied in mul9ple ways, e.g. :

  • Its SHA1 (possibly abbreviated)
  • A reference (e.g. HEAD)
  • An indirect reference : HEAD^, HEAD~, …
  • A branch name
  • A tag
slide-145
SLIDE 145

20 JUIN 2017 DAVID PARSONS – GIT « AVANCÉ » 20 JUIN 2017 - 145

Tags

$ # Create a tag named "v0.1" $ git tag v0.1

  • $ # List tags

$ git tag v0.1 $ Log rev 3 Add content to foo Add file baz Add content to bar v0.1 master

Tagging a commit simply means giving it a name. This commit can then be referenced by this tag Tags are not pushed by default, use

git push origin <tagname> or git push --tags