git for system admins · git workflow stages working directory staging area add repository commit....

59
GIT FOR SYSTEM ADMINS JUSTIN ELLIOTT PENN STATE UNIVERSITY

Upload: others

Post on 10-Jul-2020

71 views

Category:

Documents


0 download

TRANSCRIPT

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

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)

W H Y U S E I T ?

• Reduces risk

• Agility

• Freedom to experiment

• Collaboration

• Helps document changes

G I T I N T R O D U C T I O N

W H Y G I T ?

• Extremely fast performance

• Excellent data integrity

• Git checksums everything (SHA-1)

• Easy to learn and to start small

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 V E R S U S S U B V E R S I O N

• Much faster checkouts

• Clients have full project

• Distributed versus centralized

• Network access optional*

• Much simpler ignores

• Seamless renames and moves

• Easier branching and merging

G I T F U N D A M E N TA L S

G I T T E R M I N O L O G Y

• Working Directory

• Repository

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

• Committed

G I T W O R K F L O W S TA G E S

Working Directory

Staging AreaAdd

RepositoryCommit

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

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

• OS X: Terminal.app

• Windows: Git Bash

• Linux: Terminal

• OS X: MacPorts and HomeBrew

C O N F I G U R I N G G I T

• Git GUI clients handle it for you, or …

• $ git config

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

U S I N G G I T

C R E AT E A R E P O

• Create new empty project repository

Working Directory

A D D F I L E S T O T H E R E P O

Working Directory

Staging AreaAdd

• Add (stage) files for the next commit

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 M E S S A G E S

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

I G N O R I N G F I L E S

• Why • How • Scope

• Repo only • All Repos ( Global )

R E V I E W I N G D I F F S

• Very easy to view with GitHub client and web

• External diff apps

• FileMerge

• Kaleidoscope

H I S T O R Y L O G S

• Review history of commits to repo

• Extremely helpful for your future self and others

G I T C L I E N T A P P S

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

• GitHub

• https://mac.github.com

• https://windows.github.com

• SourceTree

• https://www.sourcetreeapp.com

G I T H U B

• Simplifies the complex Git tasks

• Integrates well with GitHub remote web service

• Easy to start with

• Limited feature set (by design)

G I T H U B A N D S TA G I N G F I L E S

• Be aware …

• The GitHub app does not show staged items.

• So, why doesn’t it?

• And why are you tell us this, Justin?!

B R I N G I T A L L T O G E T H E R

• Create repo

• Commit file

• Modify the file

• View file diff, commit

• Ignore files, commit

• Review history logs

G I T R E M O T E S

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

• First remote is called “origin”

R E M O T E R E P O S

• Network Based

• SSH

• HTTPS

• Git Protocol (daemon)

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

R E M O T E R E P O H O S T S E R V I C E S

• 3rd Party

• GitHub, BitBucket

• CodeSpaces, SourceRepo

• Assembla, Gitorious

• git.psu.edu (For Penn Staters)

• And possibly more, I’m sure…

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 not changed

• Tip: Fetch before pushing to remotes

R E M O T E S D E M O

• 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

B R A N C H E S

B R A N C H E S

• When to use them?

• “master” is the default branch

• Merging of branches is where Git really excels

B R A N C H E S

• Help to separate lines of development

• “Git Flow” is a popular branching model

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

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)

H E A D R E F E R E N C E

• Checking out commit older than latest on branch

• No code changes! Or …

• Create a new branch FIRST

L I N E E N D I N G S

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

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

L I N E E N D I N G S O N O S X

Enable auto convert CRLF to LF:

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:

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?

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

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”

Q & AJustin Elliott

jelliott [at] psu.edu

@justindelliott