2014 MacAdmins Git Workshop

Preview:

Citation preview

Git: From Start To FinishScott Gallagher spg14@psu.edu

Justin Elliott jde6@psu.edu

Session Feedback!

http://j.mp/psumac9

Part I - Git Basics

What is Version Control?

Why use it?

What is Git?

Git versus Subversion

Git Overview

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)

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 )

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

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

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!

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

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!

TerminologyRepository

Working Directory

Staging Area

Used for preparing commits

Git Directory

Repository object store/Remote Repository

Final destination for you data changes

Terminology

Working Directory

Index/ Staging

Directory/Repository

Local Local

Remote

Local

Git File ClassificationsTracked

Already in the repo

Ignored

Can exist but git ignores (specified in .gitignore)

Untracked

What's not tracked or ignored

Git File StatesModified

Changed but not yet committed

Staged

Modified filed marked to go into next commit

Committed

Safely stored in the repo

Git File States Lifecycle

Basic Git workflow

Working Directory

Staging Area

Repository

Example Workflow

git init

git add .

git commit -m “Added project files.”

git push

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

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?

Git Help

Online docs are really, really good

http://git-scm.com

Command line help

git help

git help <conmand>

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

How to Configure

$ git config

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

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

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

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

Line Endings on Windows

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

Line Endings on OS X

On OS X, enable auto convert CRLF to LF:

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

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

Status of File ChangesHow do I know what has changed?

$ git status

Status reports different states

Untracked

Modified

Staged

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.

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

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

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?

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

Accessing Remote ReposNetwork Based

HTTPS

SSH

Git Protocol (daemon)

Multiple Remotes

Fetch updates

Pruning Local Branches

Viewing Changes

Logs

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

Git LogUsed for git reset

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

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

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

GUI Clients

SourceTree (OS X and Windows)

GitHub (OS X and Windows)

Tower (OS X)

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

Diff GUI Applications

FileMerge

Changes.app

Kaleidoscope is totally AWESOME!

SourceTree In Depth DemoInstall

Configure

.gitignore

Create New Repo

Adding Items

Commit Items

Push Items

Revert Items

Clone Existing Repo

SourceTree In Depth DemoBookmarks List

Recommended Git Defaults

Line Endings

Host Repositories Feature

Working Directory, Branches, Searching

Excellent Git Flow Integration

Kaleidoscope Demo

Using Kaleidoscope for diffs, conflicts, merges

Excellent support for Git and SourceTree

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

Fetching

Get updates from remote repo

Does not apply changes to local working directory

Useful to run before doing a pull

$ git fetch origin

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

Tags

Create a tag syntax

$ git tag <commit hash ID>

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

BranchesCreate and switch to new branch

$ git checkout -b <branch>

Checkout (switch to) branch

$ git checkout master

Delete branch

$ git branch -d <branch>

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

Merging Changes

Demo of the merge process

Conflicts

Cherry Picking

Show how to change diff tool to GUI app

Merging Changes

Use FileMerge for merging conflicts

$ git mergetool -t opendiff

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

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

Adding a Remote Repo

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

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

Pushing Changes

git push origin master

Pushes all your commits on master branch to the origin remote

Push to other locations

git push

Advanced Git IgnoresGlobal Git Ignore File in ~/.gitignore

Git tracks files, not directories

By default, Git does not track empty directories

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

Ignore all files except the .gitignore file

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

Advanced Diffs

Logging Changes of one file

Tags

git diff <tag1> <tag2>

Branches

git diff branch1..branch2

Searching

Search file names, commit messages

$ git log -S<string>

RegEx for commit messages

$ git log -G”RegEx Pattern Here”

Who Made Changes?

List who modified what files

git blame -l FileName.txt

git blame -l —show-email FileName.txt

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

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

Advanced Demo

Detached HEAD

Configure Remote

Push Changes to remote

Moving Files, Stashing

Branching

Merging, Tagging

Diffs

Searching

GitFlow

Bisect

Repo Hosting ServicesGitHub, BitBucket, Google Code

CodeSpaces, SourceRepo

Assembla, Gitorious

GitLab

Git.psu.edu for Penn Staters

Lots more!

Self-Hosted Repos

SSH

Atlassian Stash

Enterprise GitHub

GitLab

Git FlowWorkflow for managing branches

Five different branch types

Feature

Develop

Release

Hotfixes

Master

Removing Tracked Files

$ git rm —cached <FileName>

Update repo .gitignore to ignore the file(s)

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

Tips and Tricks

Aliases

Can be used to shorten commands

Pros and Cons on shared systems

Auto-Completion

Auto completes Git commands; like Linux

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

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

Lessons Learned

Write good commits messages!

Really helps when writing release notes

Easy to search

Helps future self!

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

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/*’

Lessons Learned

Tags

Use "annotated" most of the time

Working with others

Team collaboration tips

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

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

Further Reading

Cherry Picking

Patches

Reflog

Rebasing

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

Resources

Atlassian Git Tutorials

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

https://try.github.io

O'Reilley Books

“McCullough and Berglund on Mastering Git”

Thank You!

Please submit Session Feedback!

http://j.mp/psumac9

Q & AJustin Elliott

jde6@psu.edu

Scott Gallagher spg14@psu.edu