git tutorial

63
Git and Github™: Code Management and Sharing for Researchers Eliezer Kanal 10/24/2011 Versioning Your Code: using Git and Github (™)

Upload: eliezer-kanal

Post on 17-Nov-2014

1.614 views

Category:

Technology


2 download

DESCRIPTION

Slides from a Git intro talk I gave at Carnegie Mellon University.

TRANSCRIPT

Page 1: Git tutorial

Git and Github™: Code Management and Sharing for Researchers

Eliezer Kanal10/24/2011

Versioning Your Code:

using Gitand Github

(™)

Page 2: Git tutorial

Slides available online!

!""#$%%"&'()*+,-./%-'0-1&"2

Page 3: Git tutorial

What is Versioning?

• Keeping track of changes to your code

• Documenting those changes

• You’re probably doing it now...

Page 4: Git tutorial

Why Change?

• Troubleshooting

• Logging

• Simplify addingfeatures

• Simplify codesharing

Page 5: Git tutorial

What is Git?

• DVCS = Distributed Version Control System

• Technique for managing large/small coding projects

Large

Small Me, >1 million other people

Page 6: Git tutorial

What is Git?

• Two parts:

1. “Database” (hidden folder) to hold project history

2. Set of tools to interact with database

• Used via Command Line or GUI interfaces

Page 7: Git tutorial

What does Git do?

• Retain file history

Page 8: Git tutorial

What does Git do?

• Retain file historydistributed^

Page 9: Git tutorial

What does Git do?

• Branching & Merging

version 2 version 3 version 4

version 4b.2

version 5

version 4b

version 1

new feature branch

Page 10: Git tutorial

Case Study – Analysis toolkit

• Improve interface, ~2 days work

- …while analyzing data

• Branch!

- Improvements on“development” branch

- Work on “main” branch

- Merge when feature completed

Page 11: Git tutorial

Case Study – Productivity tool• 13"45!&"46.'3: productivity

tool, on 1&"!)0,-./

• Downloaded, found bug

• Fixed bug, multiple localcheck-ins

• Sent change historiesback via 1&"!)0,-./

bad word!

Page 12: Git tutorial

Case Study – Productivity tool• 13"45!&"46.'3: productivity

tool, on 1&"!)0,-./

• Downloaded, found bug

• Fixed bug, multiple localcheck-ins

• Sent change historiesback via 1&"!)0,-./

bad word!

Page 13: Git tutorial

How Git can help YOU

• Personally:

- Keep track of changes

- Fix bugs quicker

- Share code easier

- Simplify adding new features/changing code

• CNBC as a group:

- “Don’t reinvent the wheel”

- More eyes → better code

- Collaboration

*

* Or Mercurial, or Bazaar, or Monotone, or LibreSource, or...

Page 14: Git tutorial

This slide intentionally left blank

Page 15: Git tutorial

Git Usage

Page 16: Git tutorial

781&"8966 781&"8-.//&"

Key Concepts

WorkspaceStaging Area Repository

Work is done here

Things “about to be stored” are put here

Remote

781&"8#)5!

781&"8#)++

Same as Repository, but remote

History, branches are stored here

Page 17: Git tutorial

• Create & edit some code

:)'-"&.'8/9"!;<

*3")*'8=>=?

/9"!,/

Page 18: Git tutorial

• Move code to Staging Area

• Commit code

Page 19: Git tutorial

• Make changes, start working on new code

:)'-"&.'8/9"!;<

*3")*'8=>=?

/9"!,/

:)'-"&.'8/9"!;9@0<

*3")*'89>0?

/9"!,/

:)'-"&.'8#*&'";9<

3-!.89?

#*&'",/

Page 20: Git tutorial

• Move completed code to“Staging Area”

- Leave the rest “unstaged”

• Commit code

…wash, rinse, repeat:

Page 21: Git tutorial

Key Concepts

• Finder (Explorer) not suited for Git

Not enough information!

- Are these in the workspaceor staging area?

- What branch are we on?

- What revision is this?

Page 22: Git tutorial

Git Interface

• Command line

• GUI

- Github for Mac

- GitExtensions

- Others

All work slightly (significantly)

differently

Page 23: Git tutorial

Example Usage

• Starting work on /(A*.B3-":

Terminal78-68/(A*.B3-"

781&"8&'&"8

That’s it!

Page 24: Git tutorial

Command Line Interface

Page 25: Git tutorial

• Create & edit some code

:)'-"&.'8/9"!;<

*3")*'8=>=?

/9"!,/

Page 26: Git tutorial

• Code is in working state; ready for “commit”

Terminal781&"89668/9"!,/

781&"8-.//&"84/8C-*39"368/9"!8:)'-"&.'D

Page 27: Git tutorial

• Code is in working state; ready for “commit”

Terminal781&"89668/9"!,/

781&"8-.//&"84/8C-*39"368/9"!8:)'-"&.'D

Page 28: Git tutorial

• Code is in working state; ready for “commit”

Terminal781&"89668/9"!,/

781&"8-.//&"84/8C-*39"368/9"!8:)'-"&.'D

Page 29: Git tutorial

