89
Git: From Start To Finish Scott Gallagher [email protected] Justin Elliott [email protected]

2014 MacAdmins Git Workshop

Embed Size (px)

Citation preview

Page 1: 2014 MacAdmins Git Workshop

Git: From Start To FinishScott Gallagher [email protected]

Justin Elliott [email protected]

Page 2: 2014 MacAdmins Git Workshop

Session Feedback!

http://j.mp/psumac9

Page 3: 2014 MacAdmins Git Workshop

Part I - Git Basics

What is Version Control?

Why use it?

What is Git?

Git versus Subversion

Git Overview

Page 4: 2014 MacAdmins Git Workshop

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 repository

Version Control System (VCS)

Source Code Management (SCM)

Page 5: 2014 MacAdmins Git Workshop

What is Version Control?

"Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.” ( git-scm.com )

Page 6: 2014 MacAdmins Git Workshop

Why Use it?

You can revert and go back easily

Removes risk of updating your code

See process of steps you have taken

Fantastic tool that encourages collaboration

Page 7: 2014 MacAdmins Git Workshop

Why Use it?

Easy testing on multiple systems

Runs on Windows, Mac, and Linux!

Reuse of old code

Ability to go back in time! See how far you've come

Easy sharing and collaboration

Page 8: 2014 MacAdmins Git Workshop

What can I Use Git For?

ANY text based file

binary files too, but not as efficiently

Python, Bash, Perl, Ruby, scripts, text, config files, etc.

List is shorter of what you can’t use it for!

Page 9: 2014 MacAdmins Git Workshop

Git versus SubversionLightning fast! Checkouts are MUCH faster

Distributed versus centralized

No need for constant network access

Merging is much easier

Easier to ignore files and edit other properties

Git easily handles renaming and moving files

Page 10: 2014 MacAdmins Git Workshop

Git Overview

Git is distributed

You checkout and you have the full repo

Changes are applied locally first

Backups are everywhere!

Each member of your team has a local copy!

Page 11: 2014 MacAdmins Git Workshop

TerminologyRepository

Working Directory

Staging Area

Used for preparing commits

Git Directory

Repository object store/Remote Repository

Final destination for you data changes

Page 12: 2014 MacAdmins Git Workshop

Terminology

Working Directory

Index/ Staging

Directory/Repository

Local Local

Remote

Local

Page 13: 2014 MacAdmins Git Workshop

Git File ClassificationsTracked

Already in the repo

Ignored

Can exist but git ignores (specified in .gitignore)

Untracked

What's not tracked or ignored

Page 14: 2014 MacAdmins Git Workshop

Git File StatesModified

Changed but not yet committed

Staged

Modified filed marked to go into next commit

Committed

Safely stored in the repo

Page 15: 2014 MacAdmins Git Workshop

Git File States Lifecycle

Page 16: 2014 MacAdmins Git Workshop

Basic Git workflow

Working Directory

Staging Area

Repository

Page 17: 2014 MacAdmins Git Workshop

Example Workflow

git init

git add .

git commit -m “Added project files.”

git push

Page 18: 2014 MacAdmins Git Workshop

How Git Works

git init repo git add.

Creates .git folder in current

directory

1 2 3 git commit -m " "

Files committed to

local repo, ready to be

push to remote

git push (origin

master)

Files are pushed to repository

(final destination)

Add all files to be committed

Page 19: 2014 MacAdmins Git Workshop

Git Data IntegrityGit Checksums Everything!

Git uses SHA1 hash on all tracked files

It knows if anything has changed

Git excels at tracking renamed or moved files too

Resist modifying files when renaming or moving them

What about SHA1 hash collisions?

Page 20: 2014 MacAdmins Git Workshop

Git Help

Online docs are really, really good

http://git-scm.com

Command line help

git help

git help <conmand>

Page 21: 2014 MacAdmins Git Workshop

How to Install

git-scm.com/downloads

OS X: Terminal.app, Windows: Git Bash

Update to latest version

git clone https://github.com/git/git

Embedded with many Git GUI client apps

Page 22: 2014 MacAdmins Git Workshop

How to Configure

