USING FOR AUTOMATION AND COLLABORATION

JUSTIN ELLIOTT - MATT HANSEN PENN STATE UNIVERSITY AGENDA

overview

• Introduction and basics of Git

• Advanced Git features

• Collaboration

• Automation VERSION CONTROL WHAT IS IT?

• Manages changes and versions of files

• Also known as:

• Source Code Management (SCM)

• Version Control System (VCS)

• GitHub is a hosting service that uses Git WHY USE IT?

• Reduces risk and enables you to experiment with code

• Agility

• Flexible workflows

• Quicker deployments

• Automate updates WHY USE IT?

• Helps with documentation

• Collaboration with others

• Contributing to open source projects

• Modifying a project for your own use GIT INTRODUCTION WHY GIT?

• The current de facto standard for source code management

• Easy to learn, quick to start!

• Very powerful and extensive GIT OVERVIEW

• Extremely fast

• Excellent data integrity

• Git checksums everything (SHA-1) GIT OVERVIEW

• Distributed

• All clients have the full repo

• Backups are everywhere

• Local repo updated before remotes

• When available, push updates to remote GIT TERMINOLOGY

• Repository

• Local

• Remote

• Working Directory

• Commit GIT INSTALLATION & CONFIGURATION HOW TO INSTALL GIT

• Embedded with many Git GUI Apps

• Included with XCode

• Download from http://git-scm.com

• OS X: Terminal.app

• Windows: Git Bash

• Linux: Terminal

• OS X: MacPorts and HomeBrew GIT CLIENT APPS

• http://git-scm.com/downloads/guis

• GitHub Desktop

• https://desktop.github.com

• SourceTree

• https://www.sourcetreeapp.com REVIEWING DIFFS

• Very easy to view with GitHub Desktop and Web

• External diff apps

• FileMerge

• Kaleidoscope GITHUB DESKTOP

• Simplifies the complex Git tasks

• Integrates well with GitHub hosting service

• Easy to start with

• Limited feature set (by design) DEMO: GITHUB CONFIG CONFIGURING GIT ON THE CLI

• Git GUI clients handle it for you, or for the CLI fans …

• $ git config

• Contains items such as name, email, editor, diff tool

• Critical for collaboration DEMO: GIT CONFIG CLI

https://asciinema.org/a/anjsg2tmlwul6j3ahizgl98ie FUNDAMENTALS REPO FILE CLASSIFICATIONS

• Tracked

• Ignored

• Untracked TRACKED FILE STATES

• Modified

• Staged

• GitHub Desktop shows as checkboxes

• Committed CREATE A REPO

• Create new empty project repository

Working Directory ADD FILES TO THE REPO

• Add (stage) files for the next commit

Working Directory add Staging Area COMMIT FILES TO REPO

• Save staged files to the project repository

Working Directory add Staging Area commit Repository COMMITS

• Saves file changes to the repo

• Each commit should encapsulate a single fix*

• Commit often - You will thank yourself later!

• Or else … Larger changes are much harder to pick apart and revert back to. IGNORING FILES / DIRECTORIES

• Why?

• What?

• How?

• Scope

• Single Project Repo

• All Repos ( Global ~/.gitignore_global file )

• Recommended to create ignore file as first step COMMIT HISTORY LOGS

• Review history of commits to repo

• Documents changes over time

• Extremely helpful for your future self and others DEMO

• Create repo • Modify same tracked file

• Create files • View diff of file changes

• Show status • Commit the file

• Ignore file(s) • Review history log

• Commit git ignore file • Revert to previous commit

• Initial commit of file • Review history log

BRANCHES BRANCHES

• Provide separate paths of code development

• When should they be used?

• Others to help test before merged to master

• “master” is the default branch

• Great for vetting major changes, for others to review

• Many workflow methods out there (ie, GitFlow) BRANCH WALK THROUGH

First Commit

M1 BRANCH WALK THROUGH

M1

B1

New Branch BRANCH WALK THROUGH

M1

B1 B2 Second Branch Commit BRANCH WALK THROUGH

M1

B1 B2 B3 Third Branch Commit BRANCH WALK THROUGH

M1

B1 B2 B3 B4 Fourth Branch Commit BRANCH WALK THROUGH

Merge Branch To Master

M1 M2

B1 B2 B3 B4

(Branch Deleted After Merge) GIT'S "HEAD" REFERENCE

• Reference pointer to a commit

• “Detached HEAD” warning

• What does it mean?

• When does this occur?

• What should I do?

• Have code changes? Branch! BRANCH WORKFLOWS

• GitHub Flow

• Atlassian’s List

• Centralized

• Feature

• Git Flow

• Forking REMOTES REMOTES

• Local repo on your client only

• Remote is a Git host to push to

• You clone from remote(s)

• ie. GitHub,

• First remote is called “origin”

