gtfo: git theory for opensource

51
GIT THEORY FOR OPEN SOURCE by Forest Mars 11/4/30

Upload: forest-mars

Post on 17-May-2015

1.887 views

Category:

Technology


0 download

DESCRIPTION

Slides from a talk I gave in New York on Git in theory, Git in practice and Git-Drupal use cases.

TRANSCRIPT

Page 1: GTFO: Git Theory For OpenSource

GITTHEORYFOROPENSOURCEby Forest

Mars

11/4/30

Page 2: GTFO: Git Theory For OpenSource
Page 3: GTFO: Git Theory For OpenSource
Page 4: GTFO: Git Theory For OpenSource

“Using Git for web development is like Shopping for groceries in a Ferrari”

Page 5: GTFO: Git Theory For OpenSource
Page 6: GTFO: Git Theory For OpenSource

SCCS

SVC

Source

Code

Managment

Software

Configuration

Management

Concurrent

Versioning

System

Centralised

Version

System

Version

Control

System

CVSSCM

VCSSVSC

SCM

Source

Code

Control

System

Software

Version

Control

System

Version

Control

System

RCS

SCMS

Page 7: GTFO: Git Theory For OpenSource

Distributed

Version

Control

System

DVCS

Page 8: GTFO: Git Theory For OpenSource

Distributed

Revision

Control

System

DRCS

Page 9: GTFO: Git Theory For OpenSource
Page 10: GTFO: Git Theory For OpenSource

Speed

Lightweight

Distributed

Security

Code Integrity

Easy branching

Page 11: GTFO: Git Theory For OpenSource

GITTING STARTED

LINUX

$ sudo yum install git gitweb

$ sudo aptitude install git-core gitweb

OSX

$ sudo port install git-core

osx-git-installer:code.google.com/p/git-osx-installer/

Page 12: GTFO: Git Theory For OpenSource

Installing Git the Recommended Way:

# GPG (if you didn't have it already)curl ftp://ftp.gnupg.org/gcrypt/gnupg/gnupg-1.4.7.tar.bz2 tar xjcd gnupg-1.4.7./configuremakesudo make installcd ..

# GetTextcurl http://mirrors.usc.edu/pub/gnu/gettext/gettext-0.17.tar.gz tar xzcd gettext-0.17./configuremakesudo make installcd ..

Page 13: GTFO: Git Theory For OpenSource

Installing Git the Recommended Way:

# GITcurl http://kernel.org/pub/software/scm/git/git-1.5.5.tar.bz2 tar xjcd git-1.5.5./configuremakesudo make installcd ..curl http://www.kernel.org/pub/software/scm/git/git-manpages-1.5.5.tar.bz2 \sudo tar xj -C /usr/local/share/man

Page 14: GTFO: Git Theory For OpenSource

git config --global user.name "Forest Mars"

git config --global user.email [email protected]

git config --global user.xmpp“twitter.com/forestmars”

git config --global user.irc“kombucha”

$ git config --global color.status auto

$ git config --global color.branch auto

$ git config --global color.diff auto

$ git config --global color.interactive auto

Page 15: GTFO: Git Theory For OpenSource

git protocol

$ git clone git://github.com/drupal/drupal.git

http protocol

$ git clone http://github.com/drupal/drupal.git

Page 16: GTFO: Git Theory For OpenSource

git protocol

$ git clone git://git.drupal.org/project/drupal

http protocol

$ git clone http://git.drupal.org/project/drupal

Page 17: GTFO: Git Theory For OpenSource

PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

[forest@githubris gtfo (master)]:

Page 18: GTFO: Git Theory For OpenSource

$ git

usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path] [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [-c name=value] [--help] COMMAND [ARGS]

The most commonly used git commands are: add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List, create, or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository diff Show changes between commits, commit and working tree, etc fetch Download objects and refs from another repository grep Print lines matching a pattern init Create an empty git repository or reinitialize an existing one log Show commit logs merge Join two or more development histories together mv Move or rename a file, a directory, or a symlink pull Fetch from and merge with another repository or a local branch push Update remote refs along with associated objects rebase Forward-port local commits to the updated upstream head reset Reset current HEAD to the specified state rm Remove files from the working tree and from the index show Show various types of objects status Show the working tree status tag Create, list, delete or verify a tag object signed with GPG

$ git init

Page 19: GTFO: Git Theory For OpenSource

$ git add .

$ git commit -a -m “commit message”

$ git push

Page 20: GTFO: Git Theory For OpenSource
Page 21: GTFO: Git Theory For OpenSource
Page 22: GTFO: Git Theory For OpenSource

WORKING WITH REMOTES

$ git remote add origingit@host:repo-name.git

$ git remote add origin [email protected]:sandbox/forest/project.git

$ git remote add github [email protected]:forestmars/example.git

$ git remote -v

Page 23: GTFO: Git Theory For OpenSource

(What if I need to make my repo bare later?)

$ git config --bool core.bare true

Page 24: GTFO: Git Theory For OpenSource

Quite possibly the most useful slide in this deck

$ git describe

$ git status

$ git ls-files

$ git diff

$ git log --since=2.weeks

$ git foo –help

$ cat .git/config

Page 25: GTFO: Git Theory For OpenSource

Wait I was wrong, this is the really important one:

$ git commit --ammend

$ git checkout HEAD some_file

$ git revert HEAD^

$ git revert HEAD~1 -m 1

$ git reset --hard HEAD

$ git reset --hard [reflog-id]

Page 26: GTFO: Git Theory For OpenSource

DON'T LOSE YOUR HEAD

$ git checkout HEAD

$ git reset --hard HEAD

Page 27: GTFO: Git Theory For OpenSource

IN EVENT OF CATASTROPHIC FAILURE (or where's the Undo-Undo button?)

[forest@githubris gtfo (master)]$ git add somefile

[forest@githubris gtfo (master)]$ git commit -m "updated some file"[master 7ff5b1e] updated some file 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 somefile

[forest@githubris gtfo (master)]$ cat somefile contents of somefile

[forest@githubris gtfo (master)]$ git reset --hard HEAD^HEAD is now at 15dae3b somefile

[forest@githubris gtfo (master)]$ cat somefile cat: otherfile: No such file or directory

[forest@baird gtfo (master)]$ git reflog15dae3b HEAD@{0}: HEAD^: updating HEAD7ff5b1e HEAD@{1}: commit: updated some file

[forest@githubris gtfo (master)]$ git reset --hard 7ff5b1eHEAD is now at 7ff5b1e updated some file

[forest@githubris gtfo (master)]$ cat somefile contents of somefile

Page 28: GTFO: Git Theory For OpenSource

CLONE

FETCH

CHECKOUT

PRUNE

Page 29: GTFO: Git Theory For OpenSource

CLONE

FETCH

CHECKOUT

PRUNE

Page 30: GTFO: Git Theory For OpenSource

git clone --branch 7.x git://git.drupal.org/project/drupal.git ./git clone --branch 7.x git://git.drupal.org/project/drupal.git ./

Page 31: GTFO: Git Theory For OpenSource

$ git clone --branch master http://git.drupal.org:project/devel.git

$ git clone -b '6.x-3.x' git://git.drupal.org/project/admin_menu.git

$ git clone --branch 7.x git://git.drupal.org/project/some_module.git ./

$ cd some_module

$ (edit files)

$ git add (files)

$ git commit -m 'Explain what I changed'

$ git format-patch origin/master

Page 32: GTFO: Git Theory For OpenSource

$ git clone --branch master http://git.drupal.org:project/devel.git

$ git clone -b '6.x-3.x' git://git.drupal.org/project/admin_menu.git

$ git clone --branch 7.x git://git.drupal.org/project/some_module.git ./

$ cd some_module

$ (edit files)

$ git add (files)

$ git commit -m 'Explain what I changed'

$ git format-patch origin/master

Page 33: GTFO: Git Theory For OpenSource

CLONE

FETCH

CHECKOUT

PRUNE

$ got fetch origin [remote-branch]:[new-local-branch]

Page 34: GTFO: Git Theory For OpenSource

CLONE

FETCH

CHECKOUT

PRUNE

$ git checkout -b some_branch origin/some_branch

Page 35: GTFO: Git Theory For OpenSource

CLONE

FETCH

CHECKOUT

PRUNE

$ git remote prune -v [remote-repo]

Page 36: GTFO: Git Theory For OpenSource

B R A N C H I N G

Page 37: GTFO: Git Theory For OpenSource

$ git branch* master

$ git branch new_branch

$ git checkout -b new_branch [old_branch_name]

$ git branch -r -d origin/[new_branch_name]

$ git branch --track ... origin/...

Page 38: GTFO: Git Theory For OpenSource
Page 39: GTFO: Git Theory For OpenSource

$ git branch* master new_feature

$ git merge new_feature

$ git branch new_branch

$ git rebase

$ git pull --rebase

$ git rebase -i

$ git rebase

Page 40: GTFO: Git Theory For OpenSource

$ git push $ git push origin master

$ git push origin [branch-name]

$ git push [remote-repo] LOCALBRANCHNAME:REMOTEBRANCHNAME

default behavior of git is to push matching refs, so git push <remote-repo> would not push branch if it is not present on <remote-repo>

$ git push -f –all

Once the local branch is pushed to the remote, execute:

$ git --set-upstream [local-branch] origin/[branch-name]

to make the local branch track the remote branch.

$ git push -n --dryrun

$ git push REMOTENAME :BRANCHNAME

PUSH ME PULL YOU

Page 41: GTFO: Git Theory For OpenSource

$ git push upstream upstreammaster:master! [rejected] master -> master (non-fast forward)

$ git push origin :master

$ git push origin master

The Dirtiest Git Hack You Will Ever See

Page 42: GTFO: Git Theory For OpenSource
Page 43: GTFO: Git Theory For OpenSource
Page 44: GTFO: Git Theory For OpenSource

$ git clone [email protected]:sandbox/forest//1140078.git

Cloning into /1140078...

[email protected]'s password:

Page 45: GTFO: Git Theory For OpenSource

$ git clone [email protected]:sandbox/forest//1140078.git

Cloning into /1140078...

[email protected]'s password:

$ git clone [email protected] :sandbox/forest//1140078.git

Cloning into /1140078...

Page 46: GTFO: Git Theory For OpenSource

$ echo 'name = "My Awesome Module"' > 1140078.info

$ echo '<?php>' > my_awesome.module

Page 47: GTFO: Git Theory For OpenSource

$ git status On branch master

Initial commit

Untracked files: (use "git add <file>..." to include in what will be committed)

1140078.infomy_awesome.module

nothing added to commit but untracked files present (use "git add" to track)

Page 48: GTFO: Git Theory For OpenSource

$ git add .

$ git commit -m “initial commit”

$ git push origin master

Counting objects: 5, done.Delta compression using up to 4 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (3/3), 300 bytes, done.Total 3 (delta 2), reused 0 (delta 0)To [email protected]:sandbox/forest//1140078.git 1a8d48d..f6dcf5d master -> master

Page 49: GTFO: Git Theory For OpenSource

twitter.com/forestmarsT H A N X !

linkedin.com/in/forestmars

twitter.com/forestmars

about.me/forestmars

Page 50: GTFO: Git Theory For OpenSource

twitter.com/forestmars

Adding Bash Completion for Git on Mac OS X Snow Leopard

cd /tmp && git clone git://git.kernel.org/pub/scm/git/git.git

cd git && git checkout v`git --version | awk '{print $3}'`cp contrib/completion/git-completion.bash ~/.git-completion.bash

Cd ~ && rm -rf /tmp/gitecho -e "source ~/.git-completion.bash" >> .profile

sudo port install git-core +bash_completion

if [ -f /opt/local/etc/bash_completion ]; then . /opt/local/etc/bash_completionfi

Page 51: GTFO: Git Theory For OpenSource