44
A very informal introduction to Git Federico Galatolo Federico Galatolo A very informal introduction to Git 1 / 32

A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

A very informal introduction to Git

Federico Galatolo

Federico Galatolo A very informal introduction to Git 1 / 32

Page 2: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

What is not git?

Dropbox

Google Drive

iCloud

...

Federico Galatolo A very informal introduction to Git 2 / 32

Page 3: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

What is git?

Git is VCS (Version Control System).

VCS are about the changes not the files

Federico Galatolo A very informal introduction to Git 3 / 32

Page 4: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Git is more than a VCS

Git is a distributed VCS.

Each participant can have a different view of the state of the project.

Federico Galatolo A very informal introduction to Git 4 / 32

Page 5: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Repository

A Repository (or “repo”) is a data structure containing all project’s filesand history.

You can clone a remote repo with the clone command:

git clone <repo url>

Federico Galatolo A very informal introduction to Git 5 / 32

Page 6: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Special files

In a repo you can notice some files starting with .git

Those are special files and folders used to store the project history and toinstruct git.

.git A folder containing the project history (do not touch!)

.gitignore A file containing ignoring rules

.gitmodules A file containing submodules information

Federico Galatolo A very informal introduction to Git 6 / 32

Page 7: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Commit

C1 C2 C3 C4

A commit is a snapshot of the project in a given time.

Commits are immutable and represent a transition from a state toanother.

The commits are atomic units of modification within a project.

A commit is uniquely identified by its hash

Federico Galatolo A very informal introduction to Git 7 / 32

Page 8: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

How to commit

A git commit is a two-stages process.

Add files to the staging environment

Commit the changes

F1*

F2*

F4*

F3

Staging

F4*

F2*

Commit

Federico Galatolo A very informal introduction to Git 8 / 32

Page 9: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

How to commit

A git commit is a two-stages process.

Add files to the staging environment

Commit the changes

F1*

F2*

F4*

F3

Staging

F4*

F2*

Commit

Federico Galatolo A very informal introduction to Git 8 / 32

Page 10: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

How to commit

A git commit is a two-stages process.

Add files to the staging environment

Commit the changes

F1*

F2*

F4*

F3

Staging

F4*

F2*

Commit

Federico Galatolo A very informal introduction to Git 8 / 32

Page 11: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

How to commit

A git commit is a two-stages process.

Add files to the staging environment

Commit the changes

F1*

F2*

F4*

F3

Staging

F4*

F2*

Commit

Federico Galatolo A very informal introduction to Git 8 / 32

Page 12: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

How to commit(2)

Check modified files

git status

Check commits history

git log

Add files to the staging environment

git add file/folder

Commit the changes

git commit -m "commit message"

C1 C2

C3 C4

Federico Galatolo A very informal introduction to Git 9 / 32

Page 13: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

How to commit(2)

Check modified files

git status

Check commits history

git log

Add files to the staging environment

git add file/folder

Commit the changes

git commit -m "commit message"

C1 C2 C3

C4

Federico Galatolo A very informal introduction to Git 9 / 32

Page 14: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

How to commit(2)

Check modified files

git status

Check commits history

git log

Add files to the staging environment

git add file/folder

Commit the changes

git commit -m "commit message"

C1 C2 C3 C4

Federico Galatolo A very informal introduction to Git 9 / 32

Page 15: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

HEAD

C1 C2

HEAD

C3 C4

HEAD is a git variable that points to the most recent commit.

Federico Galatolo A very informal introduction to Git 10 / 32

Page 16: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

HEAD

C1 C2 C3

HEAD

C4

HEAD is a git variable that points to the most recent commit.

Federico Galatolo A very informal introduction to Git 10 / 32

Page 17: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

HEAD

C1 C2 C3 C4

HEAD

HEAD is a git variable that points to the most recent commit.

Federico Galatolo A very informal introduction to Git 10 / 32

Page 18: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Git superpowers

Your main git superpower is:

git reset <NEW HEAD>

With this command you can change HEAD and make it point to a differentcommit.

Change HEAD without changing the files

git reset <NEW HEAD>

Change HEAD changing the files

git reset --hard <NEW HEAD>

Federico Galatolo A very informal introduction to Git 11 / 32

Page 19: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Git superpowers(2)

For example with:

git reset --hard C2

C1 C2 C3 C4

HEAD

Federico Galatolo A very informal introduction to Git 12 / 32

Page 20: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Git superpowers(2)

For example with:

git reset --hard C2

C1 C2 C3 C4

HEAD

Federico Galatolo A very informal introduction to Git 12 / 32

Page 21: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Git superpowers(3)

Don’t worry, you are not going to mess it up.

If you git reset you will lose in git log all the subsequent commitreferences.

You can retrieve all HEAD history with

git reflog

Federico Galatolo A very informal introduction to Git 13 / 32

Page 22: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Learn Git Branching

We are going to use a tool called Learn Git Branching for the next slides.This tool allow you to visualize git commands in a graph.It uses a very simplified subset of git commands:

git commit

There is no concept of staging environment nor of filesYou can make a commit without message

git reset <hash>

It uses C1, C2, ... CN as hashesIt always implies the --hard behavior

git clone

Treats the repo as it has just been cloned from an identical origin

git fakeTeamwork

Creates a new commit in remote origin

Federico Galatolo A very informal introduction to Git 14 / 32

Page 23: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Try it yourself!(1)

Exercise 1

Federico Galatolo A very informal introduction to Git 15 / 32

Page 24: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Branches

C1 C2 C3 C4

master

A branch is a (semantically significant) collection of commits.

