INTRODUCTION TO GIT · DAVID PARSONS - INTRODUCTION TO GIT 12 The Staging Area (a.k.a Index) •...
Transcript of INTRODUCTION TO GIT · DAVID PARSONS - INTRODUCTION TO GIT 12 The Staging Area (a.k.a Index) •...
INTRODUCTION TO GIT
DAVID PARSONS
DAVID PARSONS - INTRODUCTION TO GIT 2 DAVID PARSONS - INTRODUCTION TO GIT
Source Code Management Tools 1
DAVID PARSONS - INTRODUCTION TO GIT 3
• Sourcecodemanagers(scm)a.k.a.versioncontrolsystems(vcs)
• Allowforthecompletetrackingandarchivingofthemodifica=onsmadetoasetoffiles
• Makecollabora=vedevelopmenteasier• UsuallyexistonallplaBormsusingthe
commandlineoragraphicaluserinterface• Areprimarilymeanttomanagetextfilesbut
canalsohandlebinaryfiles(e.g.images).However:donottrackanygeneratedfiles
• Arenotadaptedformanagingbackups!
DAVID PARSONS - INTRODUCTION TO GIT 4
The«DropBox»case
DAVID PARSONS - INTRODUCTION TO GIT 5
Resolu=onusinganSCM
DAVID PARSONS - INTRODUCTION TO GIT 6
Resolu=onusinganSCM
DAVID PARSONS - INTRODUCTION TO GIT 7
RepositoriesandRevisions
• ARepository• Containsthecompletehistoryofthe
project(i.e.alltherevisions)
• Arevision(a.k.a.commitorversion)• Isasnapshotofallthetrackedfiles• Isusuallybasedupononeotherrevision• Correspondstoaniden=fiedauthor• Containsamessagethatexplainsthe
ra=onaleforthemodifica=onsintroducedbytherevisionandanyotherinfotheauthorconsidersrelevant
A1 B C
A2 B1 C
A B CRev1
Rev2
Rev3
DAVID PARSONS - INTRODUCTION TO GIT 8
Twofundamentalrules!
• CommitoRen• Keepcommitssmallandcommittogetheronlyrelated
changes
• Writeclearandinforma=velogs• Alogshouldenableitsreaderto:
1. Iden=fyataglancethera=onalebehindthecommit2. Havedetailedexplana=onifneeded
• Templateforlogs(fromhUp://git-scm.com/book/ch5-2.html)
Short(50charsorless)summaryofchangesMoredetailedexplanatorytext,ifnecessary.Wrapittoabout72charactersorso.Insomecontexts,thefirstlineistreatedasthesubjectofanemailandtherestofthetextasthebody.Theblanklinesepara=ngthesummaryfromthebodyiscri=cal(unlessyouomitthebodyen=rely).
DAVID PARSONS - INTRODUCTION TO GIT 9 DAVID PARSONS - INTRODUCTION TO GIT
Git – Overview 2
DAVID PARSONS - INTRODUCTION TO GIT 10
• Gitisadistributedversioncontrolsystem
• Originalauthor:LinusTorvalds• Firstversiondevelopedin~1weekaRersometroublewithBitKeeper(proprietarysolu=on)
• Thereference:hUp://git-scm.com/• Notoriouscompe=tors:svn,mercurial
Context
DAVID PARSONS - INTRODUCTION TO GIT 11
Workingcopy+Localrepo
Eachuserhas(onhisowncomputer):• Hisownlocalrepository(storedin
the.gitrepository)• Aworkingcopy(a.k.a.checkout)of
thetrackedfiles.
Youmaycheckoutanyrevision(commit)oftheprojectusingthecommand:git checkout <commit>
Thiswillpopulateyourworkingcopywiththecontentofthespecifiedcommit
DAVID PARSONS - INTRODUCTION TO GIT 12
TheStagingArea(a.k.aIndex)
• “Paya0en1onnow”(gitbook–Gitbasics)• Inaddi=ontotherepositoryitselfandtheworkingcopy,thereisathirdsec=oningitcalledthestagingarea.
• Thestagingareaisanintermediatespacebetweentheworkingcopyandtherepositorywhereonecanpreparewhat’stobeincludedinthenextcommit.
• Inshort,itisacommit-prepara1on-area
DAVID PARSONS - INTRODUCTION TO GIT 13
TheStagingArea(a.k.a.Index)
DAVID PARSONS - INTRODUCTION TO GIT 14
Remoterepositories
Repositoriescanbesynchronizedwithoneanother.Inthecentralizedworkflow,eachusersynchroniseshisrepositorywithacentralrepo(theremote)locatedonaserver.
DAVID PARSONS - INTRODUCTION TO GIT 15
LocalRepo
«Integrated»view
WorkingCopy
StagingArea
CommiUedRevisions
RemoteRepo
CommiUedRevisions
RemoteLocal
DAVID PARSONS - INTRODUCTION TO GIT 16
FileStatusLifecycle
DAVID PARSONS - INTRODUCTION TO GIT 17
Thecorrespondingcommands…
• Addafile• Editafile• Stageafile• Removeafile• Commitafile
git add
git stage (orgit add)git rm
git commit
…arequitestraighBorward:
NB:git rm actuallydeletesthefilefromyourworkingcopy.Ifyouwanttokeepitasanuntrackedfile,usethe--cached op=on
DAVID PARSONS - INTRODUCTION TO GIT 18 DAVID PARSONS - INTRODUCTION TO GIT
Graphical User Interfaces 3
DAVID PARSONS - INTRODUCTION TO GIT 19
gitk
DAVID PARSONS - INTRODUCTION TO GIT 20
git-gui
DAVID PARSONS - INTRODUCTION TO GIT 21 DAVID PARSONS - INTRODUCTION TO GIT
Illustration of the Main Commands 4
DAVID PARSONS - INTRODUCTION TO GIT 22
Illustra=ons:status$ lsfoo bar$ git statusOn branch master
nothing to commit, working directory clean$
Logrev1
Logrev2
Logrev3master
DAVID PARSONS - INTRODUCTION TO GIT 23
Illustra=ons:untrackedfile $ lsfoo bar$ git statusOn branch master
nothing to commit, working directory clean$$ # Create a new file baz$ echo "A new file…" > baz$$ git statusOn branch masterUntracked 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)$
Logrev1
Logrev2
Logrev3master
DAVID PARSONS - INTRODUCTION TO GIT 24
Illustra=ons:add$ git add baz
Logrev1
Logrev2
Logrev3master
DAVID PARSONS - INTRODUCTION TO GIT 25
Illustra=ons:add$ git add baz$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)
new file: baz
$
Logrev1
Logrev2
Logrev3
Modifsinindex
master
DAVID PARSONS - INTRODUCTION TO GIT 26
Illustra=ons:commit$ git add baz$ git statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)
new file: baz
$ git commit
Logrev1
Logrev2
Logrev3
Modifsinindex
master
DAVID PARSONS - INTRODUCTION TO GIT 27
Illustra=ons:commit$ git add baz$ git statusOn branch masterChanges 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 statusOn branch masternothing to commit, working directory clean$
Logrev1
Logrev2
Logrev3
Addfilebazmaster
DAVID PARSONS - INTRODUCTION TO GIT 28
Illustra=ons:log$ git logcommit 579001a8a6f83cb6547440ba319f7210916ece6bAuthor: David Parsons <[email protected]>Date: Sat Jan 9 17:53:11 2016 +0100
Add file baz
commit e82a7786b5ebfc1d0e62eb00ae2101a328d3f0e4Author: David Parsons <[email protected]>Date: Sat Jan 9 17:44:29 2016 +0100
Log rev3
commit f6813a3b9f081bb486975dfe5b6a426effabf61cAuthor: David Parsons <[email protected]>Date: Sat Jan 9 17:43:39 2016 +0100
Log rev2
commit f667b6e8847d20c67968c2b2d8a85fac7076251f:
Logrev1
Logrev2
Logrev3
Addfilebazmaster
DAVID PARSONS - INTRODUCTION TO GIT 29
Illustra=ons:log$ git log --pretty=oneline579001a8a6f83cb6547440ba319f7[…] Add file baze82a7786b5ebfc1d0e62eb00ae210[…] Log rev3f6813a3b9f081bb486975dfe5b6a4[…] Log rev2f667b6e8847d20c67968c2b2d8a85[…] Log rev1$$ git log --pretty=format:"%h - %an : %s"579001a - David Parsons : Add file baze82a778 - David Parsons : Log rev3f6813a3 - David Parsons : Log rev2f667b6e - David Parsons : Log rev1$
Logrev1
Logrev2
Logrev3
Addfilebazmaster
DAVID PARSONS - INTRODUCTION TO GIT 30
$ # Add content to foo$ echo "Added content" >> foo
Logrev1
Logrev2
Logrev3
Addfilebazmaster
Illustra=ons:edittrackedfile
DAVID PARSONS - INTRODUCTION TO GIT 31
Illustra=ons:edittrackedfile$ # Add content to foo$ echo "Added content" >> foo$
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsnotinindex
master
DAVID PARSONS - INTRODUCTION TO GIT 32
Illustra=ons:edittrackedfile$ # Add content to foo$ echo "Added content" >> foo$$ git statusOn branch masterChanges 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/or "git commit -a")$
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsnotinindex
master
DAVID PARSONS - INTRODUCTION TO GIT 33
Illustra=ons:stage$ # Mark changes in foo as ‘to be committed’$ git stage foo
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsnotinindex
master
DAVID PARSONS - INTRODUCTION TO GIT 34
Illustra=ons: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 statusOn branch masterChanges to be committed: (use "git reset HEAD <file>..." to unstage)
modified: foo
$
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsinindex
master
DAVID PARSONS - INTRODUCTION TO GIT 35
IndexandWorkingCopy$ # Add content to bar$ echo "Content added to bar" >> bar
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsinindex
master
DAVID PARSONS - INTRODUCTION TO GIT 36
$ # Add content to bar$ echo "Content added to bar" >> bar$
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsinindex
Modifsnotinindex
master
IndexandWorkingCopy
DAVID PARSONS - INTRODUCTION TO GIT 37
$ # Add content to bar$ echo "Content added to bar" >> bar$ git statusOn branch masterChanges 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
$
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsinindex
Modifsnotinindex
master
IndexandWorkingCopy
DAVID PARSONS - INTRODUCTION TO GIT 38
Illustra=ons:diff
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsinindex
Modifsnotinindex
git diff
git diff --staged
git diff HEAD
master
DAVID PARSONS - INTRODUCTION TO GIT 39
$ git statusOn branch masterChanges 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
$
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsinindex
Modifsnotinindex
master
IndexandWorkingCopy
DAVID PARSONS - INTRODUCTION TO GIT 40
$ git statusOn branch masterChanges 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
$ # Add more content to foo$ echo “more content” >> foo
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsinindex
Modifsnotinindex
master
IndexandWorkingCopy
DAVID PARSONS - INTRODUCTION TO GIT 41
$ git statusOn branch masterChanges 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
$
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsinindex
Modifsnotinindex
master
IndexandWorkingCopy
DAVID PARSONS - INTRODUCTION TO GIT 42
Bypassingtheindex$ # Commit working copy state of foo$ git commit foo
Logrev1
Logrev2
Logrev3
Addfilebaz
Modifsinindex
Modifsnotinindex
master
DAVID PARSONS - INTRODUCTION TO GIT 43
$ # Commit working copy state of foo$ git commit foo[master 5e60acc] Add content to foo 1 file changed, 2 insertions(+)$
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Modifsnotinindex
Addfilebaz
master
Bypassingtheindex
DAVID PARSONS - INTRODUCTION TO GIT 44
$ # Commit working copy state of foo$ git commit foo[master 5e60acc] Add content to foo 1 file changed, 2 insertions(+)$$ git statusOn branch masterChanges 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
$
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Modifsnotinindex
Addfilebaz
master
Bypassingtheindex
DAVID PARSONS - INTRODUCTION TO GIT 45
$ # Commit working copy state of all tracked files$ git commit -a -m "Add content to bar”
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Modifsnotinindex
Addfilebaz
master
Bypassingtheindex
DAVID PARSONS - INTRODUCTION TO GIT 46
$ # 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 statusOn branch masternothing to commit, working directory clean$
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarmaster
Bypassingtheindex
DAVID PARSONS - INTRODUCTION TO GIT 47
Configuringgit
# Configure your name and e-mail address (almost mandatory)# If you don’t, you’ll get:$ git commit
*** Please tell me who you are.
Run
git config --global user.email "[email protected]" git config --global user.name "Your Name"
to set your account's default identity.Omit --global to set the identity only in this repository.
# Configure your name and e-mail address$ git config --global user.name "David Parsons"$ git config --global user.email [email protected]
DAVID PARSONS - INTRODUCTION TO GIT 48
Configuringgit
$ # 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.s 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”
DAVID PARSONS - INTRODUCTION TO GIT 49 DAVID PARSONS - INTRODUCTION TO GIT
INTERACTING WITH A REMOTE Scenario
5
DAVID PARSONS - INTRODUCTION TO GIT 50
Workinginparallel
clone clone
master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 51
masterr/o/master
Workinginparallel
master masterr/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 52
Workinginparallel
master
master master
r/o/master r/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 53
Workinginparallel
master
master master
push
r/o/master
Centralrepo
r/o/master
DAVID PARSONS - INTRODUCTION TO GIT 54
Workinginparallel
master mastermasterr/o/master
r/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 55
Workinginparallel
master mastermasterr/o/master
push
r/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 56
Workinginparallel
master mastermasterr/o/master
push
r/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 57
Workinginparallel
master mastermasterr/o/master
fetch
r/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 58
Workinginparallel
master mastermasterr/o/master r/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 59
Workinginparallel
master masterr/o/master
master
r/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 60
Workinginparallel
master masterr/o/master
master
push
r/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 61
Workinginparallel
masterr/o/master
mastermaster r/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 62
Workinginparallel
masterr/o/master
mastermaster
pull
r/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 63
Workinginparallel
mastermasterr/o/master master r/o/master
Centralrepo
DAVID PARSONS - INTRODUCTION TO GIT 64 DAVID PARSONS - INTRODUCTION TO GIT
INTERACTING WITH A REMOTE Corresponding Commands
6
DAVID PARSONS - INTRODUCTION TO GIT 65
Cloningarepository$ # Clone a remote repository$ # git clone <remote-repo-url> [local-name]$
DAVID PARSONS - INTRODUCTION TO GIT 66
Cloningarepository$ # Clone a remote repository$ # git clone <remote-repo-url> [local-name]$ git clone […]/collab.gitCloning into ‘collab'...done.$
DAVID PARSONS - INTRODUCTION TO GIT 67
Cloningarepository$ # Clone a remote repository$ # git clone <remote-repo-url> [local-name]$ git clone […]/collab.gitCloning into ‘collab'...done.$ cd collab # Don’t forget to cd into the local repo dir$
Addcontenttofoo
Addfilebaz
master Addcontenttobarv0.1 remotes/origin/master
DAVID PARSONS - INTRODUCTION TO GIT 68
$ git clone […]/collab.gitCloning into ‘collab'...done.$ cd collab # Don’t forget to cd into the local repo dir$
Otheruser’sviewpoint
Addcontenttofoo
Addfilebaz
master Addcontenttobarv0.1 remotes/origin/master
DAVID PARSONS - INTRODUCTION TO GIT 69
Remotes$ # Clone a remote repository$ # git clone <remote-repo-url> [local-name]$ git clone […]/collab.gitCloning into ‘collab'...done.$ cd collab # Don’t forget to cd into the local repo dir$ git remoteorigin$ git remote –vorigin […]/collab.git (fetch)origin […]/collab.git (push)$
Addcontenttofoo
Addfilebaz
master Addcontenttobarv0.1 remotes/origin/master
DAVID PARSONS - INTRODUCTION TO GIT 70
Remotetrackingbranch
Addcontenttofoo
Addfilebaz
master Addcontenttobarv0.1 remotes/origin/master
$ # Commit stuff$ echo "This is the content of bar" > bar$
Modifsnotinindex
DAVID PARSONS - INTRODUCTION TO GIT 71
Remotetrackingbranch$ # Commit stuff$ echo "This is the content of bar" > bar$ git commit bar -m "Modify bar"$
Addcontenttobarremotes/origin/master
master Modifybar
Addcontenttofoo
Addfilebaz
v0.1
DAVID PARSONS - INTRODUCTION TO GIT 72
Push$ # Push your commits onto the remote repository$ git push
Addcontenttobarremotes/origin/master
master Modifybar
Addcontenttofoo
Addfilebaz
v0.1
DAVID PARSONS - INTRODUCTION TO GIT 73
Push$ # Push your commits onto the remote repository$ git pushCounting 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 […]/collab.git 119dfba..a8f9783 master -> master$
Addcontenttobar
master Modifybarremotes/origin/master
Addcontenttofoo
Addfilebaz
v0.1
DAVID PARSONS - INTRODUCTION TO GIT 74
$
Otheruser’sviewpoint
Addcontenttofoo
Addfilebaz
master Addcontenttobarv0.1 remotes/origin/master
DAVID PARSONS - INTRODUCTION TO GIT 75
$ # Commit stuff$ echo "This is foo’s new content" > foo$ git commit foo -m "Change foo"$
Addcontenttobarremotes/origin/master
master Changefoo
Otheruser’sviewpoint
Addcontenttofoo
Addfilebaz
v0.1
DAVID PARSONS - INTRODUCTION TO GIT 76
$ git pushTo […]/collab.git ! [rejected] master -> master (non-fast-forward)error: failed to push some refs to ‘[…]/collab.git’hint: Updates were rejected because the remote contains work that youhint: do not have locally. This is usually caused by anotherhint: repository pushing to the same ref. You may want to firsthint: integrate the remote changes (e.g., 'git pull ...') before hint: pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.$
Failedpush
Addcontenttobarremotes/origin/master
master Changefoo
Addcontenttofoo
Addfilebaz
v0.1
DAVID PARSONS - INTRODUCTION TO GIT 77
$ git fetch
Fetch
Addcontenttobarremotes/origin/master
master Changefoo
Addcontenttofoo
Addfilebaz
v0.1
DAVID PARSONS - INTRODUCTION TO GIT 78
$ git fetchFrom […]/collab.git 119dfba..a8f9783 master -> origin/master$
Addcontenttobarv0.1
Modifybar
Changefoomaster
remotes/origin/master
Fetch
Addcontenttofoo
Addfilebaz
DAVID PARSONS - INTRODUCTION TO GIT 79
$ git merge
Merge
Addcontenttobarv0.1
Modifybar
Changefoomaster
remotes/origin/master
Addcontenttofoo
Addfilebaz
Mergingconsistsininjec=ngtheaccumulatedchangesetofr/o/masterintomaster
Amergeisembodiedbyamergecommitthathastwoparentsandthatincludesthechangesfrombothsides
Thebranchbeingmergedisuntouched
DAVID PARSONS - INTRODUCTION TO GIT 80
$ git mergeMerge made by the 'recursive' strategy. bar | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 bar$
Changefoo
Modifybar
Mergeremote-trackingbranch'origin/master'master
remotes/origin/master
Mergewithoutconflicts
Addcontenttobarv0.1
Addcontenttofoo
Addfilebaz
Whentherearenoconflictsbetweenthetwochangesets,gitmergeautoma=callytriggersthecrea=onofthemergecommit.Itwillopenyourcoreeditorwithapredefinedlogmsg
DAVID PARSONS - INTRODUCTION TO GIT 81
$ git push$
Mergeremote-trackingbranch'origin/master'master remotes/origin/master
Push
Changefoo
Modifybar
Addcontenttobarv0.1
Addcontenttofoo
Addfilebaz
DAVID PARSONS - INTRODUCTION TO GIT 82
Pull$ git pull
Addcontenttobarremotes/origin/master
master Modifybar
Addcontenttofoo
Addfilebaz
v0.1
DAVID PARSONS - INTRODUCTION TO GIT 83
Pull$ git pull[…]$
Mergeremote-trackingbranch'origin/master'master remotes/origin/master
Changefoo
Modifybar
Addcontenttobarv0.1
Addcontenttofoo
Addfilebaz
DAVID PARSONS - INTRODUCTION TO GIT 84
Gitpushconfigura=on
$ # When pushing for the first time, you might get a warning message such as this one:
warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
$ # If you do, choose the new behaviour, which is safest:$ git config --global push.default simple
DAVID PARSONS - INTRODUCTION TO GIT 85 DAVID PARSONS - INTRODUCTION TO GIT
MANAGING CONFLICTS 7
DAVID PARSONS - INTRODUCTION TO GIT 86
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarv0.1
Modifybar
Changebar
$ git merge
master
Managingconflicts
r/o/master
DAVID PARSONS - INTRODUCTION TO GIT 87
$ git mergeAuto-merging barCONFLICT (content): Merge conflict in barAutomatic merge failed; fix conflicts and then commit the result.$
Managingconflicts
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarv0.1
Modifybar
Changebarmaster
r/o/master
DAVID PARSONS - INTRODUCTION TO GIT 88
$ git mergeAuto-merging barCONFLICT (content): Merge conflict in barAutomatic merge failed; fix conflicts and then commit the result.$$ cat barThis is line 1This is line 2This is line 3<<<<<<< HEADThis is the fourth line=======This is line number 4>>>>>>> featureAThis is line 5This is line 6This is line 7This is line 8$
Managingconflicts
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarv0.1
Modifybar
Changebarmaster
r/o/master
DAVID PARSONS - INTRODUCTION TO GIT 89
$ # You can edit the conflicting files directly and then stage them and commit, or you can use a merge tool$$ git mergetool
Managingconflicts
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarv0.1
Modifybar
Changebarmaster
r/o/master
DAVID PARSONS - INTRODUCTION TO GIT 90
mergetool–p4merge
DAVID PARSONS - INTRODUCTION TO GIT 91
$ git config --global merge.tool meld$ git config --global mergetool.meld.cmd 'meld $LOCAL $MERGED $REMOTE'$ git config --global mergetool.meld.trustExitCode false$
Configuringamergetool
DAVID PARSONS - INTRODUCTION TO GIT 92 DAVID PARSONS - INTRODUCTION TO GIT
Branches 8
DAVID PARSONS - INTRODUCTION TO GIT 93
Whatisabranch?
branch
Eachbranchcontainsanalterna=veversionofthesamesetoffilesGitismeantforanextensiveuseofbranches«master»isthemainbranch,i.e.thatwhichischeckedoutbydefault
master
DAVID PARSONS - INTRODUCTION TO GIT 94
Branchtypes
branch
Gitdoesnotknowaboutbranchtypes,itistheusageyoumakeofabranchthatdeterminesits“type”
Abranchmayremainlocaltoauser’srepoorbepublished(pushed)tothecentralrepo
Commonusecasesforbranchesinclude:• Feature• Bugfix• LTS• …
master
DAVID PARSONS - INTRODUCTION TO GIT 95
FeatureandBugfixbranches
featureA
ThesebranchesareusefulfordevelopingafeatureorapatchwithoutmessingwiththemasterTheyareusuallymeanttobemergedbackintothemasterwhenthefeatureorpatchisreadyTheworkflowisyours(yourteam’s)todefine
master
DAVID PARSONS - INTRODUCTION TO GIT 96
Mergingbranches
featureA
HerewehavemergedbranchfeatureAintobranchmaster.ThismeanswehaveinjectedtheaccumulatedchangesetoffeatureAintomasterMergingalocalbranchisjustthesameasmergingaremote-trackingbranchThismergeisembodiedbyacommit(unlessitisafast-forward)thathastwoparentsandthatincludethechangesmadetobothbranches(here5changesets)BranchfeatureAisuntouched
master
DAVID PARSONS - INTRODUCTION TO GIT 97
Maintenancebranches
v1.1.2 Branchescanbeusedforintegra=ngbugfixesinearliermaintainedreleases(e.g.LTS)Thesebranches arenotmeant tobemerged into themaster
v1.1.0
v1.1.1
master
v1.2.0
DAVID PARSONS - INTRODUCTION TO GIT 98
WarningforSVNusers
• Atany=me,yourworkingcopycontainsonlythecontentofonesinglebranch
DAVID PARSONS - INTRODUCTION TO GIT 99
Branches$ git branch* master$ git branch featureA
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarmasterv0.1
DAVID PARSONS - INTRODUCTION TO GIT 100
Branches$ git branch* master$ git branch featureA$
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
featureA Addcontenttobarv0.1 master
DAVID PARSONS - INTRODUCTION TO GIT 101
Branches$ git branch* master$ git branch featureA$ git branch featureA* master$
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
featureA Addcontenttobarv0.1 master
DAVID PARSONS - INTRODUCTION TO GIT 102
Branches$ git branch* master$ git branch featureA$ git branch featureA* master$ git checkout featureA$ git branch* featureA master$
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
featureA Addcontenttobarv0.1 master
Youcanalsocreateandcheckoutanewbranchatoncewithgit checkout –b newBranch
DAVID PARSONS - INTRODUCTION TO GIT 103
Branches$ # Commit stuff in branch featureA$ echo “Blabla” >> foo$ git commit foo -m “Modify foo”
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
featureA Addcontenttobarv0.1 master
Modifsnotinindex
DAVID PARSONS - INTRODUCTION TO GIT 104
Branches$ # Commit stuff in branch featureA$ echo “Blabla” >> foo$ git commit foo -m “Modify foo”$
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Add[…]barv0.1
ModifyfoofeatureA
master
DAVID PARSONS - INTRODUCTION TO GIT 105
Branches$ # Checkout branch master and commit stuff in it$ git checkout master$
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Add[…]barv0.1
ModifyfoofeatureA
master
DAVID PARSONS - INTRODUCTION TO GIT 106
Branches
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarv0.1
ModifyfoofeatureA
$ # Checkout branch master and commit stuff in it$ git checkout master$ echo “Blabla” >> bar$ git commit foo -m “Modify bar”$
Modifybarmaster
DAVID PARSONS - INTRODUCTION TO GIT 107
Mergingbranches
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarv0.1
ModifyfoofeatureA
Modifybarmaster
$ git merge featureA
DAVID PARSONS - INTRODUCTION TO GIT 108
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarv0.1
Modifybar
ModifyfoofeatureA
Mergebranch‘featureA’master
Mergingalocalbranchisjustthesameasmergingaremote-trackingbranch
$ git merge featureA$
Mergingbranches
DAVID PARSONS - INTRODUCTION TO GIT 109 DAVID PARSONS - INTRODUCTION TO GIT
Tags 9
DAVID PARSONS - INTRODUCTION TO GIT 110
Tags
$ # Create an annotated tag named "v0.1"$ git tag –a v0.1 –m “version 0.1”
$ # List tags$ git tagv0.1$
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarv0.1 master
Atagistypicallyusedtomarkareleasepoint.Thereare2typesoftags:• Lightweight(justareferencetoaspecificcommit)• Annotated(fullobject,containsaddi=onalinfo,canbesigned)
DAVID PARSONS - INTRODUCTION TO GIT 111
Tags
Tagshavetobepushedandfetchedmanuallygit push origin <tagname>orgit push origin –tags
Git fetch origin --tags
DAVID PARSONS - INTRODUCTION TO GIT 112 DAVID PARSONS - INTRODUCTION TO GIT
Tree-ish ? 10
DAVID PARSONS - INTRODUCTION TO GIT 113
Tree-ish:aconceptyoushouldknowof
Acommitcanbeiden=fiedinmul=pleways,e.g.:• ItsSHA1(possiblyabbreviated)• Abranchname• Atag• Areference(e.g.HEAD,…)• Anindirectreference:HEAD^,HEAD~,master~3,…• …
DAVID PARSONS - INTRODUCTION TO GIT 114
Indirectreferences
Acommitcanbeiden=fiedindirectlyusingabasecommitfollowedbyanycombina=onofthe^and~operators:• C^n:breadthfirstoperator– getsthenthparentofcommitC• C~n:depthfirstoperator– getstheancestorofcommitC,n
genera=onsback,alwaysfavouringthefirstparent• N==1bydefault
• D==B^1==B^==B~1==B~• D==A~2==A~1~1==A^^…• C==A^2
A
B
D
C
DAVID PARSONS - INTRODUCTION TO GIT 115 DAVID PARSONS - INTRODUCTION TO GIT
Merge or Rebase ? 11
DAVID PARSONS - INTRODUCTION TO GIT 116
Rebase$ git rebase
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarv0.1
Modifybar
Changebarmaster
remotes/origin/master
DAVID PARSONS - INTRODUCTION TO GIT 117
Rebase$ git rebase$
Logrev1
Logrev2
Logrev3
Addcontenttofoo
Addfilebaz
Addcontenttobarv0.1
Changebar
Changebarmaster remotes/origin/master
Modifybar
ThiswasyourHEADjustbeforetherebase
Thisisabrandnewcommit
DAVID PARSONS - INTRODUCTION TO GIT 118
• Rebasingbasicallymeansre-wri=ngthehistoryofwhathappened.Thisisaverypowerfulfeature,inpar=cularintheinterac=vemode(rebase -i).Butasalways,greatpowercomeswithgreatresponsibility
DONOTREBASECOMMITSTHATEXISTOUTSIDEYOURREPOSITORY
Rebaseonlylocalcommits
DAVID PARSONS - INTRODUCTION TO GIT 119
Whichonewouldyouprefer?
Merge Rebase
Mergeorrebase?
DAVID PARSONS - INTRODUCTION TO GIT 120
Mergeorrebase?
Ormaybethismixedone?
Merge RebaseMixed
DAVID PARSONS - INTRODUCTION TO GIT 121 DAVID PARSONS - INTRODUCTION TO GIT
To go further… 12
DAVID PARSONS - INTRODUCTION TO GIT 122
• Addi=onalcommandsorop=ons• commit --amend• stash / stash pop• rebase –i• add –p• cherry-pick• bisect• revert• Andsomanymore…
• git-awtude.fr
Togofurther…
LIEULOCALISATION
www.inria.frAntenneINRIALyonlaDoua
www.inria.fr
Thank you