$ git config

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

Page 23: 2014 MacAdmins Git Workshop

How to Configure$ git config

Located in 3 places

/etc/gitconfig -- Values for all users

~/.gitconfig -- Values for single user

.git/config -- Value for single repository

$ git config --list

Page 24: 2014 MacAdmins Git Workshop

Configure Line EndingsEnd of line characters differ based on client 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 OS to be HAPPY

Page 25: 2014 MacAdmins Git Workshop

Line Ending Modescore.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

Page 26: 2014 MacAdmins Git Workshop

Line Endings on Windows

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

Page 27: 2014 MacAdmins Git Workshop

Line Endings on OS X

On OS X, enable auto convert CRLF to LF:

Page 28: 2014 MacAdmins Git Workshop

Creating Repositories

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

Page 29: 2014 MacAdmins Git Workshop

Ignoring Files

Git ignore files, paths specified in the .gitignore file

Specifies files/folders that are not tracked via Git

Wildcards

*NotForRepo*, *.dmg, etc.

10.9.4-Lab-Image.dmg

LargeDataFile-NotForRepo.db

Page 30: 2014 MacAdmins Git Workshop

Status of File ChangesHow do I know what has changed?

$ git status

Status reports different states

Untracked

Modified

Staged

Page 31: 2014 MacAdmins Git Workshop

DifferencesCommits are all about what is … different!

Viewing the differences

At the command line

$ git diff FileName

In GUI Apps

Kaleidoscope, FileMerge, Changes.app, etc.

Page 32: 2014 MacAdmins Git Workshop

Staging Files

Added files are staged

Why not just directly commit the changes?

git add -v <file/folder_name>

git add -v .

git add -A

Page 33: 2014 MacAdmins Git Workshop

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

Page 34: 2014 MacAdmins Git Workshop

Committing ChangesThis 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>

What's the most important part of the above command?

Page 35: 2014 MacAdmins Git Workshop

Pushing to Remote Repos

Local versus Remote (Hosted) Repos

Commit changes to local repo 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

Page 36: 2014 MacAdmins Git Workshop

Accessing Remote ReposNetwork Based

HTTPS

SSH

Git Protocol (daemon)

Multiple Remotes

Fetch updates

Pruning Local Branches

Page 37: 2014 MacAdmins Git Workshop

Viewing Changes

Logs

$ git log --shows all pulls + info about each

Page 38: 2014 MacAdmins Git Workshop

Git LogUsed for git reset

Page 39: 2014 MacAdmins Git Workshop

Git Log

List changes made in a commit

git log

View entire change log

$ git log -p -2

Lists changes in last two commit entries

Page 40: 2014 MacAdmins Git Workshop

Reverting Changesgit reset

Change back to file state at specific commit

git revert

Creates a new commit and undoes last changes

git checkout <commit> -- <filename>

Changes branch you’re currently working on

Page 41: 2014 MacAdmins Git Workshop

Hands On Git DemoCommand Line

Install

Configure

Create new repo

Add to .gitignore

Add files

Commit changes

Add remote

Push Items

Revert Items

Clone Existing Repo

Page 42: 2014 MacAdmins Git Workshop

GUI Clients

SourceTree (OS X and Windows)

GitHub (OS X and Windows)

Tower (OS X)

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

Page 43: 2014 MacAdmins Git Workshop

Diff GUI Applications

FileMerge

Changes.app

Kaleidoscope is totally AWESOME!

Page 44: 2014 MacAdmins Git Workshop

SourceTree In Depth DemoInstall

Configure

.gitignore

Create New Repo

Adding Items

Commit Items

Push Items

Revert Items

Clone Existing Repo

Page 45: 2014 MacAdmins Git Workshop

SourceTree In Depth DemoBookmarks List

Recommended Git Defaults

Line Endings

Host Repositories Feature

Working Directory, Branches, Searching

Excellent Git Flow Integration

Page 46: 2014 MacAdmins Git Workshop

Kaleidoscope Demo

Using Kaleidoscope for diffs, conflicts, merges

Excellent support for Git and SourceTree

Page 47: 2014 MacAdmins Git Workshop

