Git talk from Open 2011 conference in Israel

52
Git Reuven M. Lerner • [email protected] Open Israel Conference June 30 th , 2011

Transcript of Git talk from Open 2011 conference in Israel

Page 1: Git talk from Open 2011 conference in Israel

GitReuven M. Lerner • [email protected]

Open Israel ConferenceJune 30th, 2011

Page 2: Git talk from Open 2011 conference in Israel

Who am I?

• Web developer, software architect, consultant, lecturer/trainer

• Linux Journal columnist since 1996

• Mostly Ruby on Rails + PostgreSQL, but also Python, PHP, jQuery, and lots more...

• Git user (and enthusiast) since 2008

Page 3: Git talk from Open 2011 conference in Israel

What is version control?

Page 4: Git talk from Open 2011 conference in Israel

Time machine

• Return to any point in a project’s history

• Find out how files, folders have changed

• Learn which user made which changes

Page 5: Git talk from Open 2011 conference in Israel

Backup

• Never fear that you’ve deleted a file — just rewind the history

• Everything is recorded, can be undone

Page 6: Git talk from Open 2011 conference in Israel

Maintenance tool

• “Tag” releases

• Who is to blame for a particular bug?

• When did we introduce a particular problem?

• Deploy specific versions on specific servers

Page 7: Git talk from Open 2011 conference in Israel

Scratchpad

• Try out new ideas

• Experiment with alternative techniques

• Upgrade a library

• Refactor without affecting the “live” code base

Page 8: Git talk from Open 2011 conference in Israel

Collaboration tool

• Work without fear of “stomping” on someone else’s code

• Identify conflicts between what developers wrote

• Mix and match teams for projects

Page 9: Git talk from Open 2011 conference in Israel

Version control is great

• If you’re a programmer and not using version control, you’re really missing out

• It’s also good for non-programs, such as configuration files

• If you’re a CTO/team leader/VP R&D and your people aren’t using version control, you’re inviting disaster

Page 10: Git talk from Open 2011 conference in Israel

What do people use?

• RCS (ancient history)

• CVS (should be ancient history)

• Subversion (“a better CVS”)

• SourceSafe

• Perforce

• lots of other systems

Page 11: Git talk from Open 2011 conference in Israel

Problems with these

• Work requires a server connection

• (You can work, but not commit.)

• Branching and merging is painful and slow

• So no one does it!

• Enforces a particular workflow

• More code, more developers — slower VC

Page 12: Git talk from Open 2011 conference in Israel

Distributed version control

• Distributed: Everyone is their own server!

• Everyone has a complete commit history

• No single point of failure

• Commit whenever you want (i.e., often)

• Merge when two repositories connect

Page 13: Git talk from Open 2011 conference in Israel

Git

• Linus needed a VCS... so he wrote one!

• Fast

• Accurate

• Distributed

• Massively scalable

• Super flexible

Page 14: Git talk from Open 2011 conference in Israel

Open-source Git users

• Linux kernel

• PostgreSQL

• Ruby language

• Ruby on Rails

• Perl language

• among many, many others

Page 15: Git talk from Open 2011 conference in Israel

Give me details!

• What does Git do that others don’t?

• Why do I claim that Git has changed my life?

Page 16: Git talk from Open 2011 conference in Israel

Branching & merging

• Branching and merging is both easy and fast in Git

• I often branch and merge several times in a given day

• When I used SVN, I would do everything in my power to avoid branching, because merges were long and difficult

Page 17: Git talk from Open 2011 conference in Israel

Branching & merging

• Create a branch:

git checkout -b new-branch

• Merge commits from other-branch

git merge other-branch

• Yes, it’s really that easy.

• You don’t even have to change directories!

Page 18: Git talk from Open 2011 conference in Israel

Easy branchingchanges everything!

• Every new feature goes in a branch

• Merging becomes easy, fast, and natural

• Side projects are stored, not kept out of version control for fear of “contamination”

• Commit, commit, commit all of the time, in an appropriate branch — never lose work!

Page 19: Git talk from Open 2011 conference in Israel

Local repository is great

• Work without a network connection

• Commits, merges, and switching branches are nearly instantaneous

• Reports and logs are super-fast

• “Show me all files modified on Tuesday of last week” produces immediate resultss

Page 20: Git talk from Open 2011 conference in Israel

Create a repository!

/tmp $ cd myapp/tmp/myapp $ git initInitialized empty Git repository in /private/tmp/myapp/.git/master ✗ /tmp/myapp $ git add .master ✗ /tmp/myapp $ git commit -a -m 'Initial commit'[master (root-commit) 030c6b6] Initial commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 fooInitial commit

