Top Banner
An Introduction to GIT Version Control with Git Avoid to be the repository manager Dr. Fabio Fumarola
82
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 3 Git

An Introduction to GITVersion Control with Git

Avoid to be the repository manager

Dr. Fabio Fumarola

Page 2: 3 Git

Agenda• What is Version Control? (and why use it?)• What is Git? (And why Git?)• How git works• Create a repository• Branches• Add remote• How data is stored

2

Page 3: 3 Git

History• Created by Linus Torvalds for work on the Linux

kernel ~2005• Some of the companies that use git:

3

Page 4: 3 Git

What is Git?

Page 5: 3 Git

Git is a

DistributedVersion Control System

Page 6: 3 Git

OR

Page 7: 3 Git

Git is a

Content Management System

Page 8: 3 Git

Git is a

history storage system

Page 9: 3 Git

Git is a

content tracker

Page 10: 3 Git

How ever you think about it…

Page 11: 3 Git

How ever you think about it…

Page 12: 3 Git

What is a Version Control • Version Control - A system for managing changes made to

documents and other computer files• What kinds of files can we use it with?

– Source code– Documentation– Short stories– Binary files (music and pictures)

• What should we use it for?– Text files– Projects that have lots of revisions (changes)– Collaborating

12

Page 13: 3 Git

VCS Systems and their Operations

• Lots of different choices available:– CVS– SVN– Perforce– Git– Mercurial (Hg), Bazaar– And more!

• Most follow a repository model (though differ in how the repositories work)

13

Page 14: 3 Git

So why do we need a VCS?• Our goals

– Share code (or something else) easily– Keep track of any changes we make (and undo them with

ease)– Maintain multiple versions of the same project/code base– Clearly communicate what changes have been made

• There are two type of VCS– Centralized and– Distributed

14

Page 15: 3 Git

Distributed vs Centralized

Centralized VCS•One central repository•Must be capable of connecting to repo•Need to solve issues with group members making different changes on the same files

Distributed VCS•Everyone has a working repo•Faster•Connectionless•Still need to resolve issues, but it's not an argument against DVCS

15

Page 16: 3 Git

Centralized vs Distributed

16

Central ServerRemote Server

Page 17: 3 Git

Git file lifecycle

17

Page 18: 3 Git

Creating our first repository• Install git• Establish our first repository:

– mkdir git-test– cd git-test– git init

• What do we have here?– git status

18

Page 19: 3 Git

Using our first repository• Add a file

– touch file.txt– git add file.txt– git commit –m “add the first file”

19

Page 20: 3 Git

Branching• With Git we should embrace

the idea of branching• Branching is the best method

to work with other in a project

20

Page 21: 3 Git

Branches Illustrated

master

A

> git commit –m ‘my first commit’> git commit –m ‘my first commit’

Page 22: 3 Git

Branches Illustrated

master

> git commit (x2)> git commit (x2)

A B C

Page 23: 3 Git

Branches Illustrated

bug123

master

> git checkout –b bug123> git checkout –b bug123

A B C

Page 24: 3 Git

Branches Illustrated

master

> git commit (x2)> git commit (x2)

A B C

D E

bug123

Page 25: 3 Git

Branches Illustrated

master

> git checkout master> git checkout master

A B C

D E

bug123

Page 26: 3 Git

Branches Illustrated

bug123

master

> git merge bug123> git merge bug123

A B C D E

Page 27: 3 Git

Branches Illustrated

master

> git branch -d bug123> git branch -d bug123

A B C D E

Page 28: 3 Git

Branches Illustrated

master

A B C D E

F G

bug456

Page 29: 3 Git

Branches Illustrated

master

A B C D E

F G

bug456

> git checkout master> git checkout master

Page 30: 3 Git

Branches Illustrated

master

A B C D E

F G

> git merge bug456> git merge bug456

H

bug456

Page 31: 3 Git

Branches Illustrated

master

A B C D E

F G

> git branch -d bug456> git branch -d bug456

H

Page 32: 3 Git

Branches Illustrated

master

A B C D E

F G

bug456

Page 33: 3 Git

Branches Illustrated

master

A B C D E

> git rebase master> git rebase master

F’ G’

