Go with the Flow - pnwdrupalsummit.org · How to be a total git at Git git (British) : a foolish or...

66
PNWDS 2017 - nielsonm git for teams Go with the Flow

Transcript of Go with the Flow - pnwdrupalsummit.org · How to be a total git at Git git (British) : a foolish or...

PNWDS 2017 - nielsonm

git for teams

Go with the Flow

PNWDS 2017 - nielsonm

About Me

Mike Nielson

Nielsonm on Drupal.org

Sr. Web Developer at Red Hat

Working with Drupal since 2007

PNWDS 2017 - nielsonm

What we’re on aboutI. Introduction to version control

A. Dark ages

B. Medieval times

II. Introduction to gitA. Inception

B. DAG-yo

III. Why git and DrupalA. On D.O.

B. Everywhere else (pretty much)

PNWDS 2017 - nielsonm

IV. Beyond commit, pull, pushA. Managing the upstream

B. Leveling up the git game

V. How to be a total git at GitVI. git for teams

A. git flow

B. Wunderflow

C. Develop is the new master

D. Merge vs. rebase

VII. What’s next?VIII. Q & A

What we’re on about

PNWDS 2017 - nielsonm

Intro to version control(How we got here)

PNWDS 2017 - nielsonm

Pre-version control: the Dark Ages● Filename editing

○ index.php.bak, index.final.php○ Index-v2.php, index-v2-r1.php

● Pros:○ Human readable○ Single source of truth○ Only track applicable files

● Cons:○ Little to no ability to track changes○ Single Point Of Failure

PNWDS 2017 - nielsonm

VCS early days: CVS, SVN● Pros:

○ Single source of truth○ File-by-file revision tracking

● Cons:○ Branches were difficult to merge○ Scaled poorly

PNWDS 2017 - nielsonm

Introduction to git(Some math required)

PNWDS 2017 - nielsonm

Git jargonRepo: Repository - Box all the files & their changes

Index: Bucket of changes that are staged to be committed

Remote: Repository hosted on a separate machine

Diff: Output format to show changes

PNWDS 2017 - nielsonm

Git jargonLocal: Repository on local machine (where the real work gets done)

Commit: Formally add changes to the repository from the index

Push: Add local commits to the remote repository

Branch: Set of changes isolated from the main repo line

PNWDS 2017 - nielsonm

git historygit development began in April 2005

Linus Torvalds wanted a distributed SCM system that he could use like BitKeeper.

However, none of the free or OSS systems on the market met kernel team specifications, especially for performance.

PNWDS 2017 - nielsonm

git history (cont.)

For his design criteria, Linus specified three points:

1. Take Concurrent Versions System (CVS) as an example of what not to do;

2. Support a distributed, BitKeeper-like workflow3. Include very strong safeguards against corruption, either

accidental or malicious

PNWDS 2017 - nielsonm

git history (cont.)

Where’d the name git come from?

I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'.

--Linus Torvalds

PNWDS 2017 - nielsonm

git for math nerds

Git repos are at their heart a Directed Acyclic Graph or DAG

3 basic kinds of nodes (tree, blob, commit)● Blobs are the binary data ● Trees contain file metadata (name, parent directory, file

permissions)● Commits contain tree & blob state at time of commit as

well as commit parentage

-- http://eagain.net/articles/git-for-computer-scientists

PNWDS 2017 - nielsonm

End result:

git for math nerds

PNWDS 2017 - nielsonm

Why git & Drupal(Some math required)

PNWDS 2017 - nielsonm

Why git & Drupal

Drupal.org started out in CVS

Since Drupal predated git by nearly 4 years

There was an initiative!https://groups.drupal.org/node/48818

PNWDS 2017 - nielsonm

Why move to git & Drupal?

Moving D.O. off of CVS was the goal.

● It's an archaic system that no one in their right mind chooses CVS as a version control system today (2010)

● Things that should be really simple (submitting changes that add/remove files, renaming files, etc.) are absolutely horrendous in CVS.