Part II - Advanced GitFetching

Tags

Branches

Merging

Origin (Remotes)

HEAD Reference

Pushing to Remotes

Automate Authentication

Advanced Git Ignores

Stashing

Advanced Diffs

Searching

Moving Files

Bisect

Pull Requests

Page 48: 2014 MacAdmins Git Workshop

Fetching

Get updates from remote repo

Does not apply changes to local working directory

Useful to run before doing a pull

$ git fetch origin

Page 49: 2014 MacAdmins Git Workshop

TagsUseful for marking a specific commit

Example: Mark commit that is for major version change

1.0.1

1.0.2

Useful for regression testing with the ‘git bisect’ command

Page 50: 2014 MacAdmins Git Workshop

Tags

Create a tag syntax

$ git tag <commit hash ID>

Page 51: 2014 MacAdmins Git Workshop

Branches

“master” branch is the default

Helps to separate lines of development

Ex: Master, Hotfix, Development, Feature, Release

Merging of branches is where Git excels

“Git Flow” is a popular branching model

Page 52: 2014 MacAdmins Git Workshop

BranchesCreate and switch to new branch

$ git checkout -b <branch>

Checkout (switch to) branch

$ git checkout master

Delete branch

$ git branch -d <branch>

Page 53: 2014 MacAdmins Git Workshop

Deleting Branches

Delete Remote Branch

$ git push origin —delete <branchname>

Don’t forget to delete all stale remote-tracking branches!

$ git remote prune origin

Otherwise your local repo will still show it

Page 54: 2014 MacAdmins Git Workshop

Merging Changes

Demo of the merge process

Conflicts

Cherry Picking

Show how to change diff tool to GUI app

Page 55: 2014 MacAdmins Git Workshop

Merging Changes

Use FileMerge for merging conflicts

$ git mergetool -t opendiff

Page 56: 2014 MacAdmins Git Workshop

Remotes

Default is called "origin" (leave it this name)

Versus Local Repo

Multiple

Primary, off site, etc.

Be careful that local branches still track the primary repo branch after adding more remotes

Page 57: 2014 MacAdmins Git Workshop

Automate Authentication

SSH Remote Host

Create Open SSH Key pair, place id_rsa.pub key on remote host

HTTPS

Credential Helpers for command line

SourceTree and other GUI apps manage this well

Page 58: 2014 MacAdmins Git Workshop

Adding a Remote Repo

$ git remote add origin ssh://user@host/path/to/repo.git

Page 59: 2014 MacAdmins Git Workshop

HEAD Reference

HEAD is a reference to branch or a specific commit

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

Working directory state points to specific commit and not latest commit on a branch

Page 60: 2014 MacAdmins Git Workshop

Pushing Changes

git push origin master

Pushes all your commits on master branch to the origin remote

Push to other locations

git push

Page 61: 2014 MacAdmins Git Workshop

Advanced Git IgnoresGlobal Git Ignore File in ~/.gitignore

Git tracks files, not directories

By default, Git does not track empty directories

Page 62: 2014 MacAdmins Git Workshop

Add Empty Dir to RepoCreate a .gitignore file in the directory to ignore

Ignore all files except the .gitignore file

Page 63: 2014 MacAdmins Git Workshop

Stashing ChangesVery 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

Stash the changes (makes working directory clean)

$ git stash

Restore the changes

$ git stash pop

Page 64: 2014 MacAdmins Git Workshop

Advanced Diffs

Logging Changes of one file

Tags

git diff <tag1> <tag2>

Branches

git diff branch1..branch2

Page 65: 2014 MacAdmins Git Workshop

Searching

Search file names, commit messages

$ git log -S<string>

RegEx for commit messages

$ git log -G”RegEx Pattern Here”

Page 66: 2014 MacAdmins Git Workshop

Who Made Changes?

List who modified what files

git blame -l FileName.txt

git blame -l —show-email FileName.txt

Page 67: 2014 MacAdmins Git Workshop

Moving Files Demo

Git excels at moving files

When moving or renaming files do NOT modify them for most efficient commit

