Git! Fundamentals IT Pro Roundtable! June 17, 2014!! Justin Elliott! ITS / TLT! Classroom and Lab Computing!! Michael Potter! IT Communications 1
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
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
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
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
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
Basic Git Terminology Repository! Working Directory! Clone! Commit 7
Basic Git Terminology Branch! Tag! Remotes (Origin)! HEAD (Refers to Branch or Commit) 8
Git File Classifications Tracked -- already in the repository! Ignored -- can exist, but git ignores it! Untracked -- What s not tracked or ignored 9
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
Git File Status Lifecycle 11
Git Workflow Working Directory Staging Area Repository 12
Installation Download installer from http://git-scm.com! Windows! Git Bash Is Recommended! OS X! Use Terminal.app! Embedded with many GUI clients 13
Initial Git Config Set your user name and email address via git config command, like this: 14
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
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
Line Endings on Windows Enable auto convert to LF on Windows by using git config to set core.autocrlf to true: 17
Line Endings on OS X On OS X, enable auto convert CRLF to LF: 18
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
Git Attributes Example In the root of the Git repo folder, the.gitattributes file: 20
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
Cloning a Repository 22
Create A New Repository From the command line:! $ git init reponame.git! Creates special.git directory within the project directory 23
Create A New Repository 24
Status Of Files How do I know what has changed?! $ git status! Status reports different states! Untracked! Modified! Staged 25
Status Of Files 26
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
Adding Files To The Repo 28
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
Committing Changes 30
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
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
Differences 33
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
Ignoring Files 35
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
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
Git GUI Clients GitHub! OS X! Windows! Mobile 38
Git GUI Clients SourceTree! OS X! Windows! Tower! OS X! SmartGit, GitBox, TortoiseGit 39
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
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
Remote Repos Network Based! HTTPS! SSH! Git Protocol (daemon) 42
Remote Repos 3rd Party! git.psu.edu! GitHub, BitBucket, Google Code! CodeSpaces, SourceRepo! Assembla, Gitorious 43
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
GitLab Cool Features Free public and private repos! Issue tracking! Groups! Commit history graph reporting! Project wikis 45
Demo 46
Git Flow Workflow for managing branches! Five different branch types! feature branches! develop! release branches! hotfixes! master 47
Git Flow 48
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
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
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
Hotfix The Oh No branch! Branched directly off of master! Once fixed, it s merged up to master and down to develop branch 52
Master The project s live code! Only for merging 53
SourceTree Git Flow 54
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
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
Start small! Lessons Learned 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
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
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
Resources and Training http://try.github.com! http://atlassian.com/git! O Reilly! McCullough and Berglund on Mastering Git 60
Questions? Yammer Groups! git.psu.edu! PSU Software Developer Working Group! Justin Elliott jde6@psu.edu! Michael Potter mgp140@psu.edu 61