PNWDS 2017 - nielsonm

Why move to git & Drupal?

● It's difficult for patch authors to have discipline to keep changes to one "context", resulting often-times in "mega-patches" that are impossible to review

● We lose the incremental commit history that happened on said "mega-patches."

PNWDS 2017 - nielsonm

Why move to git & Drupal?

● It's impossible with CVS to do much of anything while offline, because even a 'diff' operation needs to "phone home" to the parent server.

● Lack of modernization of our contribution tools are causing people to move to other places like Launchpad and GitHub, thus fragmenting both our community, and the strength of Drupal.org as the central hub to find any and all Drupal stuff.

https://www.lullabot.com/podcasts/drupalizeme-podcast/drupalorg-redesign-and-git-migration

PNWDS 2017 - nielsonm

Why git & Drupal rocks!

1. Highly distributed team2. Maintainability of multiple, long-running branches

a. Envision the chaos of needing to maintain a hybrid D7/8 module3. Ability to have highly siloed access for a legion of “teams”

a. cTools maintainers !== DrupalCommerce maintainers

PNWDS 2017 - nielsonm

Beyond commit, pull, push(Impress your manager)

PNWDS 2017 - nielsonm

git commands I can’t live without

Following are commands that revolutionized my personal usage of git

Something catch your eye? Use it!

PNWDS 2017 - nielsonm

git add

Add changes made to repo to the index

Everyone’s second command in working with git

-p FTW

PNWDS 2017 - nielsonm

git commit

????git commit --amend Amends already committed commits

E.g. You misspell the name of your module. 0.o

You forgot to add a new tpl file. x0

PNWDS 2017 - nielsonm

git checkout

Remove commits & (optionally) move to a new branch

git checkout -p -> git chp

DANGEROUS - no undo here

PNWDS 2017 - nielsonm

git status

Generic sit-rep

Not one, but two aliases in local dev env

PNWDS 2017 - nielsonm

git stash

Add changes to a local-only cache & checkout to HEAD

Temporary save state

Apply changes by git stash pop

PNWDS 2017 - nielsonm

git reset

Remove changes from index

--hard to get back to origin

See checkout for warnings.

PNWDS 2017 - nielsonm

git log

Show changes to the repo over time.

Wanna double check your repo is up to date?

DONE.

Pretty charts & commit messages!

PNWDS 2017 - nielsonm

Git clean

Remove up untracked files

-d to remove directories-i interactive mode-n dry run-f force

PNWDS 2017 - nielsonm

git diff

Show changes in diff mode

On ANYTHING, anything at all.

Double checking commits

Other people’s work, or between branches

PNWDS 2017 - nielsonm

git bisect

git bissect startgit bisect good <commit hash>git bisect bad

git bisect reset

PNWDS 2017 - nielsonm

Collect them all!

https://github.com/nielsonm/dotfiles/.gitconfig

PNWDS 2017 - nielsonm

How to be a total git at Git(But, not really)

PNWDS 2017 - nielsonm

How to be a total git at Git

git (British) : a foolish or worthless person

Git is incredibly powerful, which also makes it dangerous.

Since it’s also very flexible, there’s no one right way to do most things.

However, there are bad practices.

In other words, please please don’t do these things.

PNWDS 2017 - nielsonm

HTBATGAG - bad commit msgs

Source: https://xkcd.com/1296

PNWDS 2017 - nielsonm

HTBATGAG - one single commit

PNWDS 2017 - nielsonm

HTBATGAG - Editor history as version control

We all use an editor of some sort (PhpStorm, vim, VSCode, emacs, etc.)Editors *can* track history - but they● aren’t shareable● only exist during the editing session

PNWDS 2017 - nielsonm

HTBATGAG - Force push

PNWDS 2017 - nielsonm

HTBATGAG - Force push

http://hammad.ca/blog/2015/03/08/confirm-before-pushing-to-master-branch

PNWDS 2017 - nielsonm

git for teams(Impress your manager)

