revisioncontrol advanced git - github pages · 03/03/2015  · .git folder repositorypopulated...

35
Revision control Advanced git Waterford Institute of Technology April 30, 2016 John Fitzgerald Waterford Institute of Technology, Revision controlAdvanced git 1/35

Upload: others

Post on 15-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Revision controlAdvanced git

Waterford Institute of Technology

April 30, 2016

John Fitzgerald

Waterford Institute of Technology, Revision controlAdvanced git 1/35

Page 2: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Presentation outline

Estimated duration presentationQuestions at end presentationTopics discussed:

• Git internals brief exploration• Merging• Rebasing• Resolving conflicts• Squash commits

Waterford Institute of Technology, Revision controlAdvanced git 2/35

Page 3: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Sample projectFile structure

Waterford Institute of Technology, Revision controlAdvanced git 3/35

Page 4: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Sample projectGit structure

Waterford Institute of Technology, Revision controlAdvanced git 4/35

Page 5: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Sample projectFiles, folders & Git structure

Waterford Institute of Technology, Revision controlAdvanced git 5/35

Page 6: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Git objectsTree, Blob, Commit, Tag

Git has 4 objects:• Tree: equivalent to file or folder• Blob: stores data• Commit: describes the commit• Tag (light & annotated): describes the tag

Waterford Institute of Technology, Revision controlAdvanced git 6/35

Page 7: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Git objectsHow content is stored

Git stores content in Tree & Blob objects• Tree

• Faclitates storage group files• Similar to folder or directory

• Blob• Corresponds to file contents• May include trees (subfolders)

Waterford Institute of Technology, Revision controlAdvanced git 7/35

Page 8: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Git objectsCommit

Waterford Institute of Technology, Revision controlAdvanced git 8/35

Page 9: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Git objectsAnnotated tag

Waterford Institute of Technology, Revision controlAdvanced git 9/35

Page 10: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Sample projectGit init

Initialize empty .git repository

git init

Waterford Institute of Technology, Revision controlAdvanced git 10/35

Page 11: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Sample projectGit init

Uninititlized .git contentls −f1 .git

Waterford Institute of Technology, Revision controlAdvanced git 11/35

Page 12: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

.git folderRepository populated

Additional files and folders in .git whencontent committedCore git components:HEAD: Points to checked out branchindex: Staging area storagerefs: Commit object pointersobjects: Stores content

• tree, blob, commit, tag

Waterford Institute of Technology, Revision controlAdvanced git 12/35

Page 13: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

HEADPoints to checked out branch

git checkout mastercat .git/HEADref: refs/heads/master

git checkout −b devSwitched to new branch devcat .git/HEADref: refs/heads/dev

Waterford Institute of Technology, Revision controlAdvanced git 13/35

Page 14: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

indexREADME: not tracked, not staged file

Waterford Institute of Technology, Revision controlAdvanced git 14/35

Page 15: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

indexREADME: a staged file

Waterford Institute of Technology, Revision controlAdvanced git 15/35

Page 16: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

refsPointers to commit objects

Examples refs values:• remotes: origin• heads: master• tags: dev-branch

Waterford Institute of Technology, Revision controlAdvanced git 16/35

Page 17: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

objectsData storage

find .git/objects −type f

Waterford Institute of Technology, Revision controlAdvanced git 17/35

Page 18: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

objectsData stored as key-value pair

Waterford Institute of Technology, Revision controlAdvanced git 18/35

Page 19: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

objectsData stored as key-value pair

• SHA-1 function generates key.• SHA-1 (Secure Hash Algorithm)

• Generates 160-bit, 20-byte, 40-character value• Pretty log printing abbreviates hash

Waterford Institute of Technology, Revision controlAdvanced git 19/35

Page 20: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Local Git workflowWorking tree, staging area, git repository

Waterford Institute of Technology, Revision controlAdvanced git 20/35

Page 21: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Git deployed remote serversTrack local repo on one or more cloud servers

