How to Really Get Git

57
How to Really Get How to Really Get Git Git Sunday, February 8, 2015 Susan Tan @ArcTanSusan

Transcript of How to Really Get Git

Page 1: How to Really Get Git

How to Really GetHow to Really GetGitGit

Sunday, February 8, 2015Susan Tan

@ArcTanSusan

Page 2: How to Really Get Git

Who am I?

Software Engineer @Piston who uses Python and gitA New-Yorker-who-moved-to-San-FranciscoAlso Piston is .hiring

Page 3: How to Really Get Git
Page 4: How to Really Get Git

I. Git SetupI. Git Setup

Page 5: How to Really Get Git

Git Configs: AliasesGit Configs: Aliases

[alias] st = status ci = commit br = branch co = checkout df = diff lg = log -p

$ git st# On branch master# Untracked files:# (use "git add <file>..." to include in what will be committed)## _posts/2009-02-06-helpful-command-aliases.textilenothing added to commit but untracked files present (use "git add" to track)

In ~/.gitconfig, put this here:

Page 6: How to Really Get Git

Git Configs: ColorsGit Configs: Colors

[color "status"] added = magenta changed = yellow untracked = cyan

In ~/.gitconfig, put this:

Page 7: How to Really Get Git

Git Configs:Git Configs:

Tab AutocompletionTab Autocompletion1. Follow instructions for your OS.2. Then you can use auto tab completion on long commands

here

$ git chec<tab>

Page 8: How to Really Get Git

Git Configs:Git Configs:

A Useful Bash PromptA Useful Bash Prompt

https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion

A useful prompt shows

current branchcurrent directorycurrent virtualenvstatus of working directory

Page 9: How to Really Get Git

Who is this talk for?You already know the basics of git, but you still makemistakes (well, so does everyone)

Common git commands you already know:git addgit status git push origingit fetch origin

You want to know how to deal with mistakesCommon git commands you'll learn about:

git rebasegit resetgit refloggit revert

Page 10: How to Really Get Git

This Should Look FamiliarThis Should Look Familiar

Source: http://git-scm.com/book/ch1-3.html

Page 11: How to Really Get Git

This should be familiar, tooThis should be familiar, too

Page 12: How to Really Get Git

This may be familiar, tooThis may be familiar, too

Page 13: How to Really Get Git

This should be familiar, too -- Collaboration with git.This should be familiar, too -- Collaboration with git.

Source: http://nvie.com/posts/a-successful-git-branching-model/

Page 14: How to Really Get Git

II. Mistakes and howII. Mistakes and howto fix themto fix them

"People think computers will keep them from making mistakes.They're wrong. With computers you make mistakes faster."--Adam Osborn

Page 15: How to Really Get Git

Source: http://justinhileman.info/article/git-pretty/git-pretty.pdf

Page 16: How to Really Get Git

git rebasegit rebaseWhy use rebase?

1. git rebase to keep a branch updated

2. git rebase --interactive flag to change history

Page 17: How to Really Get Git

git rebase: The Simplest Usagegit rebase: The Simplest Usage

Note: "master" here means remote upstream's master branch.

Page 18: How to Really Get Git

git rebase: The Simplest Usagegit rebase: The Simplest Usage

Page 19: How to Really Get Git

What if your personal branch "develop" needs to rebaseWhat if your personal branch "develop" needs to rebase

against a feature branch shared by your team members?against a feature branch shared by your team members?

Note: "master" here means remote upstream repo's master branch.

Page 20: How to Really Get Git

What if your personal branch "develop" needs to rebaseWhat if your personal branch "develop" needs to rebase

against a feature branch shared by your team members?against a feature branch shared by your team members?

Note: "master" here means remote upstream repo's master branch.

Page 21: How to Really Get Git

git rebase master/feature develop

git rebase --onto master feature develop

Note: "master" here means remote upstream repo's master branch.

OR

git rebasegit rebase

git rebase <remote_repo>/<remote_branch> <your_branch_name>

git rebase --onto <first_this> <then_this> <last>

Syntax for git rebase

Page 22: How to Really Get Git

What if the feature branch needs to rebase against aWhat if the feature branch needs to rebase against a

upstream repo's master branch shared by everyone?upstream repo's master branch shared by everyone?

Note: "master" here means remote upstream repo's master branch.

Page 23: How to Really Get Git

What if the feature branch needs to rebase against aWhat if the feature branch needs to rebase against a

upstream repo's master branch shared by everyone?upstream repo's master branch shared by everyone?

Note: "master" here means remote upstream repo's master branch.

Page 24: How to Really Get Git

Rebase ConflictsRebase Conflicts

Page 25: How to Really Get Git

