59
A happier life with Git Loic Le Tiran F USP – May 6 th 2014

A happier life with git

Embed Size (px)

Citation preview

A happier life with Git

Loic Le TiranIF USP – May 6th 2014

This

Tal

kWhat do we do? Why we should

use version control? Why git?

How does git work? How to use it?Very basic stuffsUse of branchesUse of remotes

GitHub.com

Demo?

Disclaimer

I don't know what I am talking about!

I am no Git expert, I am just a humble Git enthusiast.

Please ask me questions. It will help me understand that thing better.

What is our daily routine?

We create a documentWe save this documentWe edit this document

We save this document againWe edit this document againWe save this document again

What is our daily routine?

We create a documentWe save this documentWe edit this document

We save this document againWe edit this document againWe save this document again

What is our daily routine?

We create a documentWe save this documentWe edit this document

We save this document againWe edit this document againWe save this document again

What is our daily routine?

We create a documentWe save this documentWe edit this document

We save this document againWe edit this document againWe save this document again

What is our daily routine?• We create a document• We save this document• We edit this document• We save this document again• We edit this document• We save this document again• We edit this document• We save this document again• We edit this document• We save this document again• We edit this document• We save this document again• We edit this document• We save this document again

What is version control?

Something that keeps track of:WHEN the file(s) changed WHY the file(s) changed

WHAT changed in the file(s)But also

WHO changed the file(s)

Why is ‘who’ so important?

• Something that keeps track of:–WHEN the file(s) changed –WHY the file(s) changed –WHAT changed in the file(s)

• But also–WHO changed the file(s)

Credit:Robert Simpsonshttp://orbitingfrog.com/

• Something that keeps track of:–WHEN the file(s) changed –WHY the file(s) changed –WHAT changed in the file(s)

• But also–WHO changed the file(s)

Credit:Robert Simpsonshttp://orbitingfrog.com/

Why is ‘who’ so important?

“There are also research groups or individuals who take the more modern approach of making their codes open source. This has the tremendous advantage that the task of scrutinizing, testing, validating, and debugging the code no longer rests upon the shoulders of an individual, but of the entire community. Some individuals believe that this amounts to giving away trade secrets, but there are notable examples of researchers whose careers have blossomed partly because of influential computer codes they have made freely available.”

Heng, 1404.6248, The Nature of Scientific Proof in the Age of Simulations

Why is ‘who’ so important?

"I'm an egotistical bastard, and I name all my projects after myself.

First 'Linux', now 'git'.“

Linus Torvalds 2010

The best version control software to

date.

Your own file-system for each of your projects.

Free, open-source, fast, reliable,

distributed (not centralized).

Allows you to work in a car, a plane, a train, without an

internet connection.

Git only adds data: no risk of data-loss. It's very hard to lose things

once you committed.

Your daily routine with git

Your usual working directory

Staging(preparing to

commit)

Commited

git add myfile.pygit add *.pygit add .

git commitgit commit –m”fixed bug 44”

1

23

You commit = you take a snapshot

Your daily routine with git

Your usual working directory

Staging(preparing to

commit)

Commited

Repository(on the web, or on

a server)

git pushgit pullgit fetch

We will look at this later on

Branching and Merging:

The story of a guy who wanted to

write some code.

(‘cause winter is coming)

I want to write a code on the

“distribution of temperatures

beyond the wall”

$ git init$ git add temp_distrib.py$ git commit -m "Initial Commit"

git init - creates a .git repository.

git add temp_distrib.py - stages the file (indexes it so that it is ready for commit).

git commit - records a snapshot of your file, with many other informations.

745a6ef

master

HEAD

Branch. The initial branch is always called master.

Working directory pointer: Where I am right now.

$ git init$ git add temp_distrib.py$ git commit -m "Initial Commit"[master (root-commit) 745a6ef] Initial Commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 file.txt

Contains the content of my file + my name and my email+ a time stamp + comment « Initial commit »+ checksum

745a6ef

master

HEAD

$ git add temp_distrib_2.py readme.txt$ git commit -m "Added temp_distrib_2 and a readme"  [master aa2507d] Added file2 file3 2 files changed, 2 insertions(+), 0 deletions(-)

aa2507d

745a6ef

master

HEAD

$ gedit temp_distrib_2.py$ git add temp_distrib_2.py$ git commit -m "Fixed bug 42 in temp_distrib_2"  [master 841036d] Changed mycode2 1 files changed, 2 insertions(+), 1 deletions(-)

aa2507d 841036d

I have a code that works, I even produced some interesting science

with it.

Unfortunately, I realize that I forgot to take in account the effect of the

big fires produced by the white walkers.

745a6ef

master

HEAD

$ git branch whitewalkers

aa2507d 841036d

whitewalkers

You just created a new branch.But, in your working directory, you are still on the master branch.

745a6ef

master

HEAD

$ git checkout whitewalkersSwitched to branch ‘whitewalkers'

aa2507d 841036d

whitewalkers

745a6ef

master

HEAD

$ gedit temp_distrib_2.py$ git add temp_distrib_2.py$ git commit -m "Made some changes for adding the effect of white walker fires function"[galaxies 54d9dde] 1 files changed, 2 insertions(+), 1 deletions(-)

aa2507d 841036d

whitewalkers

