Slide 1

Slide 1 text

EFFECTIVE GIT - TEJAS BUBANE @tejasbubane

Slide 2

Slide 2 text

CONTENTS ▸ Git Philosophy ▸ Log tree ▸ Commits and Branches ▸ Rewriting History ▸ Merge vs Rebase vs Squash vs Cherry-pick ▸ Workflow ▸ Good Practices

Slide 3

Slide 3 text

THE GIT PHILOSOPHY ▸ Created by Linus Torvalds (after conflicts with Bitkeeper) ▸ For use in Linux kernel development ▸ Actually the Linux Philosophy ▸ Uses concepts from POSIX filesystem ▸ Stores snapshots not diffs ▸ All data stored in .git directory in delta compressed form ▸ Precise commands ▸ "Git" means an idiot (British Slang)

Slide 4

Slide 4 text

DISTRIBUTED! ▸ Everyone has local copy of entire history ▸ Can work entirely independently of others ▸ Even without internet ▸ Remote - URL to push the code to ▸ clone brings the project, creates origin and master

Slide 5

Slide 5 text

COMMIT OBJECTS ▸ One logical change in the codebase ▸ Message + Author (name, email) + Timestamp + Snapshots of all files + Parent(s) reference = checksum ▸ Everything is checksummed internally, each version of each file has checksum ▸ Checksum - SHA-1, Unique, 40 character hex (20 bytes)

Slide 6

Slide 6 text

GIT HISTORY ▸ Commits have links with parent(s) ▸ Form a tree structure - Log tree - HISTORY ▸ Branches are nothing more than pointers to commit objects ▸ Pointers change - refer to different object ▸ Remote and local branches ▸ HEAD - symbolic pointer pointing to current branch

Slide 7

Slide 7 text

MERGE ▸ git merge feature-branch ▸ Fast Forward ▸ Both commits are inline ▸ Just move the pointer to the next commit ▸ Non-fast-forward ▸ Commits not-inline ▸ Create a new snapshot and a merge commit ▸ Looks at nearest common parent to create new snapshot

Slide 8

Slide 8 text

REWRITING HISTORY ▸ Ways to rewrite: ▸ Rebase ▸ Cherry Pick ▸ Squash ▸ Amend ▸ Time Machine ▸ Perils of Rebasing

Slide 9

Slide 9 text

REBASE ▸ Apply each commit from current branch on top of the target branch ▸ Example: ▸ git checkout feature-branch ▸ git rebase master ▸ Conflicts might arise in application of each commit

Slide 10

Slide 10 text

CHERRY PICK ▸ Example ▸ git cherry-pick eh1629ab1 ▸ git cherry-pick feature-branch ▸ Take one commit and apply on top of some branch

Slide 11

Slide 11 text

SQUASH ▸ Example: ▸ git rebase 1na9n5m ▸ git rebase HEAD~3 ▸ Then later choose which commits need to be squashed ▸ Remove unnecessary commits - Clean History

Slide 12

Slide 12 text

AMEND COMMIT ▸ Change commit snapshot ▸ Change contents ▸ Change commit message ▸ Usually the latest commit ▸ Example: ▸ Can add files before committing ▸ git commit —amend ▸ amends the latest commit ▸ git rebase HEAD~3 ▸ then select to edit a commit - amends previous commit

Slide 13

Slide 13 text

TIME MACHINE! ▸ Go back in history ▸ Change history! ▸ Changing history has challenges ▸ DO NOT change shared history ▸ DO NOT change commits on branches that others have based their work on ▸ Any of these operations changes the commit and all commits based on that: ▸ rebase, cherry-pick, squash, amend

Slide 14

Slide 14 text

WORKFLOW ▸ Merge Requests ▸ CI status ▸ Code review ▸ Merge the Request ▸ Close issues via PR ▸ Deploy branches ▸ Specifics vary from project to project

Slide 15

Slide 15 text

GOOD PRACTICES ▸ Each commit should be a logical change entity ▸ Write good commit messages ▸ Imperative mood in the subject line ▸ Fix the flash message on password change ▸ Fixed password change ▸ Details in the message ▸ Use rebase instead of merge on your own PR ▸ Unless no-one else is working on the same branch ▸ Branch names with hiphens

Slide 16

Slide 16 text

REFERENCES ▸ ProGit Book: https://git-scm.com/book/en/v2 ▸ Good commit messages: http://chris.beams.io/posts/git- commit/