bug456

Page 34: 3 Git

Branches Illustrated

master

A B C D E

> git checkout master> git merge bug456 > git checkout master> git merge bug456

F’ G’

bug456

Page 35: 3 Git

Branches Review• Branches should be used to create “Features” in our

project• Local branches are very powerful• Rebase is not scary

35

Page 36: 3 Git

Adding a remoteBut, how we share our code with the other collaborators?

36

My Local Repo

Tom’s Repo

Tracey’s Repo

Matt’s Repo

A B C

A B C A B C

A B C

Page 37: 3 Git

Sharing commits

37

My Local Repo

Tom’s Repo

Tracey’s Repo

Matt’s Repo

A B C

A B C A B C

A B C

Remote Repo

AA BB CC

D

D

D

D

DD

Page 38: 3 Git

Setting up a Remote• We can clone an existing repository– git clone [email protected]:fabiofumarola/akka-tutorial.git

• We can push our changes– git push

• We can pull friends change– git pull

• We can also add a remote to an existing repository– git remote add origin [email protected]:fabiofumarola/akka-

tutorial.git38

Page 39: 3 Git

Branches with remote

A

master

B C D E

bug123

Page 40: 3 Git

Branches with remote

A

master

origin/master

B C D E

bug123

Page 41: 3 Git

Branches with remote

A

BB CC DD EE

master

bug123

origin/master

Page 42: 3 Git

Branches with remote

A

BB CC DD EE

master

bug123

FF GG

origin/master

origin/masterorigin/master

Page 43: 3 Git

Branches with remote

A

BB CC DD EE

master

bug123

> git checkout master> git checkout master

origin/master

Page 44: 3 Git

Branches with remote

A

BB CC DD EE

master

bug123

F G

> git pull origin> git pull origin

origin/master

Page 45: 3 Git

Pull = Fecth + Merge

Fetch - updates your local copy of the remote branch

Pull essentially does a fetch and then runs the merge in one step.

Page 46: 3 Git

Branches Illustrated

A

BB CC DD EE

master

bug123

F G

origin/master

Page 47: 3 Git

Branches Illustrated

A

BB CC DD EE

master

bug123

F G

> git checkout bug123> git checkout bug123

origin/master

Page 48: 3 Git

Branches Illustrated

A

B’B’ C’C’ D’D’ E’E’

master

bug123

F G

> git rebase master> git rebase master

origin/master

Page 49: 3 Git

Branches Illustrated

A

B’B’ C’C’ D’D’ E’E’

master

bug123

F G

> git checkout master> git checkout master

origin/master

Page 50: 3 Git

Branches Illustrated

A

master

bug123

F G

> git merge bug123> git merge bug123

B’B’ C’C’ D’D’ E’E’

origin/master

Page 51: 3 Git

Branches Illustrated

A

master

F G

> git push origin> git push origin

B’ C’ D’ E’

bug123

origin/master

Page 52: 3 Git

Push

Pushes your changes upstream

Git will reject pushes if newer changes exist on remote.

Good practice: Pull then Push

Page 53: 3 Git

Branches Illustrated

A

master

F G B’ C’ D’ E’

bug123

origin/master

Page 54: 3 Git

Branches Illustrated

A

master

F G

> git branch -d bug123> git branch -d bug123

B’ C’ D’ E’

origin/master

Page 55: 3 Git

Short vs. Long-Lived Branches• We can use branch to:– Solve bugs (hotfixes)– Create features– Make a release

• In order to simplify the management we can use:– Git Flow: http://danielkummer.github.io/git-flow-

cheatsheet/index.it_IT.html

Page 56: 3 Git

Branches Illustrated

E

master

origin/master

Page 57: 3 Git

Branches Illustrated

E

master

origin/master

develop

> git branch develop> git branch develop

Page 58: 3 Git

Branches Illustrated

E

master

origin/master

develop

> git push origin develop> git push origin develop

origin/develop

Page 59: 3 Git

Branches Illustrated

E

master

origin/master

develop

> git checkout develop> git checkout develop

origin/develop

Page 60: 3 Git

Branches Illustrated

E

master

origin/master

FF GG

developorigin/develop

Page 61: 3 Git