master ✔ /tmp/myapp $

Page 21: Git talk from Open 2011 conference in Israel

Create a repository!

/tmp $ cd myapp/tmp/myapp $ git initInitialized empty Git repository in /private/tmp/myapp/.git/master ✗ /tmp/myapp $ git add .master ✗ /tmp/myapp $ git commit -a -m 'Initial commit'[master (root-commit) 030c6b6] Initial commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 fooInitial commit

master ✔ /tmp/myapp $

Page 22: Git talk from Open 2011 conference in Israel

Create a repository!

/tmp $ cd myapp/tmp/myapp $ git initInitialized empty Git repository in /private/tmp/myapp/.git/master ✗ /tmp/myapp $ git add .master ✗ /tmp/myapp $ git commit -a -m 'Initial commit'[master (root-commit) 030c6b6] Initial commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 fooInitial commit

master ✔ /tmp/myapp $

Page 23: Git talk from Open 2011 conference in Israel

Create a repository!

/tmp $ cd myapp/tmp/myapp $ git initInitialized empty Git repository in /private/tmp/myapp/.git/master ✗ /tmp/myapp $ git add .master ✗ /tmp/myapp $ git commit -a -m 'Initial commit'[master (root-commit) 030c6b6] Initial commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 fooInitial commit

master ✔ /tmp/myapp $

Page 24: Git talk from Open 2011 conference in Israel

Create a repository!

/tmp $ cd myapp/tmp/myapp $ git initInitialized empty Git repository in /private/tmp/myapp/.git/master ✗ /tmp/myapp $ git add .master ✗ /tmp/myapp $ git commit -a -m 'Initial commit'[master (root-commit) 030c6b6] Initial commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 fooInitial commit

master ✔ /tmp/myapp $

Page 25: Git talk from Open 2011 conference in Israel

Create a repository!

/tmp $ cd myapp/tmp/myapp $ git initInitialized empty Git repository in /private/tmp/myapp/.git/master ✗ /tmp/myapp $ git add .master ✗ /tmp/myapp $ git commit -a -m 'Initial commit'[master (root-commit) 030c6b6] Initial commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 fooInitial commit

master ✔ /tmp/myapp $

Page 26: Git talk from Open 2011 conference in Israel

The result

master ✔ /tmp/myapp $ git log

commit 030c6b691993e0f43d78119d9ff1c9e759120d11Author: Reuven Lerner <[email protected]>Date: Thu Jun 30 12:29:29 2011 +0300 I

Page 27: Git talk from Open 2011 conference in Israel

Committing in stages

• You can commit all modified files (“git commit -a”)

• You can add files to be committed (“git add foo.rb”) over time

• Using the staging area gives you tremendous flexibility before committing

Page 28: Git talk from Open 2011 conference in Israel

Remote branches

• A local branch can “track” a remote branch

• Different branches can be on different servers — and then you can merge across them

• One possible workflow: Read from one remote branch, and write to another

Page 29: Git talk from Open 2011 conference in Israel

Integration

• Git is all small Unix commands

• Easily integrated into text editors, IDEs, cron jobs, monitoring, and system tasks

• Example: Continuous integration servers

• Example: Code-analysis tools

• Also: Front ends to SVN and others!

Page 30: Git talk from Open 2011 conference in Israel

git blame

• Shows the commit, user, and timestamp of when each line of a file was changed

• Useful when you want to know who broke the build

• Also useful for deflecting blame from clients, who think you broke something!

Page 31: Git talk from Open 2011 conference in Israel

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 29)

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 30) #ifdef WIN32

65e806cb (Bruce Momjian 2010-02-26 02:01:40 +0000 31) #define FD_SETSIZE 1024

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 32) #endif /* ! WIN32 */

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 33)

d08741ea (Tom Lane 2001-02-10 02:31:31 +0000 34) #include "postgres_fe.h"

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 35)

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 36) #include "libpq-fe.h"

a8a198b1 (Itagaki Takahiro 2010-01-06 01:30:03 +0000 37) #include "libpq/pqsignal.h"

3da0dfb4 (Tatsuo Ishii 2009-08-03 15:18:14 +0000 38) #include "portability/instr_time.h"

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 39)

d156e1f8 (Tom Lane 2005-10-07 15:34:17 +0000 40) #include <ctype.h>

