U S I N G G I T F O R A U T O M AT I O N A N D C O L L A B O R AT I O N
J U S T I N E L L I O T T - M A T T H A N S E N P E N N S TA T E U N I V E R S I T Y
A G E N D A
• Version control overview
• Introduction and basics of Git
• Advanced Git features
• Collaboration
• Automation
V E R S I O N C O N T R O L
W H AT I S I T ?
• 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
W H Y U S E I T ?
• Reduces risk and enables you to experiment with code
• Agility
• Flexible workflows
• Quicker deployments
• Automate updates
W H Y U S E I T ?
• Helps with documentation
• Collaboration with others
• Contributing to open source projects
• Modifying a project for your own use
G I T I N T R O D U C T I O N
W H Y G I T ?
• The current de facto standard for source code management
• Easy to learn, quick to start!
• Very powerful and extensive
G I T O V E R V I E W
• Extremely fast
• Excellent data integrity
• Git checksums everything (SHA-1)
G I T O V E R V I E W
• Distributed
• All clients have the full repo
• Backups are everywhere
• Local repo updated before remotes
• When available, push updates to remote
G I T T E R M I N O L O G Y
• Repository
• Local
• Remote
• Working Directory
• Commit
G I T I N S TA L L AT I O N & C O N F I G U R AT I O N
H O W T O I N S TA L L G I T
• 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
G I T C L I E N T A P P S
• http://git-scm.com/downloads/guis
• GitHub Desktop
• https://desktop.github.com
• SourceTree
• https://www.sourcetreeapp.com
R E V I E W I N G D I F F S
• Very easy to view with GitHub Desktop and Web
• External diff apps
• FileMerge
• Kaleidoscope
G I T H U B D E S K T O P
• Simplifies the complex Git tasks
• Integrates well with GitHub hosting service
• Easy to start with
• Limited feature set (by design)
D E M O : G I T H U B C O N F I G
C O N F I G U R I N G G I T O N T H E C L I
• 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
D E M O : G I T C O N F I G C L I
https://asciinema.org/a/anjsg2tmlwul6j3ahizgl98ie
F U N D A M E N TA L S
R E P O F I L E C L A S S I F I C AT I O N S
• Tracked
• Ignored
• Untracked
T R A C K E D F I L E S TAT E S
• Modified
• Staged
• GitHub Desktop shows as checkboxes
• Committed
C R E AT E A R E P O
• Create new empty project repository
Working Directory
• Add (stage) files for the next commit
A D D F I L E S T O T H E R E P O
Working Directory
Staging Areaadd
C O M M I T F I L E S T O R E P O
Working Directory
Staging Areaadd
Repositorycommit
• Save staged files to the project repository
C O M M I T S
• 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.
I G N O R I N G F I L E S / D I R E C T O R I E S
• Why? • What? • How? • Scope
• Single Project Repo • All Repos ( Global ~/.gitignore_global file )
• Recommended to create ignore file as first step
C O M M I T H I S T O R Y L O G S
• Review history of commits to repo
• Documents changes over time
• Extremely helpful for your future self and others
D E M O
• Create repo
• Create files
• Show status
• Ignore file(s)
• Commit git ignore file
• Initial commit of file
• Modify same tracked file
• View diff of file changes
• Commit the file
• Review history log
• Revert to previous commit
• Review history log
B R A N C H E S
B R A N C H E S
• 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)
B R A N C H W A L K T H R O U G H
M 1
First Commit
B R A N C H W A L K T H R O U G H
M 1
New Branch
B 1
B R A N C H W A L K T H R O U G H
M 1
B 1 B 2
Second Branch Commit
B R A N C H W A L K T H R O U G H
M 1
B 1 B 2 B 3
Third Branch Commit
B R A N C H W A L K T H R O U G H
M 1
B 1 B 2 B 3 B 4
Fourth Branch Commit
B R A N C H W A L K T H R O U G H
M 1
Merge Branch To Master
B 1 B 2 B 3 B 4
M 2
(Branch Deleted
After Merge)
G I T ' S " H E A D " R E F E R E N C E
• Reference pointer to a commit
• “Detached HEAD” warning
• What does it mean?
• When does this occur?
• What should I do?
• Have code changes? Branch!
B R A N C H W O R K F L O W S
• GitHub Flow
• Atlassian’s List
• Centralized
• Feature
• Git Flow
• Forking
R E M O T E S
R E M O T E S
• Local repo on your client only
• Remote is a Git host to push to
• You clone from remote(s)
• ie. GitHub, BitBucket
• First remote is called “origin”
• Avoid changing the remote name, some GUI Apps look for it
R E M O T E R E P O S
• Network Protocols
• HTTPS
• SSH (Keys)
• Git Protocol (daemon)
S E L F - H O S T E D R E P O S E R V I C E S
• SSH
• Enable SSH, create “bare” repository on server
• % git init --bare --shared
• Atlassian Stash
• Enterprise GitHub
• GitLab
H O S T E D R E P O S E R V I C E S
• GitHub, BitBucket
• SourceRepo, Assembla
• GitLab
• git.psu.edu for Penn Staters (runs on GitLab)
• And many more …
G I T W O R K F L O W S TA G E S
Working Directory
Staging Areaadd
Repositorycommit
Remotepushpull
C L O N I N G
• Copies repo from remote
• Repo includes entire project
P U S H I N G
• Saves commits to remote server
• Pushes active branch that you’re on
F E T C H I N G
• Updates repo status of remote
• Local repo files are not changed
• Always fetch or pull before pushing to remotes!
R E M O T E S - D O ' S A N D D O N ' T S
• 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
R E M O T E S D E M O
• 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
C O L L A B O R AT I O N
E T I Q U E T T E
• 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
E T I Q U E T T E
• 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!
C R E AT I N G A P U L L R E Q U E S T
Their Remote Repo
Your Remote Repofork
Local Repositoryclone/pull
Working Copy
commit
Pull Request
mergepush
Discussion
Contributor
G I T H U B P U L L R E Q U E S T
R E C E I V I N G A P U L L R E Q U E S T
• Discuss and gather feedback
R E C E I V I N G A P U L L R E Q U E S T
• Squash & merge or Merge commit
R E C E I V I N G A P U L L R E Q U E S T
• Checkout and test
A U T O M AT I O N
I N T E G R AT I O N S
• Automatic emails
• Push notifications
• Slack Channels, IRC, Twitter, HipChat, etc.
• Methods
• WebHooks
• Agents / Runners
• Polling
A U T O M AT I O N T O O L S
• Scripts
• launchd, cron to automate git pulls
• AutoPkgr schedule for autopkg recipe repos
• Continuous Integration
• Jenkins, Atlassian Bamboo, GitLab
• Travis CI
W E B H O O K S
• 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
A U T O M AT I O N I D E A S
• 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
A U T O M AT E D W O R K F L O W S
• Update server content from master branch
• Auto pull “ready for production” versions
• Track changes made on servers, switches
• .git folder security issue - be aware
C L C C A S E S T U D Y
autopkg
GitLab GitHub
BigFix
Jenkins
Slack
C L C C A S E S T U D Y
• 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
S U M M A R Y
L E S S O N S L E A R N E D
• Start small
• Pick just one project to manage with Git
• Use remotes when ready
• GitHub, BitBucket
• Your own SSH host
L E S S O N S L E A R N E D
• 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?
L E S S O N S L E A R N E D
• Keep it simple
• Don't be afraid to experiment
• Automation and integrations are powerful
• Use them after you're comfortable with the basics
R E S O U R C E S A N D T R A I N I N G
• http://bit.ly/psumac-git-cs
• Official Git Site
• http://git-scm.com
• Git Cheat Sheets
• Git Pro Book
• bit.ly/psumac-nice-pulls
R E S O U R C E S A N D T R A I N I N G
• 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”
Justin Elliott jelliott [at] psu.edu
@justindelliott
Matt Hansen hansen.m [at] psu.edu
@hansen_m
Feedback: https://bit.ly/psumac2016-91
Q & A