Recall that everything is checksummed, Git knows if a file is the same one regardless if the name changed

Page 68: 2014 MacAdmins Git Workshop

Hunting For Bugs

Git provides a very handy function to help find which commit introduced a bug

You tell Git what commit point was good and bad

$ git bisect start

$ git bisect bad # Current version is bad

$ git bisect good v2.6.13-rc2

Page 69: 2014 MacAdmins Git Workshop

Advanced Demo

Detached HEAD

Configure Remote

Push Changes to remote

Moving Files, Stashing

Branching

Merging, Tagging

Diffs

Searching

GitFlow

Bisect

Page 70: 2014 MacAdmins Git Workshop

Repo Hosting ServicesGitHub, BitBucket, Google Code

CodeSpaces, SourceRepo

Assembla, Gitorious

GitLab

Git.psu.edu for Penn Staters

Lots more!

Page 71: 2014 MacAdmins Git Workshop

Self-Hosted Repos

SSH

Atlassian Stash

Enterprise GitHub

GitLab

Page 72: 2014 MacAdmins Git Workshop

Git FlowWorkflow for managing branches

Five different branch types

Feature

Develop

Release

Hotfixes

Master

Page 73: 2014 MacAdmins Git Workshop

Removing Tracked Files

$ git rm —cached <FileName>

Update repo .gitignore to ignore the file(s)

Page 74: 2014 MacAdmins Git Workshop

Removing Untracked Files

$ git status

$ git clean -n

Dry run, shows what would be deleted

$ git clean -f

Removes the files listed in previous command

Page 75: 2014 MacAdmins Git Workshop

Tips and Tricks

Aliases

Can be used to shorten commands

Pros and Cons on shared systems

Auto-Completion

Auto completes Git commands; like Linux

Page 76: 2014 MacAdmins Git Workshop

Best Practices

Handling Very Large Repos

run `git gc` every 100 or so commits

http://blogs.atlassian.com/2014/05/handle-big-repositories-git/

Simple solution is a shallow clone

git clone --depth depth remote-url

Page 77: 2014 MacAdmins Git Workshop

What's New in Git 2.0

Default push changes

Simple versus matching

git push (with no arguments) defaults to simple push mechanics

git add now includes removals to the repo

Pre 2.0 only added new or modified files

Page 78: 2014 MacAdmins Git Workshop

Lessons Learned

Write good commits messages!

Really helps when writing release notes

Easy to search

Helps future self!

Page 79: 2014 MacAdmins Git Workshop

Lessons Learned

Take the time to write good commit messages when they are fresh in your mind

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

Page 80: 2014 MacAdmins Git Workshop

Lessons LearnedSubmodules and dependencies

Branch Names

No White Space Permitted (tabs, spaces)

Supports Hierarchical Names*

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

$ git show-branch ‘bug/*’

Page 81: 2014 MacAdmins Git Workshop

Lessons Learned

Tags

Use "annotated" most of the time

Working with others

Team collaboration tips

Page 82: 2014 MacAdmins Git Workshop

Lessons LearnedStart small

Pick one project to implement Git

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

Watch commits on GitHub, fork a project, create pull requests

Page 83: 2014 MacAdmins Git Workshop

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

Page 84: 2014 MacAdmins Git Workshop

Further Reading

Cherry Picking

Patches

Reflog

Rebasing

Page 85: 2014 MacAdmins Git Workshop

ResourcesGit Docs

http://git-scm.com/documentation

Git cheat sheets

Lynda.com (lynda.psu.edu)

Git Essential Training, GitHub for Web Designers

Fundamentals of Software Version Control

Page 86: 2014 MacAdmins Git Workshop

Resources

Atlassian Git Tutorials

https://www.atlassian.com/git/tutorial

https://try.github.io

O'Reilley Books

“McCullough and Berglund on Mastering Git”

Page 87: 2014 MacAdmins Git Workshop

Thank You!

Page 88: 2014 MacAdmins Git Workshop

Please submit Session Feedback!

http://j.mp/psumac9

Page 89: 2014 MacAdmins Git Workshop

Q & AJustin Elliott

[email protected]

Scott Gallagher [email protected]