23
GIT DOWN, GIT ON UP PRACTICAL EXAMPLES OF INTERMEDIATE GIT TECHNIQUES Siena Aguayo Indiegogo Software Engineer & Hackbright Alumna April 23 rd , 2014

Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

Embed Size (px)

Citation preview

Page 1: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

GIT DOWN, GIT ON UPPRACTICAL EXAMPLES OF INTERMEDIATE GIT TECHNIQUES

Siena AguayoIndiegogo Software Engineer & Hackbright AlumnaApril 23rd, 2014

Page 2: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

ACKNOWLEDGMENTS

@the_zenspider for the title of this talk

Page 3: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

MERGING VS. REBASING

Page 4: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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

Page 5: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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.

Page 6: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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

Page 7: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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!

Page 8: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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)

Page 9: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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

Page 10: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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

Page 11: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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

Page 12: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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.

Page 13: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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.

Page 14: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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

Page 15: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

OTHER GIT COMMANDS

Page 16: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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

Page 17: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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!

Page 18: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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?

Page 19: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

OTHER GIT RESOURCES

Page 20: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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

Page 21: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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) $

Page 22: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques

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

Page 23: Git Down, Git On Up: Practical Examples of Intermediate Git Techniques