Real-World Git

18
Real-World Git Stuff We Didn’t Cover Last Time

Transcript of Real-World Git

Page 1: Real-World Git

Real-World GitStuff We Didn’t Cover Last Time

Page 2: Real-World Git

Stuff to Cover

1. Migrating from Subversion

2. Branching strategies

3. Branch-management commands

a. Merge

b. Rebase

c. Interactive rebase

d. Cherry pick

Page 3: Real-World Git

Migrating from Subversion

Three aspects:

1.Import commits

2.Clean up metadata

a. Authors

b. Tags

3.eHarmony infrastructure

Best practice:Repository per artifact

Page 4: Real-World Git

Easier Waygit svn clone <subversion_url>

• Built into git, brings in all commits and branches

• Can import Subversion subdirectories

http://git-scm.com/book/en/Git-and-Other-Systems-Migrating-to-Git

• Authors looks like this in history:

rwarren <rwarren@4c93b258-373f-11de-be05-5f7a86268029>

• Subversion tags become Git branches

Page 5: Real-World Git

More Complete Way

1. Get svn2git from https://github.com/nirvdrum/svn2git

2. Build list of authors in your history

• Manually, or:

svn log ^/ --xml | grep -P "^<author" | sort -u | perl -pe 's/<author>(.*?)<\/author>/$1 = /'

Page 6: Real-World Git

More Complete Way

3. Import: svn2git <subversion_url> <local_dir> --authors <authors_file>

https://wiki.corp.eharmony.com/display/MAT/Matching+SVN+to+Git+migration

• Subversion tags are recognized as Git tags

• Authors look like this in the history:

Rick Warren <[email protected]>

Page 7: Real-World Git

Either Way1. Push to Git repository:

$ git remote add origin <git_url>

$ git push origin --all

$ git push origin --tags

2. Set up eHarmony stuff

a. Add build/release jobs in Jenkins

b. Add repositories to Crucible

Page 8: Real-World Git

Branching Strategies

Remember:

• Everything is a branch. It ain’t no thing, y’all.

• You can rename or rebase your branch if you screw up (but you’ll cause pain to your team)

• Stuff happens: keep one branch releasable.

Page 9: Real-World Git

What We Do in Matching

• Release from master, and keep it releasable

• Do stories on branches: “MAT-1234_feature”

• When master released, merge to branch

• Preliminary QA acceptance on branch

• Merge to master, release, accept again

Page 10: Real-World Git

Fancier: Git Flow

• master == latest release

• develop == latest integration, accumulating merges until next release

• Specialized branches, off develop:features, hot fixes, release stabilization

• Built directly into some Git clients

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

Page 11: Real-World Git

Rebasing• Replay the commits of one branch on top of another

branch

• Use as an alternative to merge, if you value “clean” history

http://git-scm.com/book/ch3-6.html

before after

Page 12: Real-World Git

Interactive RebasingDo any of these apply to you?

• Big, coarse commits: you fear sharing changes too early.

• Lots going on in your working copy: you lose track of what you’ve changed, and occasionally revert a file by accident.

• Five minutes after committing, you realize you missed something.

Page 13: Real-World Git

Solution: Interactive Rebase

• Commit early and often, to checkpoint each fine-grained change.

• Wait to push until ready to share.

• Squash and reorder unshared commits in the mean time.

Page 14: Real-World Git

older

newer

git rebase -i

$ git rebase -i HEAD~5

In editor:pick 2174c26 MAT-1234: Pruned dependencies

pick 4d1d456 MAT-5678: Improved validation

pick aedba8f MAT-9876: Updated API

pick 6727eca MAT-5432: Removed cruft

pick 0afb08b MAT-1098: Fixed bean name

Page 15: Real-World Git

git rebase -ipick: Keep commitreword: Change messagesquash: Combine with previous

pick 2174c26 MAT-1234: Pruned dependencies

pick 4d1d456 MAT-5678: Improved validation

pick aedba8f MAT-9876: Updated API

pick 6727eca MAT-5432: Removed cruft

pick 0afb08b MAT-1098: Fixed bean name

Page 16: Real-World Git

Word of Warning• Commits are immutable

• Rebasing doesn’t delete old commits; it just makes new ones

• Rebasing can’t remove those old commits from others’ local repositories

• Don’t rebase any commits that you’ve pushed

Page 17: Real-World Git

Cherry Pick

• Copy commit(s) from history onto current branch

• Leaves source branch alone

• Use when merging is impossible or premature

http://git-scm.com/docs/git-cherry-pick

Page 18: Real-World Git

Please Enjoy Git Responsibly