Rebase Conflicts require manual fixesRebase Conflicts require manual fixes(because git isn't clever enough to fix conflicts)

1. Locate conflict markers (<<<<<<) andmake edits to resolve

2. Resolve a conflict in each file with gitadd <filename>

3. git rebase --continue4. Alternatively, you can undo the git

rebase with git rebase --abort

Page 26: How to Really Get Git

Fixing git rebase conflicts is notFixing git rebase conflicts is notalways the right solutionalways the right solutiongit checkout mastergit branch -D featuregit fetch mastergit checkout -b feature master/featuregit checkout -b develop_v2git merge develop_v1git branch -D develop_v1

git fetch mastergit checkout [YOUR BRANCH]git rebase --onto master/feature [commit]

OR

Page 27: How to Really Get Git

Different computers, sameDifferent computers, samebranchbranch

git fetch origin && git rebase origin/<MY_BRANCH>

Note: "origin" is your personal cloned copy of an upstream masterrepo

git pull --rebase

Or, equivalently

Page 28: How to Really Get Git
Page 29: How to Really Get Git

git rebase --interactive <HASH> orgit rebase --interactive HEAD^

Defn: To re-order, edit, squash, or remove manycommits at once

Page 30: How to Really Get Git

git rebase -i HEAD~5

Page 31: How to Really Get Git

Squashing commitsSquashing commits

Page 32: How to Really Get Git

git resetgit reset

Defn: Reset current HEAD to the specified state

Page 33: How to Really Get Git

git reset --soft HEAD^

Defn: Set files from previous commit onto staging area. HEADpoints to the previous commit. This leaves all your changed filesas "Changes to be committed".

Page 34: How to Really Get Git

git reset --soft HEAD^

Page 35: How to Really Get Git

git reset --hard HEAD^

Defn: Abandon everything since your last commit. HEAD pointsto the previous commit. Nothing is in staging. Use with greatcaution

Page 36: How to Really Get Git

git refloggit reflog

Defn: shows all the commits and actions on your machine that hasever happened such as branch switches, rebases, resets, branchdeletions, cherry-picks, reverts.

Page 37: How to Really Get Git

git reflog git reflog --grep="Revert"

How to make git reflog more usefulHow to make git reflog more useful

git reflog --all --date=iso

Page 38: How to Really Get Git

You can undo a hard reset with the help of git reflogYou can undo a hard reset with the help of git reflog

git reset --hard HEAD^^ to remove the last 3 commits

Page 39: How to Really Get Git

You can undo a hard reset with git reflog + git reset --hardYou can undo a hard reset with git reflog + git reset --hard

git reset --hard a64b0f2 to restore branch to previous stategit reset --hard a64b0f2 to restore branch to previous state

Page 40: How to Really Get Git

Why use git reflog?Why use git reflog?

You lost commits when you did a git reset --hardYou squashed too many commits into 1 commit, so youwant to undo git rebase -iYou accidentally deleted branches and want to restorethem

Summary: Use reflog to look up old lost commits

Page 41: How to Really Get Git

Use cases for git reflogUse cases for git reflogLook up a commit hash. Then either

create a new (un-named) branch out of this commitgit checkout -b <COMMIT_HASH>

reset your branch to a previous stategit reset --hard <COMMIT_HASH>git reset --soft <COMMIT_HASH>

cherrypick that lost commit on top of your branchgit cherry-pick <COMMIT_HASH>

Page 42: How to Really Get Git

How long does theHow long does theoutput of reflog last?output of reflog last?

Hanging commits are removed from the local repository bygarbage collection (gc) or by manual removal. Default is 30days.

[gc] reflogExpire = never reflogExpireUnreachable = never

Page 43: How to Really Get Git

git revert 638351801cd802d6e0c4e601db1eca801dd58936

Defn: Makes a revert commit with a message that states thespecified commit is reverted

git revert <HASH>

Page 44: How to Really Get Git

Defn: Revert changes specified by 4th last commit awayfrom HEAD and create a new commit with reverted changes.

git revert HEAD~3

Page 45: How to Really Get Git

git revert --continueContinue after resolving conflicts git revert --abortCancel operation and return to pre-sequence state git revert --quitCan be used to start over after a failed cherry-pick or revert

Git revert conflictsGit revert conflicts

Page 46: How to Really Get Git

When to use git reset vs. git revertWhen to use git reset vs. git revert

Page 47: How to Really Get Git

Permanent MistakesPermanent Mistakes

Page 48: How to Really Get Git

Permanent MistakesPermanent Mistakes

git push -f origin <REMOTE>

Page 49: How to Really Get Git

SummarySummaryIf you run into git problems, the solution is most likely touse one of these --

git rebasegit resetgit refloggit revert

Page 50: How to Really Get Git

III. Automate gitIII. Automate git

Page 51: How to Really Get Git

Problem: Repetitive branch deletingProblem: Repetitive branch deleting

vagrant@precise64:~/work/savage$ git push origin :73997184To [email protected]:onceuponatimeforever/savage.git - [deleted] 73997184vagrant@precise64:~/work/savage$ git push origin :76321732-on-masterTo [email protected]:onceuponatimeforever/savage.git - [deleted] 76321732-on-mastervagrant@precise64:~/work/savage$ git push origin :77910316 To [email protected]:onceuponatimeforever/savage.git - [deleted] 77910316vagrant@precise64:~/work/savage$ git push origin :77910316-bottleTo [email protected]:onceuponatimeforever/savage.git - [deleted] 77910316-bottlevagrant@precise64:~/work/savage$ git push origin :77910316-bottle-from-scratchTo [email protected]:onceuponatimeforever/savage.git - [deleted] 77910316-bottle-from-scratchvagrant@precise64:~/work/savage$ git push origin :2.0/master

Page 52: How to Really Get Git

IPython notebook supports gitIPython notebook supports git

Page 53: How to Really Get Git

Solution: IPython notebook scriptSolution: IPython notebook script

Page 54: How to Really Get Git

Problem: Fetch & rebase repetitionsProblem: Fetch & rebase repetitions

cd <PROJECT>git fetch upstreamgit rebase upstream/master

cd ../<ANOTHER_PROJECT>git fetch upstreamgit rebase upstream/master

# And again....

Page 55: How to Really Get Git

Solution: IPython notebook scriptSolution: IPython notebook scriptlist_of_projects = ['ipython', 'oh-mainline']

def update(project): %cd {project} !git checkout master !git fetch upstream !git rebase upstream/master !git push origin master %cd .. print "Done git rebasing this repoo: ", {project} for project in list_of_projects: update(list_of_projects[project]) print "---------------------------------"

Page 56: How to Really Get Git

Solution: IPython notebook scriptSolution: IPython notebook script

Page 57: How to Really Get Git

Thanks! Q&A Time.Thanks! Q&A Time.