• Avoid changing the remote name, some GUI Apps look for it REMOTE REPOS

• Network Protocols

• HTTPS

• SSH (Keys)

• Git Protocol (daemon) SELF-HOSTED REPO SERVICES

• SSH

• Enable SSH, create “bare” repository on server

• % git init --bare --shared

• Atlassian Stash

• Enterprise GitHub

• GitLab HOSTED REPO SERVICES

• GitHub, BitBucket

• SourceRepo, Assembla

• GitLab

• git.psu.edu for Penn Staters (runs on GitLab)

• And many more … GIT WORKFLOW STAGES

Working Directory add Staging Area commit Repository pull push Remote CLONING

• Copies repo from remote

• Repo includes entire project PUSHING

• Saves commits to remote server

• Pushes active branch that you’re on FETCHING

• Updates repo status of remote

• Local repo files are not changed

• Always fetch or pull before pushing to remotes! REMOTES - DO'S AND DON'TS

• Do...

• Always fetch or pull before making changes

• Otherwise you will need to merge or rebase your changes

• Don't...

• Make changes to local repo before fetching/pulling, or else create a new branch first

• OR stash the files first REMOTES DEMO

• Add local repo to GitHub Desktop

• Publish to GitHub

• View new remote host via “git remote -vv”

• View tracking branch(es) via “git branch -vv”

• Local branches track remote branches

• Commit first to local, then push to GitHub

• Create README.md file on GitHub, Pull

COLLABORATION ETIQUETTE

• Pull Request vs. Feature Requests

• Read the project's docs and guidelines first

• Start with simple changes to learn processes

• Avoid pull requests on pull requests

• Instead, provide feedback to author of pull for additional fixes or suggestions ETIQUETTE

• Commit Messages

• Keep them small, clear and descriptive

• Remember others need to understand them

• Stick to the project's coding styles and conventions

• Follow or establish team decided branch workflow

• Be nice! CREATING A PULL REQUEST

Their Remote Repo Contributor fork Your Remote Repo push clone/pull merge Local Repository commit Pull Request Working Copy

Discussion GITHUB PULL REQUEST RECEIVING A PULL REQUEST

• Discuss and gather feedback RECEIVING A PULL REQUEST

• Squash & merge or Merge commit RECEIVING A PULL REQUEST

• Checkout and test AUTOMATION INTEGRATIONS

• Automatic emails

• Push notifications

• Slack Channels, IRC, Twitter, HipChat, etc.

• Methods

• WebHooks

• Agents / Runners

• Polling AUTOMATION TOOLS

• Scripts

• launchd, cron to automate git pulls

• AutoPkgr schedule for autopkg recipe repos

• Continuous Integration

• Jenkins, Atlassian Bamboo, GitLab

• Travis CI WEBHOOKS

• Receive event notifications between multiple systems

• commit, push (tag or branch), releases, issues, documentation updates, forks, etc.

• Useful for updating, notifying, building, testing, backing up, deploying AUTOMATION IDEAS

• Auto update servers (Web, etc.)

• Revert to previous commit

• Example

• Agent/Runner runs script after repo receives commit

• Automated testing, build and test products/recipes AUTOMATED WORKFLOWS

• Update server content from master branch

• Auto pull “ready for production” versions

• Track changes made on servers, switches

• .git folder security issue - be aware CLC CASE STUDY

GitLab GitHub

autopkg Jenkins

Slack BigFix CLC CASE STUDY

• Lessons Learned

• Use the tool that works best

• Keep it simple and robust

• Lots of notifications, categorized and specific

• Working with internal and external collaborators

• Leverage the features of Git - commits, events, builds SUMMARY LESSONS LEARNED

• Start small

• Pick just one project to manage with Git

• Use remotes when ready

• GitHub, BitBucket

• Your own SSH host LESSONS LEARNED

• Write good commit messages

• There will be a time when you need to search your commit messages and it will really help you out!

• Be nice to your future self.

• “Where / when / how did I fix that issue?”

• What’s for the release notes? LESSONS LEARNED

• Keep it simple

• Don't be afraid to experiment

• Automation and integrations are powerful

• Use them after you're comfortable with the basics RESOURCES AND TRAINING

• http://bit.ly/psumac-git-cs

• Official Git Site

• http://git-scm.com

• Git Cheat Sheets

• Git Pro Book

• bit.ly/psumac-nice-pulls RESOURCES AND TRAINING

• http://try.github.com - learn Git in 15 minutes!

• http://atlassian.com/git

• http://bit.ly/psumac-git-wf

• O’Reilly

• “McCullough and Berglund on Mastering Git” Q & A Justin Elliott jelliott [at] psu.edu @justindelliott

Matt Hansen hansen.m [at] psu.edu @hansen_m

Feedback: https://bit.ly/psumac2016-91