git down, git on up: practical examples of intermediate git techniques
Embed Size (px)
TRANSCRIPT

GIT DOWN, GIT ON UPPRACTICAL EXAMPLES OF INTERMEDIATE GIT TECHNIQUES
Siena AguayoIndiegogo Software Engineer & Hackbright AlumnaApril 23rd, 2014

ACKNOWLEDGMENTS
@the_zenspider for the title of this talk

MERGING VS. REBASING

THINGS TO KNOW• Local copy: a somewhat-secret copy of the files of the
remote repository that lives in the .git folder of your repo
• Working tree/working directory: the files you are actually modifying in your text editor

GIT MERGE
Did you know…?
$ git pull is actually two commands:
$ git fetch
$ git merge FETCH_HEAD
$ git fetch updates your local repository (not your working tree) with changes from the remote branch, then $ git merge merges your local repo with the changes you have made to your working tree, creating a new commit that has two parents.

GIT MERGE
A---B---C topic
/
D---E---F---G master
Becomes…
A---B---C topic
/ \
D---E---F---G---H master
H is a new, merge commitwith two parents!
C G
H

GIT REBASE
(my_feature_branch) $ git rebase master
Replays your changes on top of master so that your commits are always on top. Does not create a merge commit and your history is streamlined.
But remember $ git pull fetches then merges…?
$ git pull –-rebase will fetch and then rebase instead!

GIT REBASE
A---B---C topic
/
D---E---F---G master
Becomes…
A'--B'--C' topic
/
D---E---F---G master No new commit, history is in a single line (each commit only has one parent)

INTERACTIVE REBASE TO THE RESCUE
commit 9f82kh
Author: Siena Aguayo [email protected]
Omg I fixed it
commit jf8djb
Author: Siena Aguayo [email protected]
What is happening
commit 0g1bdi
Author: Siena Aguayo [email protected]
Stuff is broken
Does your commit history look like this?
$ git log

INTERACTIVE REBASE TO THE RESCUE
$ git rebase –i HEAD~3
vim
pick 9f82kh Stuff is broken
pick jf8djb What is happening
pick 0g1bdi Omg I fixed it

INTERACTIVE REBASE TO THE RESCUE
$ git rebase –i HEAD~3
vim
pick 9f82kh Stuff is broken
squash jf8djb What is happening
squash 0g1bdi Omg I fixed it

INTERACTIVE REBASE TO THE RESCUE
vim
# This is a combination of 4 commits.
# The first commit's message is:
…
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

INTERACTIVE REBASE TO THE RESCUE
vim
I fixed that thing and here is a wonderfully detailed commit message.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

IF YOU SCREW IT UP
$ git reflog
e94a244 HEAD@{0}: checkout: moving from master to gittalk
e94a244 HEAD@{1}: rebase -i (finish): returning to refs/heads/master
e94a244 HEAD@{2}: rebase -i (start): checkout HEAD~2
e94a244 HEAD@{3}: cherry-pick: updated readme to include link to Vimeo video

OTHER GIT COMMANDS

GIT STASH
Allows you to push your current changes onto a stash stack so that you can do something (like pull or checkout) without committing your changes yet
$ git stash save “working on some stuff”
$ git stash list
stash@{0}: On master: working on some stuff
stash@{1}: WIP on (no branch): 8684246
stash@{2}: On master: stuff
stash@{3}: On master: acceptance tests for that thing
$ git stash apply stash@{2}
$ git stash pop

GIT CHERRY-PICK
Take a commit from another branch and stick it into your current branch
Commit 9283fj is in some other branch
$ git checkout master
$ git cherry-pick 9283fj
Now it’s in master too!

Uses binary search to help you figure out what commit introduced a bug
$ git bisect start
$ git bisect good 9193jfp
$ git bisect bad 09480f
…
$ git bisect reset
GIT BISECT
good
bad
Good or bad?

OTHER GIT RESOURCES

GIT PAIR
A nifty little script from Pivotal Labs that lets you add multiple authors to a commit. Just add author info to a text file.
$ git pair sa sc
global: user.name Siena Aguayo
local: user.name Siena Aguayo & Stella Cotton
NOTE: Overriding global user.email setting with local.
global: user.email [email protected]
local: user.email [email protected]
local: user.initials sa sc

BASH SCRIPTS
git-completion
• Tab completion for git commands, branch names, etc.
git-prompt
• Show which branch you are on in your prompt, e.g.siena@Siena-MBP ocr-jpn (master) $

GITHUG
Play a fun game to take your Git skills to the next level!
Just clone the repo and run $ githug play
https://github.com/Gazler/githug
