FOR SYSTEM ADMINS

JUSTIN ELLIOTT PENN STATE UNIVERSITY

1 WHAT IS VERSION CONTROL?

• Management of changes to documents like source code, scripts, text files

• Provides the ability to check documents in and out of a versioning system

• Version Control System (VCS)

• Source Code Management (SCM)

2 WHY SHOULD YOU USE IT?

• Enables you to easily back out of mistakes

• Removes the risk of updating your scripts and code

• Excellent for collaboration with others

• Greatly enhances your ability to be agile and move quickly with scripts and coding.

3 GIT VERSUS SUBVERSION

• Lightning fast! Checkouts are MUCH faster

• Clients have the full repo

• Distributed versus centralized

• No need for constant network access

• Easier to ignore files and edit other properties

• Git easily handles renaming and moving files

• Branching and Merging is much easier in Git

4 GIT: A GREAT SCM TOOL

• Extremely fast performance

• Excellent data integrity

• Git checksums everything (SHA-1)

• Easy to learn and start small

5 GIT OVERVIEW

• Git is distributed

• All clients have the full repo

• Changes are applied to local repo first

• Backups are everywhere

• Each member of your team has a local copy

6 GIT TERMINOLOGY

• Repository

• Working Directory

• Staging Area

• Remote

7 GIT FILE CLASSIFICATIONS

• Tracked

• Already in the repo, Git is watching it

• Ignored

• Can exist locally, not in repo and git ignores it

• Untracked

• What's not tracked or ignored

8 GIT FILE STATES

• Modified

• Is tracked, changed but not yet committed

• Staged

• Modified filed marked to go into next commit

• Committed

• Safely stored in the repo

9 BASIC GIT WORKFLOW

Working Directory

Staging Area

Repository

10 HOW TO INSTALL GIT

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

• OS X: Terminal.app

• Windows: Git Bash

• Embedded with many Git GUI client apps

11 CONFIGURING GIT

• $ git config

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

12 CONFIGURE LINE ENDINGS

• End of line characters differ based on client OS

• OS X and 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

13 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

14 LINE ENDINGS ON OS X

Enable auto convert CRLF to LF:

15 LINE ENDINGS ON WINDOWS

Enable auto convert to LF on Windows by using git config to set core.autocrlf to true:

16 CREATE A REPOSITORY

• $ git init ProjectName.git

• Initializes a new and clean repository in new directory

• $ git clone

• The process of copying an existing Git repository locally to your computer

• Copies the entire repository history and file revisions

17 STATUS OF FILE CHANGES

• How do I know what has changed?

• $ git status

• Status reports different states

• Untracked

• Modified

• Staged

18 IGNORING FILES

• Some files should never reside in the repo

• The .gitignore file solves this

• Specify file names, directory paths to ignore

• Supports Wildcards

*NotForRepo*, *.dmg

10.9.4-Lab-Image.dmg

LargeDataFile-NotForRepo.db

19 ADDING FILES THE REPO

• Adding files are “staged”

• Why not just directly commit the changes?

• git add -v

• git add -v .

• git add -A

20 UNSTAGING FILES

• File not yet tracked but staged:

• $ git rm --cached NewFile.txt

• Files already tracked in repo, revert to previous

• $ git reset HEAD FileName.txt

21 COMMITTING CHANGES

• This process saves the revisions made to the repository

• 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*

• $ git commit -m

22 VIEWING DIFFS

• Commits are all about what is different

• Viewing the differences

• $ git diff

• GUI Apps

• Kaleidoscope, FileMerge, Changes.app, etc.

23 VIEWING COMMIT LOGS

• $ git log

• $ git log -p -2

• Lists changes in last two commit entries

24 BRANCHES

• “master” is the default branch

• Merging of branches is where Git excels

• Help to separate lines of development

• “Git Flow” is a popular branching model

• Ex: Master, Hotfix, Development, Feature, Release

25 BRANCHES

• $ git checkout master

• Create and switch to new branch

• $ git checkout -b

• Delete branch

• $ git branch -d

26 HEAD REFERENCE

• HEAD is a reference to latest commit in branch OR a specific commit

• What does "detached HEAD" mean, and why should I care?

• Occurs when you check out a specific commit in a branch versus the latest commit of branch

27 COMMAND LINE DEMO

• Create Repo • Review Logs

• Create .gitignore • View Diffs

• Add Files • Checkout commit

• Commit Changes

28 REMOTE REPOS

• Local repo on your client only

• Remote is a Git host to push to

• You clone from remotes

29 REMOTE REPOS

• Network Based

• SSH

• HTTPS

• Git Protocol (daemon)

30 SELF-HOSTED REPOS

• SSH

• Enable SSH, create “bare” repository on server

• Atlassian Stash

• Enterprise GitHub

• GitLab

31 REMOTE REPOS

• 3rd Party

• GitHub, BitBucket, Google Code

• CodeSpaces, SourceRepo

• Assembla, Gitorious

• git.psu.edu (For Penn Staters)

• And possibly more, I’m sure…

32 CLONING A REPOSITORY

• The process of copying an existing Git repository to your computer

• A cloned repo includes the entire file history changes and commit messages

• Git clone command example:

• $ git clone https://server.edu/RepoName.git

33 PUSHING CHANGES

• $ git push origin master

• Push (upload) the ‘master’ branch to the ‘origin’ remote host

34 GUI CLIENTS

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

• GitHub (OS X and Windows)

• SourceTree (OS X and Windows)

• Tower (OS X)

35 BASICS OF GITHUB DEMO

• Create Repo • Commit

• Create .gitignore • View Diffs

• Add Files • Review Logs

36 SOURCETREE

• Integrated support for Git hosting services

• GitHub, BitBucket, Kiln, Stash

• Helps to simplify more complex tasks

• Decent UI

37 SOURCETREE DEMO

• Create repo • Review Logs

• Create .gitignore • View Diffs

• Add files • Create Branch

• Commit changes • Merge Branch

38 LESSONS LEARNED

• Start small

• Pick just one project to manage with Git

• Use remotes when ready

• GitHub, BitBucket, SSH host

39 LESSONS LEARNED

• Take the time to 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?”

40 GIT COMMAND LINE HELP

• $ git help

• $ git help

41 RESOURCES AND TRAINING

• Official Git Site

• http://git-scm.com

• Git Cheat Sheets

• Git Pro Book

42 RESOURCES AND TRAINING

• http://try.github.com

• http://atlassian.com/git

• O’Reilly

• “McCullough and Berglund on Mastering Git”

43 Q & A

Justin Elliott jelliott [at] psu.edu ! @justindelliott

44 THANK YOU.

Justin Elliott jelliott [at] psu.edu ! @justindelliott

45