Changing History, or How to Git pretty

@bobthecow

Presentate

Changing History

or, How to Git pretty

Lesson 1: Code is messy

what with all this branching.

1 week in the life of OpenSky

It's not that different from many open source projects

Turn
it on
its
side

git-flow

A codified Git branching model that works

I only care about 3 branches

There's more…

GitHub Flow

Kinda like git-flow

… but optimized for continuous development

Pull Requests are
the big awesome

GitHub has an amazing code review system called Pull Requests that I fear not enough people know about…

Scott Chacon, GitHub

Actually, we use it more as a branch conversation view more than a pull request.

Scott Chacon, GitHub

Again, RTFA

Lesson 2: Pretty pull
requests get merged

You have made
something awesome

and you want to contribute

Beauty really does matter

Get your code merged first

Make the most of
your commit message

Capitalized, short (50 chars or less) summary

More detailed explanatory text, if necessary.  Wrap it to about 72
characters or so.  In some contexts, the first line is treated as the
subject of an email and the rest of the text as the body.  The blank
line separating the summary from the body is critical (unless you omit
the body entirely); tools like rebase can get confused if you run the
two together.

Write your commit message in the present tense: "Fix bug" and not "Fixed
bug."  This convention matches up with commit messages generated by
commands like git merge and git revert.

Further paragraphs come after blank lines.

 - Bullet points are okay, too

 - Typically a hyphen or asterisk is used for the bullet, preceded by a
   single space, with blank lines in between, but conventions vary here

 - Use a hanging indent

If you can't summarize your commit in 50 characters,
you're trying to cram two commits into one

Err on the side of bullets

Nobody likes a drive-by

Whitespace drive-by

Feature drive-by

Use gitx or `git add -p` to stage partial-file changesets

`git add .` and `git commit -a`
are the enemy of clean commits

Minimum viable pull requests

Lesson 3: How to
change history

So you have a mess
on your hands?

We can fix that

Revert, what?

It doesn't do what you think it does

Your last commit

can be `--amend`ed

$ git add myawesomefile.txt
$ git commit --amend
$ git commit --amend -m "Your message..."

Throw the last commit away

$ git reset --hard HEAD^

Bam. No more commit.

`reset` and re-commit

Take a mulligan on the last three commits:

$ git reset HEAD~3
$ git add … # etc, as usually

Rebase before (or after) opening your pull request

Remove merge commits:

$ git checkout mybranch
$ git fetch origin
$ git rebase origin/develop

Most other things require an interactive rebase:

$ git checkout mybranch
$ git fetch origin
$ git rebase -i origin/develop
pick b497aaf Move PO Box validation into Address object.
pick ca232ad Trying something
pick f3e498f Testing more
pick d876c5c slight simplification for address controller.

# Rebase 086a57c..d876c5c onto 086a57c
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
pick b497aaf Move PO Box validation into Address object.
reword ca232ad Trying something
fixup f3e498f Testing more
fixup d876c5c slight simplification for address controller.

# Rebase 086a57c..d876c5c onto 086a57c
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.

But wait, there's more!

`origin` doesn't like it
when you change history

You can use the `--force` to push

DANGER, Will Robinson.

Never, ever, ever use `git push --force` without specifying `git push --force origin mybranch`

Now, back to swimlanes

Don't pee in the pool

You should only change history

if (1) you are the only one affected,
or (2) you hate everyone downstream from you.

Wow.

That's a lot to keep straight.

Liked this talk? Share it!

  •   / 56

Published on , Last updated:

Share this presentation