• Code is in working state; ready for “commit”

Terminal781&"89668/9"!,/

781&"8-.//&"84/8C-*39"368/9"!8:)'-"&.'D

Page 30: Git tutorial

• Make changes, add new code

:)'-"&.'8/9"!;<

*3")*'8=>=?

/9"!,/

:)'-"&.'8/9"!;9@0<

*3")*'89>0?

/9"!,/

:)'-"&.'8#*&'";9<

3-!.89?

#*&'",/

Page 31: Git tutorial

• See what we’ve done

Terminal781&"85"9")5

E8F'80*9'-!8/95"3*

E8G!9'1358'."85"91368:.*8-.//&"$

E8;)538H1&"89668I:&+3J,,,H8".8)#69"38K!9"8K&++8038-.//&""36<

E

E8 /.6&:&36$8/9"!,/

E

E8L'"*9-M368:&+35$

E888;)538H1&"89668I:&+3J,,,H8".8&'-+)638&'8K!9"8K&++8038-.//&""36<

E

E8 #*&'",/

'.8-!9'1358966368".8-.//&"8;)538H1&"8966H89'6%.*8H1&"8-.//&"849H<

Page 32: Git tutorial

• See what we’ve done

Terminal781&"85"9")5

E8F'80*9'-!8/95"3*

E8G!9'1358'."85"91368:.*8-.//&"$

E8;)538H1&"89668I:&+3J,,,H8".8)#69"38K!9"8K&++8038-.//&""36<

E

E8 /.6&:&36$8/9"!,/

E

E8L'"*9-M368:&+35$

E888;)538H1&"89668I:&+3J,,,H8".8&'-+)638&'8K!9"8K&++8038-.//&""36<

E

E8 #*&'",/

'.8-!9'1358966368".8-.//&"8;)538H1&"8966H89'6%.*8H1&"8-.//&"849H<

Page 33: Git tutorial

• See what we’ve done

Terminal781&"85"9")5

E8F'80*9'-!8/95"3*

E8G!9'1358'."85"91368:.*8-.//&"$

E8;)538H1&"89668I:&+3J,,,H8".8)#69"38K!9"8K&++8038-.//&""36<

E

E8 /.6&:&36$8/9"!,/

E

E8L'"*9-M368:&+35$

E888;)538H1&"89668I:&+3J,,,H8".8&'-+)638&'8K!9"8K&++8038-.//&""36<

E

E8 #*&'",/

'.8-!9'1358966368".8-.//&"8;)538H1&"8966H89'6%.*8H1&"8-.//&"849H<

Page 34: Git tutorial

• Add...

Terminal781&"89668/9"!,/

781&"89668#*&'",/

781&"85"9")5

E8F'80*9'-!8/95"3*

E8G!9'1358".8038-.//&""36$

E888;)538H1&"8*353"8NOPQ8I:&+3J,,,H8".8)'5"913<

E

E8 /.6&:&36$888/9"!,/

E8 '3K8:&+3$888#*&'",/

E

Page 35: Git tutorial

• Add...

Terminal781&"89668/9"!,/

781&"89668#*&'",/

781&"85"9")5

E8F'80*9'-!8/95"3*

E8G!9'1358".8038-.//&""36$

E888;)538H1&"8*353"8NOPQ8I:&+3J,,,H8".8)'5"913<

E

E8 /.6&:&36$888/9"!,/

E8 '3K8:&+3$888#*&'",/

E

Page 36: Git tutorial

• …and Commit

Terminal781&"8-.//&"84/8R966368S9*&90+358".8/9"!@8'3K8#*&'"8:)'-"&.'R

T/95"3*89U3-VW9X8966368S9*&90+358".8/9"!@8'3K8#*&'"8:)'-"&.'

8=8:&+358-!9'136@8=8&'53*"&.'5;><@8W863+3"&.'5;4<

8-*39"38/.638=WWVYY8#*&'",/

Page 37: Git tutorial

• …and Commit

Terminal781&"8-.//&"84/8R966368S9*&90+358".8/9"!@8'3K8#*&'"8:)'-"&.'R

T/95"3*89U3-VW9X8966368S9*&90+358".8/9"!@8'3K8#*&'"8:)'-"&.'

8=8:&+358-!9'136@8=8&'53*"&.'5;><@8W863+3"&.'5;4<

8-*39"38/.638=WWVYY8#*&'",/

Page 38: Git tutorial

• Let’s see what we’ve done...

Terminal781&"8+.1

-.//&"89U3-VW9ZVU:6VY3=[ZU9WVW=6U[2\[03V]U:-U99

P)"!.*$8O+&3^3*8_9'9+8I3M9'9+`-/),36)J

Q9"3$888a!)8b)'8=V8=[$=\$2=82W==84WYWW

8888966368S9*&90+358".8/9"!@8'3K8#*&'"8:)'-"&.'

-.//&"8603[]\WW]930\3V000[30:0\-]9=]VY=6]2ZW\\W

P)"!.*$8O+&3^3*8_9'9+8I3M9'9+`-/),36)J

Q9"3$888a!)8b)'8=V8=[$==$WY82W==84WYWW