As a commit represent an atomic unit of modification,a branch represent a continuous flow of modifications.

A commit is always in a branch.

Federico Galatolo A very informal introduction to Git 16 / 32

Page 25: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Branches(2)

git branch

Show all the existing branches and the active one (denoted with anasterisk)

git branch <branch>

Create a new branch called <branch>

git checkout <branch>

Switch working on the branch <branch>

git checkout -b <branch>

Create <branch> and switch working on it

Federico Galatolo A very informal introduction to Git 17 / 32

Page 26: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Try it yourself!(2)

Exercise 2

Federico Galatolo A very informal introduction to Git 18 / 32

Page 27: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Merge(1)

You can merge different branches (or even single commits).Most of the times commits has been pushed in both branches.Don’t panic!

C1 C2 C3

master

feature

C4 C5

C6 C7

Federico Galatolo A very informal introduction to Git 19 / 32

Page 28: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Merge(1)

You can merge different branches (or even single commits).Most of the times commits has been pushed in both branches.Don’t panic!

C1 C2 C3

master

feature

C4

C5

C6 C7

Federico Galatolo A very informal introduction to Git 19 / 32

Page 29: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Merge(1)

You can merge different branches (or even single commits).Most of the times commits has been pushed in both branches.Don’t panic!

C1 C2 C3

master

feature

C4 C5

C6 C7

Federico Galatolo A very informal introduction to Git 19 / 32

Page 30: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Merge(1)

You can merge different branches (or even single commits).Most of the times commits has been pushed in both branches.Don’t panic!

C1 C2 C3

master

feature

C4 C5

C6

C7

Federico Galatolo A very informal introduction to Git 19 / 32

Page 31: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Merge(1)

You can merge different branches (or even single commits).Most of the times commits has been pushed in both branches.Don’t panic!

C1 C2 C3

master

feature

C4 C5

C6 C7

Federico Galatolo A very informal introduction to Git 19 / 32

Page 32: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Merge(2)

Don’t worry, git auto-merge is smart.

If the commits changed different files the merge is without conflicts.

If the commits changed different sections of the same files the merge iswithout conflicts.

If the commits changed the same sections of the same files (at leastonce) the commit is with conflict

git merge <branch>

Merges <branch> in the active branch

git merge --abort

Abort the merge and restore everything as it was before git merge

Federico Galatolo A very informal introduction to Git 20 / 32

Page 34: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Merge(3)

Handling conflicts is pretty easy.When a conflict is detected git puts in the place of the conflict:

Some non-conflicting text

<<<<<<< HEAD

CONFLICTING PORTION COMING FROM HEAD

=======

CONFLICTING PORTION COMING FROM bugfix

>>>>>>> bugfix

Some other non-conflicting text

Is then up to you keep the portions that you want and then git add andgit commit the changes

Federico Galatolo A very informal introduction to Git 22 / 32

Page 35: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Remotes

Git is distributed.

A remote is a reference to a remote instance of the repository.The default remote is called origin.If you clone a repository then origin points to the cloned repo.

git fetch <remote> <branch>

Fetch the commits from branch <branch> of <remote> in the localbranch <remote>/<branch>

git pull <remote> <branch>

Fetch the commits from branch <branch> of <remote> and mergethem in the local <branch>

git push <remote> <branch>

Push the state of the local branch to the origin branch branch

Federico Galatolo A very informal introduction to Git 23 / 32

Page 37: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Demo time

Lets see some examples in the real world!

Federico Galatolo A very informal introduction to Git 25 / 32

Page 38: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Git tags

Tags are a way to point specific points in time (commits) that representmilestones.

git tag <tag> <commit>

Tags <commit> with the tag <tag>

The default commit is HEAD

git checkout <tag>

Check out the tag <tag>

git push <remote> <tag>

Push the (newly created) tag <tag> to the remote <remote>

git push <remote> --tags

Push all the (newly created) tags to the remote <remote>

Federico Galatolo A very informal introduction to Git 26 / 32

Page 39: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

Cherry picking

Cherry picking is the action of merging into HEAD some cherry-pickedcommits.

git cherry-pick <commit>

Applies the changes from commit <commit> to HEAD

Federico Galatolo A very informal introduction to Git 27 / 32

Page 40: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

A successful Git branching model

Federico Galatolo A very informal introduction to Git 28 / 32

Page 41: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

GitHub

GitHub is git server.

over 37 Million Users

over 100 Million Repositories

Largest source code host in the world

Home of millions of free and open source projects

Federico Galatolo A very informal introduction to Git 29 / 32

Page 42: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

GitHub forks

In GitHub there is the concept of fork.A fork is a clone of someone else’s repository into yours repositories.

A fork is not a git clone

After a fork you own an exact copy of a repository.Copy of which you and only you are the administrator.

Federico Galatolo A very informal introduction to Git 30 / 32

Page 43: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

GitHub Pull Requests

If you want to ask for the integration of your changes in the forkedrepository you have to open a Pull Request.

In the pull request you have to specify the destination branch (originalrepository) and the source branch (your repository)

Federico Galatolo A very informal introduction to Git 31 / 32

Page 44: A very informal introduction to Git A very... · GitHub Pull Requests If you want to ask for the integration of your changes in the forked repository you have to open a Pull Request

That’s all folks!

You can find the slides PDF as well as their LATEX source code on GitHub.

https://github.com/galatolofederico/git-very-informal-introduction

R [email protected]

> @galatolo

� galatolo.me

� @galatolofederico

Federico Galatolo A very informal introduction to Git 32 / 32