d156e1f8 (Tom Lane 2005-10-07 15:34:17 +0000 41)

a8a198b1 (Itagaki Takahiro 2010-01-06 01:30:03 +0000 42) #ifndef WIN32

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 43) #include <sys/time.h>

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 44) #include <unistd.h>

ffda6747 (Tom Lane 2007-09-27 20:39:43 +0000 45) #endif /* ! WIN32 */

Page 32: Git talk from Open 2011 conference in Israel

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 29)

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 30) #ifdef WIN32

65e806cb (Bruce Momjian 2010-02-26 02:01:40 +0000 31) #define FD_SETSIZE 1024

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 32) #endif /* ! WIN32 */

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 33)

d08741ea (Tom Lane 2001-02-10 02:31:31 +0000 34) #include "postgres_fe.h"

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 35)

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 36) #include "libpq-fe.h"

a8a198b1 (Itagaki Takahiro 2010-01-06 01:30:03 +0000 37) #include "libpq/pqsignal.h"

3da0dfb4 (Tatsuo Ishii 2009-08-03 15:18:14 +0000 38) #include "portability/instr_time.h"

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 39)

d156e1f8 (Tom Lane 2005-10-07 15:34:17 +0000 40) #include <ctype.h>

d156e1f8 (Tom Lane 2005-10-07 15:34:17 +0000 41)

a8a198b1 (Itagaki Takahiro 2010-01-06 01:30:03 +0000 42) #ifndef WIN32

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 43) #include <sys/time.h>

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 44) #include <unistd.h>

ffda6747 (Tom Lane 2007-09-27 20:39:43 +0000 45) #endif /* ! WIN32 */

Commit

Page 33: Git talk from Open 2011 conference in Israel

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 29)

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 30) #ifdef WIN32

65e806cb (Bruce Momjian 2010-02-26 02:01:40 +0000 31) #define FD_SETSIZE 1024

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 32) #endif /* ! WIN32 */

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 33)

d08741ea (Tom Lane 2001-02-10 02:31:31 +0000 34) #include "postgres_fe.h"

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 35)

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 36) #include "libpq-fe.h"

a8a198b1 (Itagaki Takahiro 2010-01-06 01:30:03 +0000 37) #include "libpq/pqsignal.h"

3da0dfb4 (Tatsuo Ishii 2009-08-03 15:18:14 +0000 38) #include "portability/instr_time.h"

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 39)

d156e1f8 (Tom Lane 2005-10-07 15:34:17 +0000 40) #include <ctype.h>

d156e1f8 (Tom Lane 2005-10-07 15:34:17 +0000 41)

a8a198b1 (Itagaki Takahiro 2010-01-06 01:30:03 +0000 42) #ifndef WIN32

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 43) #include <sys/time.h>

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 44) #include <unistd.h>

ffda6747 (Tom Lane 2007-09-27 20:39:43 +0000 45) #endif /* ! WIN32 */

Commit User

Page 34: Git talk from Open 2011 conference in Israel

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 29)

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 30) #ifdef WIN32

65e806cb (Bruce Momjian 2010-02-26 02:01:40 +0000 31) #define FD_SETSIZE 1024

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 32) #endif /* ! WIN32 */

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 33)

d08741ea (Tom Lane 2001-02-10 02:31:31 +0000 34) #include "postgres_fe.h"

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 35)

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 36) #include "libpq-fe.h"

a8a198b1 (Itagaki Takahiro 2010-01-06 01:30:03 +0000 37) #include "libpq/pqsignal.h"

3da0dfb4 (Tatsuo Ishii 2009-08-03 15:18:14 +0000 38) #include "portability/instr_time.h"

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 39)

d156e1f8 (Tom Lane 2005-10-07 15:34:17 +0000 40) #include <ctype.h>

d156e1f8 (Tom Lane 2005-10-07 15:34:17 +0000 41)

a8a198b1 (Itagaki Takahiro 2010-01-06 01:30:03 +0000 42) #ifndef WIN32

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 43) #include <sys/time.h>

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 44) #include <unistd.h>

ffda6747 (Tom Lane 2007-09-27 20:39:43 +0000 45) #endif /* ! WIN32 */

Commit User Timestamp

Page 35: Git talk from Open 2011 conference in Israel

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 29)

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 30) #ifdef WIN32

65e806cb (Bruce Momjian 2010-02-26 02:01:40 +0000 31) #define FD_SETSIZE 1024

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 32) #endif /* ! WIN32 */

a23c6415 (Magnus Hagander 2009-07-30 09:28:00 +0000 33)