PNWDS 2017 - nielsonm

Version control for teamsWorking in parallel means working on branches

Isolation is the name of the game

Your branch, your work

Merging is the tricky bit

PNWDS 2017 - nielsonm

git flowA branching methodology, git flow was introduced by Vincent Driessen in 2010

Branching and merging as a central strategy.

Has a ton of upsides, few downsides

-- http://nvie.com/posts/a-successful-git-branching-model

PNWDS 2017 - nielsonm

git flow

PNWDS 2017 - nielsonm

WunderflowGit flow for Drupal

Built by WunderKraut

Breaks code into features

PNWDS 2017 - nielsonm

Wunderflow

PNWDS 2017 - nielsonm

Develop is the new Master

PNWDS 2017 - nielsonm

Merge vs. Rebase

PNWDS 2017 - nielsonm

Merge vs. rebaseScenario - Branch exists, has changes, then surprise - upstream changes exist - What to do?

Merge - Maintains all the history, hard to revert (2 parents), time dependent, debugging can get dicey with bad merges

Rebase - rewrites history on the branch, easier debugging (when done properly), cleaner branch history, merge hard mode

PNWDS 2017 - nielsonm

Merge vs. rebaseGolden rule of rebasing:

DON’T use rebase on a public/shared branch

Further reading:

https://www.atlassian.com/git/tutorials/merging-vs-rebasinghttps://www.derekgourlay.com/blog/git-when-to-merge-vs-when-to-rebasehttps://hackernoon.com/git-merge-vs-rebase-whats-the-diff-76413c117333

PNWDS 2017 - nielsonm

Rebase

PNWDS 2017 - nielsonm

Merge

PNWDS 2017 - nielsonm

ALL THE MERGE COMMITS!!1!

PNWDS 2017 - nielsonm

The central tenet of working with git

TMTOWDI

While on old branchgit branch -m <new branch>===git branch <new branch> <parent branch>git cherry-pick <commit hashes>git branch -D <old branch>

PNWDS 2017 - nielsonm

What’s next?

PNWDS 2017 - nielsonm

git in the futurePossible uses for git1. beyond idiot proof2. git for government 3. git for biotech4. git for images5. encrypted git

PNWDS 2017 - nielsonm

git accessible● Ease of setup

○ Importing history from an editor into commits○ Auto committing

■ Human & machine readable commit messages○ Error & exception handling

PNWDS 2017 - nielsonm

git accessible● Beyond idiot proofing - malfeasance proofing

○ Access control not inherently baked in○ Private branches○ Public pull - whitelist to push

--https://wincent.com/wiki/Git_repository_access_control

PNWDS 2017 - nielsonm

Git for government?!?!?

https://twitter.com/ElijahLynn/status/955853670272380928

PNWDS 2017 - nielsonm

git for everyone● git for images

○ Image filenames as revision markers○ Engineering challenges

■ VCS on a JPEG■ Vectors less so

● Chalkflow & Abstract (Sketch plugin)○ UI challenges & visual diff display

PNWDS 2017 - nielsonm

git for everyone (cont.)● git for biotech

○ Gene editing■ Editors need a VCS

○ Sharing gene sequences for recombination■ GitHub for DNA

PNWDS 2017 - nielsonm

git for privacy● Encrypted git - OMGWTFBBQ?!?● Editing a novel co-written by Cory Doctorow & Neil

Gaiman● Sensitive internal business documents● Health or legal documents● Private financial documents● Tax documents

--https://keybase.io/blog/encrypted-git-for-everyone

PNWDS 2017 - nielsonm

Resources

https://www.drupal.org/documentation/git https://github.com/k88hudson/git-flight-ruleshttps://git-scm.com/book/en/v2http://wunderflow.wunder.iohttp://ohshitgit.comhttps://www.atlassian.com/git/tutorials

PNWDS 2017 - nielsonm

Questions?

PNWDS 2017 - nielsonm

Thanks!!

about.me/nielsonm

linkedin.com/in/nielsonm