git - it pro roundtable june 16...

61
Git Fundamentals IT Pro Roundtable June 17, 2014 Justin Elliott ITS / TLT Classroom and Lab Computing Michael Potter IT Communications 1

Upload: truongkhue

Post on 10-Feb-2018

239 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git!Fundamentals

IT Pro Roundtable!June 17, 2014!

!Justin Elliott!

ITS / TLT!Classroom and Lab Computing!

!Michael Potter!

IT Communications

1

Page 2: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

What is Version Control?

• Version Control System (VCS)!

• Source Code Management (SCM)!

• Management of changes to documents like

source code, scripts, text files!

• Provides the ability to check documents in and

out of a repository

2

Page 3: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Why Use It?

• It’s totally liberating!!

• Removes the risk of updating your code, encourages you to experiment!

• It will greatly enhance your ability to be agile and move quickly with development!

• Quick access to snapshot versions of your documents / source code

3

Page 4: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Why Use It?

• Can easily collaborate with multiple developers on different project!

• Starting to become a standard for new open source web projects!

• Git Flow promotes good dev, test, live practices.!

• Very easy to track, deploy, revert code

4

Page 5: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git: A Great SCM Tool• Easy to learn, very powerful!

• Distributed (DVCS)!

• Clients have the full repo!

• Extremely fast performance!

• Excellent data integrity!

• Git checksums everything in the repo (sha1)!

• Easy branching. No, really. Sorry SVN!

5

Page 6: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git versus Subversion• Lightning Fast, much faster checkouts!

• Merging is much easier!

• Distributed versus centralized!

• No need for constant network access!

• Easier to ignore files and edit other properties!

• Moving files and folders in Git is much easier to do than in Subversion

6

Page 7: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Basic Git Terminology

• Repository!

• Working Directory!

• Clone!

• Commit

7

Page 8: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Basic Git Terminology

• Branch!

• Tag!

• Remotes (Origin)!

• HEAD (Refers to Branch or Commit)

8

Page 9: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git File Classifications

• Tracked -- already in the repository!

• Ignored -- can exist, but git ignores it!

• Untracked -- What’s not tracked or ignored

9

Page 10: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git File States

• Modified -- changed but not yet committed!

• Staged -- marked a modified file to go into next commit!

• Committed -- safely stored in the repository

10

Page 11: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git File Status Lifecycle

11

Page 12: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git Workflow

Working Directory

Staging Area

Repository

12

Page 13: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Installation

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

• Windows!

• Git Bash Is Recommended!

• OS X!

• Use Terminal.app!

• Embedded with many GUI clients

13

Page 14: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Initial Git Config

• Set your user name and email address via ‘git config’ command, like this:

14

Page 15: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Beware Line Endings• End of line characters differ based on OS!

• Windows uses two invisible characters!

• CR (Carriage Return)!

• LF (Line Feed)!

• OS X and Linux use one invisible character!

• LF (Line Feed)!

• Embrace the native line endings for each platform, everyone will be much happier.

15

Page 16: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Line Ending Modes

• core.autocrlf true!

• Recommended for cross platform projects from Windows clients!

• core.autocrlf input!

• Recommended for cross platform projects from OS X / Linux / Unix clients!

• core.autocrlf false!

• Recommended only for single platform projects

16

Page 17: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Line Endings on Windows

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

17

Page 18: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Line Endings on OS X

• On OS X, enable auto convert CRLF to LF:

18

Page 19: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git Attributes

• Useful for configuring and enforcing the type of line endings files get when commited to the repository!

• Specified based on filename extensions in the .gitattributes file!

• Still need to configure the client’s auto.crlf setting so their OS is happy with endings

19

Page 20: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git Attributes Example

• In the root of the Git repo folder, the .gitattributes file:

20

Page 21: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Cloning a Repository

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

• Cloning a repo includes the entire file history changes and commit messages!

• Git Clone Command example:!

• $ git clone https://git.psu.edu/jde6/project.git

21

Page 22: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Cloning a Repository

22

Page 23: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Create A New Repository

• From the command line: !

$ git init reponame.git!

• Creates special ‘.git’ directory within the project directory

23

Page 24: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Create A New Repository

24

Page 25: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Status Of Files

• How do I know what has changed?!

$ git status!

• Status reports different states!

• Untracked !

• Modified!

• Staged

25

Page 26: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Status Of Files

26

Page 27: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Adding Files To The Repo• Added files are staged!

• Why stage anything at all?!

• How do I add modified items?!

• cd to reponame.git directory!

• Add a file!

$ git add -v FileName.php!

• Add all new files!

$ git add -v .

27

Page 28: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Adding Files To The Repo

28

Page 29: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Committing Changes• This process saves the changes to the repository.!

• Commit Often - You will thank yourself later!!

• Or else …. larger changes are much harder to pick apart and revert back.!

• Get those “Mini Victories” of progress!

$ git commit -m “Commit Message”!

• Each commit should encapsulate a single fix*

29

Page 30: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Committing Changes

30

Page 31: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Stashing

• Very useful for when you’re not ready to commit the changes to the repo yet.!

• Saves your changes to the local Git repo and does not commit them.!

$ git help stash

31

Page 32: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Differences

