GIT FOR SYSTEM ADMINS · GIT VERSUS SUBVERSION • Lightning fast! Checkouts are MUCH faster •...
Transcript of GIT FOR SYSTEM ADMINS · GIT VERSUS SUBVERSION • Lightning fast! Checkouts are MUCH faster •...
G I T F O R S Y S T E M A D M I N S
J U S T I N E L L I O T T P E N N S TA T E U N I V E R S I T Y
1
W H AT I S V E R S I O N C O N T R O L ?
• 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
W H Y S H O U L D Y O U U S E I T ?
• 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
G I T V E R S U S S U B V E R S I O N
• 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
G I T: A G R E AT S C M T O O L
• Extremely fast performance
• Excellent data integrity
• Git checksums everything (SHA-1)
• Easy to learn and start small
5
G I T O V E R V I E W
• 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
G I T T E R M I N O L O G Y
• Repository
• Working Directory
• Staging Area
• Remote
7
G I T F I L E C L A S S I F I C AT I O N S
• 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
G I T F I L E S TAT E S
• Modified
• Is tracked, changed but not yet committed
• Staged
• Modified filed marked to go into next commit
• Committed
• Safely stored in the repo
9
B A S I C G I T W O R K F L O W
Working Directory
Staging Area
Repository
10
H O W T O I N S TA L L G I T
• Download from http://git-scm.com/downloads
• OS X: Terminal.app
• Windows: Git Bash
• Embedded with many Git GUI client apps
11
C O N F I G U R I N G G I T
• $ git config
• Contains items such as name, email, editor, diff tool
12
C O N F I G U R E L I N E E N D I N G S
• 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
13
L I N E E N D I N G M O D E S
• 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
L I N E E N D I N G S O N O S X
Enable auto convert CRLF to LF:
15
L I N E E N D I N G S O N W I N D O W S
Enable auto convert to LF on Windows by using git config to set core.autocrlf to true:
16
C R E AT E A R E P O S I T O R Y
• $ git init ProjectName.git
• Initializes a new and clean repository in new directory
• $ git clone <repoURL> <directory>
• The process of copying an existing Git repository locally to your computer
• Copies the entire repository history and file revisions
17
S TAT U S O F F I L E C H A N G E S
• How do I know what has changed?
• $ git status
• Status reports different states
• Untracked
• Modified
• Staged
18
I G N O R I N G F I L E S
• 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
A D D I N G F I L E S T H E R E P O
• Adding files are “staged”
• Why not just directly commit the changes?
• git add -v <file/folder_name>
• git add -v .
• git add -A
20
U N S TA G I N G F I L E S
• 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
C O M M I T T I N G C H A N G E S
• 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 <commit_message>
22
V I E W I N G D I F F S
• Commits are all about what is different
• Viewing the differences
• $ git diff <FileName>
• GUI Apps
• Kaleidoscope, FileMerge, Changes.app, etc.
23
V I E W I N G C O M M I T L O G S
• $ git log
• $ git log -p -2
• Lists changes in last two commit entries
24
B R A N C H E S
• “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
B R A N C H E S
• $ git checkout master
• Create and switch to new branch
• $ git checkout -b <branch>
• Delete branch
• $ git branch -d <branch>
26
H E A D R E F E R E N C E
• 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
C O M M A N D L I N E D E M O
• Create Repo
• Create .gitignore
• Add Files
• Commit Changes
• Review Logs
• View Diffs
• Checkout commit
28
R E M O T E R E P O S
• Local repo on your client only
• Remote is a Git host to push to
• You clone from remotes
29
R E M O T E R E P O S
• Network Based
• SSH
• HTTPS
• Git Protocol (daemon)
30
S E L F - H O S T E D R E P O S
• SSH
• Enable SSH, create “bare” repository on server
• Atlassian Stash
• Enterprise GitHub
• GitLab
31
R E M O T E R E P O S
• 3rd Party
• GitHub, BitBucket, Google Code
• CodeSpaces, SourceRepo
• Assembla, Gitorious
• git.psu.edu (For Penn Staters)
• And possibly more, I’m sure…
32
C L O N I N G A R E P O S I T O R Y
• 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
P U S H I N G C H A N G E S
• $ git push origin master
• Push (upload) the ‘master’ branch to the ‘origin’ remote host
34
G U I C L I E N T S
• http://git-scm.com/downloads/guis
• GitHub (OS X and Windows)
• SourceTree (OS X and Windows)
• Tower (OS X)
35
B A S I C S O F G I T H U B D E M O
• Create Repo
• Create .gitignore
• Add Files
• Commit
• View Diffs
• Review Logs
36
S O U R C E T R E E
• Integrated support for Git hosting services
• GitHub, BitBucket, Kiln, Stash
• Helps to simplify more complex tasks
• Decent UI
37
S O U R C E T R E E D E M O
• Create repo
• Create .gitignore
• Add files
• Commit changes
• Review Logs
• View Diffs
• Create Branch
• Merge Branch
38
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, SSH host
39
L E S S O N S L E A R N E D
• 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
G I T C O M M A N D L I N E H E L P
• $ git help
• $ git help <command>
41
R E S O U R C E S A N D T R A I N I N G
• Official Git Site
• http://git-scm.com
• Git Cheat Sheets
• Git Pro Book
42
R E S O U R C E S A N D T R A I N I N G
• http://try.github.com
• http://atlassian.com/git
• O’Reilly
• “McCullough and Berglund on Mastering Git”
43
Q & AJustin Elliott
jelliott [at] psu.edu !
@justindelliott
44
T H A N K Y O U .
Justin Elliott jelliott [at] psu.edu
!@justindelliott
45