Branches Illustrated

E

master

origin/master

F G

developorigin/develop

> git pull origin develop> git pull origin develop

Page 62: 3 Git

Branches Illustrated

E

master

origin/master

F G

develop

origin/develop

> git checkout –b idea> git checkout –b idea

idea

Page 63: 3 Git

Branches Illustrated

E

master

origin/master

F G

developorigin/develop

> git commit> git commit

idea

H

Page 64: 3 Git

Branches Illustrated

E

origin/master

F G

origin/develop idea

H

II

master

develop

Page 65: 3 Git

Branches Illustrated

E

origin/master

F G

origin/develop idea

H

master

develop

> git pull (at least daily)> git pull (at least daily)

I

Page 66: 3 Git

Branches Illustrated

E

origin/master

F G

origin/develop idea

H

master

> git checkout develop> git checkout develop

I

develop

Page 67: 3 Git

Branches Illustrated

E

origin/master

F G

origin/develop idea

H

master

> git merge idea (fast forward merge)> git merge idea (fast forward merge)

I

develop

Page 68: 3 Git

Branches Illustrated

E

origin/master

F G

origin/develop

H

master

> git branch –d idea> git branch –d idea

I

develop

Page 69: 3 Git

Branches Illustrated

E

origin/master

F G

origin/develop

H

master

> git push origin develop> git push origin develop

I

develop

Page 70: 3 Git

Merge Flow vs. Rebase Flow

E

origin/master

F G

origin/develop

H

master

> git push origin develop> git push origin develop

I

develop

Page 71: 3 Git

Branches Illustrated – Merge Flow

E

origin/master

F G

origin/develop

H

master

> git checkout master> git checkout master

I

develop

Page 72: 3 Git

Branches Illustrated – Merge Flow

E

origin/master

F G

origin/develop

H

master

> git merge develop> git merge develop

I

develop

J

Page 73: 3 Git

Branches Illustrated – Merge Flow

E

origin/master

F G

origin/develop

H

master

> git push origin> git push origin

I

develop

J

Page 74: 3 Git

Branches Illustrated – Rebase Flow

E

origin/master

F G

origin/develop

H

master

> git checkout master> git checkout master

I

develop

Page 75: 3 Git

Branches Illustrated – Rebase Flow

E

origin/master

F G

origin/develop

H

master

> git rebase develop> git rebase develop

I’

develop

II

Page 76: 3 Git

Branches Illustrated – Rebase Flow

E

origin/master

F G

origin/develop

H

master

> git push origin> git push origin

I’

develop

Page 77: 3 Git

Rebase Flow

E

origin/master

F G

origin/develop

H

master

I’

develop

E

origin/master

F G

origin/develop

H

master

I

develop

J

Merge Flow

Page 78: 3 Git

How Git stores data• Git stores the content of each file in the tracking history• Each time we do a commit it is made a copy of the file.• However the content of each file is subject to revision for

conflicts (merge).

78

Page 79: 3 Git

Git best practices for code collaboration• When to commit?

– Source of major arguments (big changes vs small change)– Never put broken code on the master branch (test first!)– Try not to break things (don't do two weeks worth of work in one

commit)– Always use a clear, concise commit message– Put more details in lines below, but always make the first line short– Describe the why; the what is clear in the change log

• When making giant changes, consider branches (we'll talk about these in a few slides)

• Oh, and make sure your name and email are right79

Page 80: 3 Git

Supplemental

Page 81: 3 Git

SSH• Used to be most common transport for git• Pros

– Allows reads and writes– Authenticated network protocol– Secure: data transfer is encrypted and authenticated– Efficient: makes data as compact as possible

• Cons– No anonymous read-only access

Page 82: 3 Git

Sidebar: What is SSH?• SSH is a protocol used for secure network

communication Getting files from github• Generate public/private keys (ssh-keygen)• Distribute public keys (add key to github)• Someone (github) sends secure “message” (files) –

they encode with public key• You receive the message/files – decode with

private key (only you know)Putting files on github• Process is reversed to send files to github• You have the github public key (see

github_rsa.pub, in Documents and Settings/Cyndi/.ssh on my machine)

• Use it to encode when sending• github uses their private key to decode