d08741ea (Tom Lane 2001-02-10 02:31:31 +0000 34) #include "postgres_fe.h"

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 35)

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 36) #include "libpq-fe.h"

a8a198b1 (Itagaki Takahiro 2010-01-06 01:30:03 +0000 37) #include "libpq/pqsignal.h"

3da0dfb4 (Tatsuo Ishii 2009-08-03 15:18:14 +0000 38) #include "portability/instr_time.h"

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 39)

d156e1f8 (Tom Lane 2005-10-07 15:34:17 +0000 40) #include <ctype.h>

d156e1f8 (Tom Lane 2005-10-07 15:34:17 +0000 41)

a8a198b1 (Itagaki Takahiro 2010-01-06 01:30:03 +0000 42) #ifndef WIN32

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 43) #include <sys/time.h>

a765db40 (Tatsuo Ishii 2000-01-15 12:38:09 +0000 44) #include <unistd.h>

ffda6747 (Tom Lane 2007-09-27 20:39:43 +0000 45) #endif /* ! WIN32 */

Commit User Timestamp Line # and code

Page 36: Git talk from Open 2011 conference in Israel

git bisect

• You introduced a bug, but don’t know where? Git can find it!

• Give two endpoints

• Git does a binary search between them

• For each commit, tell Git whether it is “good” or “bad”

• This finds the bug very quickly

Page 37: Git talk from Open 2011 conference in Israel

Stash

• Temporary storage space

• Useful if you have done enough work to keep, but not enough for a commit

• Emergency fixes in the middle of working on something else

• If you need, put the stash into a new branch

• Oh, you can have multiple stashes

Page 38: Git talk from Open 2011 conference in Israel

Hooks

• Execute arbitrary code when things happen

• Example: Update submodules automatically

• Example: Send e-mail after a commit

• Example: Heroku uses hooks to deploy a new version of your Web app when you push to them!

Page 39: Git talk from Open 2011 conference in Israel

Workflows

• Git supports many, many workflows

• Traditional centralized server

• BDFL and users

• BDFL and Lieutenants

• Do what works for you!

Page 40: Git talk from Open 2011 conference in Israel

Source: ProGit.org

Traditional

Page 41: Git talk from Open 2011 conference in Israel

Source: ProGit.org

Integration-manager

Page 42: Git talk from Open 2011 conference in Israel

Source: ProGit.org

Large, BDFL

Page 43: Git talk from Open 2011 conference in Israel

Cherry picking

• You can merge specific commits, or parts of commits

• Amazingly powerful

Page 44: Git talk from Open 2011 conference in Israel

Rebase

• An alternative to regular merging

• First merge all external commits, then replay your changes on top

• Helps to keep you aligned with remote branches

• Avoids some merge-conflict issues

Page 45: Git talk from Open 2011 conference in Israel

Submodules

• A Git repository can point to other repositories — submodules

• Break a project into smaller projects

• Git puts the pieces together for you!

Page 46: Git talk from Open 2011 conference in Israel

Amending commits

• Commits are not sacred in Git

• Rather, they are a tool to manage history

• Change, amend, and edit commits

• Yes, this can be dangerous!

• Also avoids hundreds of “fixed typo” commits in a row...

Page 47: Git talk from Open 2011 conference in Israel

Commercial hosting

• You can host Git repositories on your own

• Want to go commercial? GitHub, Gitorious, and many others work great

• GitHub is free for open-source projects, and thus very popular

Page 48: Git talk from Open 2011 conference in Israel
Page 49: Git talk from Open 2011 conference in Israel

GUIs

• Yes, there are now GUIs for Git

• Windows, Mac, and Unix versions all exist

• Integration with Emacs and other editors for hard-core hacker-nerds

• Also: Shell-prompt integration for Unix

Page 50: Git talk from Open 2011 conference in Israel

In short

• Git has completely changed how I develop code, both alone and with others

• The speed and easy branching/merging are game-changers

• It’s really not that hard to start using...

• ... and once you start, you’ll never want to go back to CVS or SVN

Page 51: Git talk from Open 2011 conference in Israel

Want to learn more?

• Mailing lists, wikis, and blogs

• http://git-scm.com/

• http://progit.org/

• http://gitimmersion.com/

• I’m probably going to offer Git training soon — let me know if you’re interested!

Page 52: Git talk from Open 2011 conference in Israel

Thanks!(Any questions?)

[email protected]://www.lerner.co.il/

054-496-8405“reuvenlerner” on Skype/AIM