54d9dde

You know nothing, Jon Snow.

Your initial results can’t be right. You must have a bug

in your code.

Pffff. First she tries to kill me, now my code.

Let’s look at tis code…….

Wow she is right actually!

745a6ef

master

HEAD

$ git checkout masterSwitched to branch 'master'

aa2507d 841036d

whitewalkers

54d9dde

Jon is currently working on the whitewalkers branch. He does not want to mess it up, so he will correct the bug in the master branch (the wrong results that Ygritte spotted

were produced using this version).

745a6ef

master

HEAD

$ gedit temp_distrib_2.py$ git add temp_distrib_2.py$ git commit -m "Corrected bug discovered by Ygritte" [master 9143561] Changed temp_distrib_2 1 files changed, 2 insertions(+), 1 deletions(-)

aa2507d 841036d

whitewalkers

54d9dde

9143561

745a6ef

master

HEAD

aa2507d 841036d

whitewalkers

54d9dde

9143561

$ git checkout whitewalkersSwitched to branch ‘whitewalkers'

Now Jon wants to continue working on his new whitewalkers functions

master

HEAD

$ gedit whitewalk_properties.dat temp_distrib_2.py$ git add whitewalk_properties.dat temp_distrib_2.py$ git commit -m "Added the walkers properties table file and improved function snow() in temp_distrib_2.py" [galaxies 7201070] Added temp_distrib_2, whitewalk_properties 2 files changed, 2 insertions(+), 0 deletions(-)

aa2507d 841036d

whitewalkers

54d9dde

9143561

7201070

Now that the bug in my master branch is corrected, and that the

whitewalkers functions have been added, I want to merge my 2

branches.

master

HEAD

aa2507d 841036d

whitewalkers

54d9dde

9143561

7201070

$ git checkout masterSwitched to branch ‘master'

When you merge, you have to select from where to where. Here, we want to merge whitewalkers in master, so we checkout master…

aa2507d 841036d

whitewalkers

54d9dde

9143561

7201070

$ git merge whitewalkersMerge made by recursive. 2 files changed, 2 insertions(+), 1 deletions(-)

master

HEAD

e815ec4

This new version of the code, in branch master, contains :- The correction of the bug pointed out by Ygritte- The whitewalker functions added in branch whitewalkers

The “magic” merge command

How it (basicaly) works:1/ Looks at common ancestors in your

code (there can be many).2/ Creates a virtual ancestor from all

common ancestors.3/ Applies the changes that have been

done since this common ancestor.

Don’t be afraid: git merge is conservative. If two changes happen to be in contradiction, it won’t

merge, and ask you to resolve the conflict.

Resolving conflicts

You finally don’t want to merge ? git merge --abort

Just look at your(s) file(s) in your working directory: they have been commented where you have to

choose between two versions. Just save your code how you like it and commit !

Or use a visualization tool (git will look for what you have already, and suggest you some if you don’t).

How to work with Remotes(University servers, github.com,

bitbucket.com, etc…)

github.com/project.git

a6b4c f42c5

masterRemote server

github.com/project.git

a6b4c f42c5

master

Sources/

Remote server

Your computer

?

github.com/project.git

a6b4c f42c5

master

$ cd Sources$ git clone [email protected]/project.git

Sources/project

a6b4c f42c5

origin/master

master

Remote server

Your computer

github.com/project.git

a6b4c f42c5

master

Sources/project

a6b4c f42c5

31b8e 190a3

origin/master

master

Remote server

Your computer

Some code from Arya

github.com/project.git

a6b4c f42c5

master

Sources/project

a6b4c f42c5

31b8e 190a3

$ ...

a38de 893cf

origin/master

master

Remote server

Your computer

github.com/project.git

a6b4c f42c5

master

Sources/project

a6b4c f42c5

31b8e 190a3

$ git fetch origin

a38de 893cf

origin/master

master

31b8e 190a3

Remote server

Your computer

github.com/project.git

a6b4c f42c5

master

Sources/project

a6b4c f42c5

31b8e 190a3

$ git merge origin

a38de 893cf

origin/master

master

31b8e 190a3967ef

Remote server

Your

com

pute

r

github.com/project.git

master

Sources/project

a6b4c f42c5

$ git push origin

a38de 893cf

origin/master

master

31b8e 190a3967ef

a6b4c f42c5a38de 893cf

31b8e 190a3967ef

Remote server

Your

com

pute

r

Afraid of command line?

Use a graphic interface :

http://git-scm.com/downloads/guis

Or ask google “What is the best git gui for linux/windows/mac?”

There are plenty: GitEye, Giggle, gitK….

• A social network for hosting/sharing/merging/discuss git projects.

• Online repository for your git projects.

• Basic account is free (but in academia we can have a micro plan for free for 2 years).

github.com

github.com

A social network for hosting / sharing / merging / discussing git

projects.

Online repository for your git projects.

Basic account is free (but in academia we can have a micro plan

for free for 2 years).

github.com

github.com

Request a free micro plan for free for 2 year: https://education.github.com/

I can improvise you a quick example on my computer.

Or you can try this good tutorial and I can help you if you need it:

http://gitimmersion.com/

Important commands:http://rogerdudler.github.io/git-guide/files/git_cheat_sheet.pdf

Now…