Waterford Institute of Technology, Revision controlAdvanced git 21/35

Page 22: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Git deployed remote serversDevelopment team track local repo on one or more cloud servers

Waterford Institute of Technology, Revision controlAdvanced git 22/35

Page 23: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Git reviewSome commands encountered in Basic Git presentation and labs

git initgit add READMEgit commit −m 'added README'git remote add origin https://github.com/yourdomain/repo.gitgit pushgit pullgit statusgit branch developmentgit checkout developmentgit tag −a dev0

Waterford Institute of Technology, Revision controlAdvanced git 23/35

Page 24: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

git rebaseMove branch to new base commit resulting in linear history

Waterford Institute of Technology, Revision controlAdvanced git 24/35

Page 25: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

git mergeIntegrate branch using merge resulting in master tip having two parent commits

Waterford Institute of Technology, Revision controlAdvanced git 25/35

Page 26: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Rewrite historyEdit last commit message

Replace last commit message:

git commit −−amend −i 'new commit message'

Waterford Institute of Technology, Revision controlAdvanced git 26/35

Page 27: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Rewrite historyInteractive rebase to squash commits

git rebase −i HEAD~4

pick ffe9d3a commit 2squash db70a19 commit 3squash 1c63d2e commit 4squash a261ef4 commit 5

Waterford Institute of Technology, Revision controlAdvanced git 27/35

Page 28: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Rewrite historyExample: interactive rebase or amend last commit

HEALTH WARNING:Do not rewrite history of commits already pushed to remote cloudrepository (public repo).

Waterford Institute of Technology, Revision controlAdvanced git 28/35

Page 29: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

TeamResolving conflicts

Consider the following:• Two-developer team (A & B)• A & B have local repos• Shared cloud repo• Three modules in app

• module-A (developer A only)• module-B (developer B only)• module-Shared (A & B joint work)

Waterford Institute of Technology, Revision controlAdvanced git 29/35

Page 30: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

TeamResolving conflicts

Possible scenario• No conflict possible modules A & B• A & B independently modify shared module:

• A pushes module: should succeed• B attempts push: this will fail

• solution: pull• fix conflicted files• save & merge

Waterford Institute of Technology, Revision controlAdvanced git 30/35

Page 31: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Merge conflictsHow to resolve

Waterford Institute of Technology, Revision controlAdvanced git 31/35

Page 32: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Summary

• Four Git objects• Tree• Blob• Commit• Tag

• Relationship between project file structure and .git objects• Directory - Tree• File - Blob

• Git core components• HEAD• index• objects• refs

Waterford Institute of Technology, Revision controlAdvanced git 32/35

Page 33: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Summary

• Git: content-addressable system• Data stored as key-value pair• Key is 40 character SHA-1 hash value

• Git workflow• Working tree• Staging area• Repository

• Review basic commands• init• add• commit• push• pull

Waterford Institute of Technology, Revision controlAdvanced git 33/35

Page 34: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Summary

• Rebase• Move branch to new base commit• Linear history results

• Merge• Not linear history• Two parent commits

• Resolve conflicts• Using vi(m) or other text editor

• Change commit history• Use interactive rebase

Waterford Institute of Technology, Revision controlAdvanced git 34/35

Page 35: Revisioncontrol Advanced git - GitHub Pages · 03/03/2015  · .git folder Repositorypopulated Additionalfilesandfoldersin.git when contentcommitted Coregitcomponents: HEAD:Pointstocheckedoutbranch

Referenced Material

1. Pro Git by Scott Chacon & Ben Straubhttps://git-scm.com/book/en/v2 [Accessed 2016-04-27]2. Atlassian Tutorial: Rewriting Historyhttps://www.atlassian.com/git/tutorials/rewriting-history/git-rebase [Accessed 2016-30-04]3. Git usage statisticshttps://www.wikivs.com/wiki/Git_vs_Subversion [Accessed2015-03-03]

Waterford Institute of Technology, Revision controlAdvanced git 35/35