8888-*39"368/9"!8:)'-"&.'

Page 39: Git tutorial

Git Workflow – Complete

• Do some work

• See what you’ve changed, verify things look good:

• Tell Git what you’re ready to commit:

• Commit & log changes:

781&"85"9")5

781&"89668I:&+3'9/35J

781&"8-.//&"8I:&+3'9/35J

!"#$%&'(&%)$*&('*+,'-+%&./

781&"86&::"..+8I:&+3'9/35JHomework: figure out

what this does

Page 40: Git tutorial

Github for Mac™

Page 41: Git tutorial
Page 42: Git tutorial
Page 43: Git tutorial
Page 44: Git tutorial
Page 45: Git tutorial
Page 46: Git tutorial
Page 47: Git tutorial
Page 48: Git tutorial

Branches

• Branches let us work in a “new directory” which we can later either delete or merge.

• Question: How can we integrate new features without messing up our current working code?

Page 49: Git tutorial

Branches

• Think of branches as separate folders, each with it’s own content and history.

& Tags

Page 50: Git tutorial

Bran

ches

– G

o N

uts

Page 51: Git tutorial

Branches

• If a branch is a split in the graph, then merge is a merging of split branches

• One single command (SVN users rejoice!)

• Conflicts: Same code was changed in both branches

• Fix by hand, re-try the merge.

- !""#$%%KKK,M3*'3+,.*1%#)0%5.:"K9*3%5-/%1&"%6.-5%)53*4

/9')9+,!"/+E*35.+S&'1494/3*13

- better idea: Google “fixing a git merge conflict”

& Merges

Page 52: Git tutorial

Branches

• Conflicts: Same code was changed in both branches

• Merge fails!

• Fix by hand, re-try the merge

- !""#$%%KKK,M3*'3+,.*1%#)0%5.:"K9*3%5-/%1&"%6.-5%)53*4

/9')9+,!"/+E*35.+S&'1494/3*13

- better idea: Search “fixing a git merge conflict”

& Merges

Page 53: Git tutorial

c&"!)0,-./

git·hub /'ɡɪtˌhʌb/GitHub is the best way to collaborate with others. Fork, send pull requests and manage all your public and private git repositories.

Page 54: Git tutorial

c&"!)0,-./

• Very easy collaboration

• Clear, detailed setup instructions

4 !""#$%%!3+#,1&"!)0,-./%

• Easy “forking” for Github projects

- Get your own copy of their code

- Contribute back with “pull requests”

Page 55: Git tutorial
Page 56: Git tutorial
Page 57: Git tutorial

Demo

Page 58: Git tutorial

SVN integration

• Very easy to move from SVN → Git

• Works best as one-time deal, can work side-by-side

• !""#$%%.*35"&5,1*%0+.1%2WWU%WU%2\%1&"45S'4")".*&9+%

Page 59: Git tutorial

Good Git Practice

• Check in often!

- Rule of thumb… check in whenever code works

• Write detailed log notes! You’ll thank yourself for it later!

• Branch for new features

- Avoid working on the master branch, it should always be the “clean, working” code

Page 60: Git tutorial

References• Pro Git - free online: ! !""#$%%#*.1&",.*1%0..M%

• Branching:! !""#$%%'S&3,-./%#.5"5%945)--355:)+41&"40*9'-!&'14/.63+%

• Git tutorials and useful FAQ links:! !""#$%%5&d*3S&5&.'5,-./%*35.)*-35%1&"4")".*&9+54031&''3*5%

• Github:! !""#$%%1&"!)0,-./%

• Git-SVN integration:! !""#$%%.*35"&5,1*%0+.1%2WWU%WU%2\%1&"45S'4")".*&9+%

• Google:! !""#$%%1..1+3,-./%

• Bing:! !""#$%%0&'1,-./%

Page 61: Git tutorial

Git Hands-On

Page 62: Git tutorial

Git Setup – one-time

• Identify yourself to Git:

All your work is logged using the name/email you provide here.

• Give Git some color to make things easier to read:

• Line-ending bandaids:

781&"8-.':&18441+.09+8)53*,'9/38He.)*8f9/3H88

781&"8-.':&18441+.09+8)53*,3/9&+8H(.)*`3/9&+,-./H8

781&"8-.':&18441+.09+8-.+.*,)&89)".8

Recommend downloading xcode, just for the FileMerge program (“opendiff” when launched from command line)

781&"8-.':&18441+.09+8-.*3,59:3-*+:8"*)38

781&"8-.':&18441+.09+8-.*3,9)".-*+:8&'#)"

Windows users: use “"*)3”

instead of “&'#)"”

Page 63: Git tutorial

Git Setup

• Begin using Git for a project:

Creates the (hidden) ,1&" folder. No files being tracked yet.

• Add files to repository:

Instructs Git to manage all files in the folder %#*.B3-"g.+63*%.

78-68#*.B3-"g.+63*%

781&"8&'&"

h'&"&9+&^3683/#"(8c&"8*3#.5&".*(8&'8%#*.B3-"g.+63*%,1&"%

781&"89668,

781&"8-.//&"84/8i:&*5"8-.//&"j