Git for System Admins.Key
Total Page:16
File Type:pdf, Size:1020Kb
GIT FOR SYSTEM ADMINS JUSTIN ELLIOTT PENN STATE UNIVERSITY VERSION CONTROL WHAT IS IT? • Manages changes and versions of files • Also known as: • Source Code Management (SCM) • Version Control System (VCS) WHY USE IT? • Reduces risk • Agility • Freedom to experiment • Collaboration • Helps document changes GIT INTRODUCTION WHY GIT? • Extremely fast performance • Excellent data integrity • Git checksums everything (SHA-1) • Easy to learn and to start small GIT OVERVIEW • Distributed • All clients have the full repo • Backups are everywhere • Local repo updated before remotes • When available, push updates to remote GIT VERSUS SUBVERSION • Much faster checkouts • Clients have full project • Distributed versus centralized • Network access optional* • Much simpler ignores • Seamless renames and moves • Easier branching and merging GIT FUNDAMENTALS GIT TERMINOLOGY • Working Directory • Repository REPO FILE CLASSIFICATIONS • Tracked • Ignored • Untracked TRACKED FILE STATES • Modified • Staged • Committed GIT WORKFLOW STAGES Working Directory Add Staging Area Commit Repository GIT INSTALLATION & CONFIGURATION HOW TO INSTALL GIT • Embedded with many Git GUI Apps • Download from http://git-scm.com • OS X: Terminal.app • Windows: Git Bash • Linux: Terminal • OS X: MacPorts and HomeBrew CONFIGURING GIT • Git GUI clients handle it for you, or … • $ git config • Contains items such as name, email, editor, diff tool USING GIT 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 COMMIT MESSAGES • Commit often - You will thank yourself later! • Or else … Larger changes are much harder to pick apart and revert back to! • Each commit should encapsulate a single fix* IGNORING FILES • Why • How • Scope • Repo only • All Repos ( Global ) REVIEWING DIFFS • Very easy to view with GitHub client and web • External diff apps • FileMerge • Kaleidoscope HISTORY LOGS • Review history of commits to repo • Extremely helpful for your future self and others GIT CLIENT APPS • http://git-scm.com/downloads/guis • GitHub • https://mac.github.com • https://windows.github.com • SourceTree • https://www.sourcetreeapp.com GITHUB • Simplifies the complex Git tasks • Integrates well with GitHub remote web service • Easy to start with • Limited feature set (by design) GITHUB AND STAGING FILES • Be aware … • The GitHub app does not show staged items. • So, why doesn’t it? • And why are you tell us this, Justin?! BRING IT ALL TOGETHER • Create repo • View file diff, commit • Commit file • Ignore files, commit • Modify the file • Review history logs GIT REMOTES REMOTE REPOS • Local repo on your client only • Remote is a Git host to push to • You clone from remotes • First remote is called “origin” REMOTE REPOS • Network Based • SSH • HTTPS • Git Protocol (daemon) SELF-HOSTED REPOS • SSH • Enable SSH, create “bare” repository on server • Atlassian Stash • Enterprise GitHub • GitLab REMOTE REPO HOST SERVICES • 3rd Party • GitHub, BitBucket • CodeSpaces, SourceRepo • Assembla, Gitorious • git.psu.edu (For Penn Staters) • And possibly more, I’m sure… 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 not changed • Tip: Fetch before pushing to remotes REMOTES DEMO • Create repo on GitHub (the remote) • Adds remote ref to local repo settings • Local branches track remote branches • Push full local repo to remote • Commit locally, push to origin remote • Create README.md file on GitHub, Pull BRANCHES BRANCHES • When to use them? • “master” is the default branch • Merging of branches is where Git really excels BRANCHES • Help to separate lines of development • “Git Flow” is a popular branching model • Ex: Master, Hotfix, Development, Feature, Release 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) HEAD REFERENCE • Checking out commit older than latest on branch • No code changes! Or … • Create a new branch FIRST LINE ENDINGS CONFIGURE LINE ENDINGS • End of line characters differ based on client OS • OS X and Linux use one invisible character • LF (Line Feed) • Windows uses two invisible characters • CR (Carriage Return) + LF (Line Feed) • Embrace the native line endings for each OS LINE ENDING MODES • core.autocrlf true • Use for cross platform projects on Windows clients • core.autocrlf input • Use for cross platform projects on OS X clients • core.autocrlf false • Use only on single platform projects LINE ENDINGS ON OS X Enable auto convert CRLF to LF: LINE ENDINGS ON WINDOWS Enable auto convert to LF on Windows by using git config to set core.autocrlf to true: 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? RESOURCES AND TRAINING • Official Git Site • http://git-scm.com • Git Cheat Sheets • Git Pro Book RESOURCES AND TRAINING • http://try.github.com • http://atlassian.com/git • O’Reilly • “McCullough and Berglund on Mastering Git” Q & A Justin Elliott jelliott [at] psu.edu @justindelliott.