• Commits are all about what is … different!!

• Viewing the differences!

• At the command line!

• $ git diff FileName!

• In GUI Apps!

• Kaleidoscope, FileMerge, Changes.app, etc.

32

Page 33: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Differences

33

Page 34: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Ignoring Files

• Use the .gitignore file to add filenames and paths to ignore and exclude from the repo!

• Specify names of directories or files!

• Use wildcards for matching!

• Temp Cache files, file extensions, etc.!

• *.mp3, *.tmp, SensitiveData.xlsx, etc.

34

Page 35: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Ignoring Files

35

Page 36: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Branches

• Master (Main) branch is the default!

• Separate lines of development!

• Ex: Master, Hot Fix, Development, Feature!

• Merging of branches is where Git excels!

• “Git Flow” is a popular branching model

36

Page 37: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Tags

• Useful for marking a specific commit!

• Example: Mark commit that is for major version change!

• 1.0.1!

• 1.0.2!

• Also useful for regression testing with the ‘git bisect’ command

37

Page 38: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git GUI Clients

• GitHub!

• OS X!

• Windows!

• Mobile

38

Page 39: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git GUI Clients

• SourceTree!

• OS X!

• Windows!

• Tower!

• OS X!

• SmartGit, GitBox, TortoiseGit

39

Page 40: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

SourceTree

• Free!

• Excellent GUI client for OS X and Windows!

• Connects to Git Hosting Services!

• git.psu.edu!

• Via SSH, HTTPS!

• GitHub, Bitbucket, Google Code, etc.

40

Page 41: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Remote Repos

• Local Versus Remote (Hosted) Repos!

• Push changes to local first, then push all to remote repo(s)!

• Cloning from a remote host!

• Branching from a remote/local host!

• Read/Write versus Read-Only Accounts

41

Page 42: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Remote Repos

• Network Based!

• HTTPS!

• SSH!

• Git Protocol (daemon)

42

Page 43: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Remote Repos

• 3rd Party!

• git.psu.edu!

• GitHub, BitBucket, Google Code!

• CodeSpaces, SourceRepo!

• Assembla, Gitorious

43

Page 44: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

PSU GitLab

• Web-based hosting service for Git http://git.psu.edu!

• Internally hosted by VMhosting a service of ITS SaS!

• Available to anyone with a Penn State Access Account!

• Currently doesn’t support FOPS accounts

44

Page 45: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

GitLab Cool Features

• Free public and private repos!

• Issue tracking!

• Groups!

• Commit history graph reporting!

• Project wikis

45

Page 46: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Demo

46

Page 47: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git Flow

• Workflow for managing branches!

• Five different branch types!

• feature branches!

• develop!

• release branches!

• hotfixes!

• master

47

Page 48: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Git Flow

48

Page 49: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Feature Branches

• Where the code is made!

• Examples of feature branch: !

• “Responsive Calendar”, “Homepage Twitter Widget”, “New Article Field”!

• Feature branch could have multiple branches off of it

49

Page 50: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Develop

• Completed features are tested!

• Multiple features can be tested to see how they impact each other !

• Can be linked to a development server, often named “Integration Server”

50

Page 51: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Release Branches

• Collection of features that passed testing in the develop branch!

• Production ready code ready to be merged into master!

• Could be linked to a QA server

51

Page 52: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Hotfix

• The “Oh No” branch!

• Branched directly off of master!

• Once fixed, it’s merged up to master and down to develop branch

52

Page 53: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Master

• The project’s live code!

• Only for merging

53

Page 54: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

SourceTree Git Flow

54

Page 55: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Lessons Learned• Branch Names!

• No White Space (tabs, spaces)!

• Supports Hierarchical Names*!

• ‘bug/pr-1023’, ‘bug/pr-17’!

• $ git show-branch ‘bug/*’!

• Tags!

• Use “annotated” most of the time

55

Page 56: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

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?”

56

Page 57: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Lessons Learned

• Start small!

• Pick one project to implement Git!

• Use just the develop and master branches at first, add additional branches as you get more comfortable!

• Incorporate GitLab features into your project management workflow!

• wikis, issues, comments

57

Page 58: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Lessons Learned

• Using a VCS/SCM makes you more mobile and code tested on more environments makes that project more robust.!

• Commenting helps to clarify your comments, design flow, and for code review!

• Git is a fantastic tool for collaborative projects

58

Page 59: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Resources and Training

• Official Git Site!

• http://git-scm.com!

• http://git-scm.com/book (Pro Git Book)!

• lynda.psu.edu

• “Fundamentals of Software Version Control”

• “Git Essential Training”

59

Page 60: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Resources and Training

• http://try.github.com!

• http://atlassian.com/git!

• O’Reilly!

• “McCullough and Berglund on Mastering Git”

60

Page 61: Git - IT Pro Roundtable June 16 2014sites.psu.edu/.../3098/2014/06/Git-IT-Pro-Roundtable-June-16-2014.pdf · •Merging of branches is where Git excels! •“Git Flow” is a popular

Questions?

• Yammer Groups!

• git.psu.edu!

• PSU Software Developer Working Group!

• Justin Elliott [email protected]!

• Michael Potter [email protected]

61