Git



Git

0 2


slides-git

Slides on git

On Github themouette / slides-git

Git

By Julien Muetton / @themouette

Setup

Download an install

Refer to online documentation

git help

To get some help on a command.

$ man git-commit
$ git help
$ git help commit

Git Book is a golden mine too.

Generate an ssh key

$ cd ~
$ ssh-keygen -t rsa
$ cat id_rsa.pub
ssh-rsa
AAAAB3NzaC1yc2EAAAABIwAAAQEAyyA8wePstPC69PeuHFtOwyTecByonsHFAjHbVnZ+h0dpomvLZxUtbknNj3+
c7MPYKqKBOx9gUKV/diR/mIDqsb405MlrI1kmNR9zbFGYAAwIH/Gxt0Lv5ffwaqsz7cECHBbMojQGEz3IH3twEvDfF6cu5p
00QfP0MSmEi/eB+W+h30NGdqLJCziLDlp409jAfXbQm/4Yx7apLvEmkaYSrb5f/pfvYv1FEV1tS8/J7DgdHUAWo6gyGUUSZ
JgsyHcuJT7v9Tf0xwiFWOWL9WsWXa9fCKqTeYnYJhHlqfinZRnT/+jkz0OZ7YmXo6j4Hyms3RCOqenIX1W6gnIn+eQIkw==
my computer

Then add the public key content to github, bitbucket, gitlab or any other server and test everything is all right.

$ ssh git@github.com
Hi themouette! You've successfully authenticated, but GitHub does not provide shell access.

Configure your identity

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

Configure your editor

Default is vi, but if you really want to change:

$ git config --global core.editor emacs
$ git config --global core.editor \
    "'C:/Program Files/Notepad++/notepad++.exe' \
    -multiInst -notabbar -nosession -noPlugin"

Vi cheat sheet

Configure your diff tool

$ git config --global merge.tool vimdiff

Kdiff3

Add some alias

Version Control

A bit of theory

Definition

Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.

Local

Local version system put in the same place versions and working directory.

Centralised

In centralized version control everything is tracked on the server, clients just check out the latest snapshot of the files.

Decentralized

In decentralized version control, every client mirror the whole repository.

GIT is decentralized

Getting Started

Local first

Everything is local, remote operations are only for sharing.

git init

Create an empty git repository or reinitialize an existing one.

$ git init

Initialized empty Git repository in ~/.git/

Running git init in an existing repository will not overwrite things that are already there.

Commit

A commit is a snapshot of your working tree and a pointer to previous commit.

Three Zones

Everything in GIT is in one of those following area.

  • Working directory: the directory you work in;
  • Staging area: prepare next commit;
  • Repository: history of every committed change.

Three Zones

Local Repository

git add file

Add file contents to the staging area.

$ git add README

Track a file for the first time.

Track changes on file since previous commit.

git checkout [file]

Checkout a branch or paths to the working tree.

$ git checkout README
$ git checkout my-branch

Reset file/path to staging area's version.

Switch branch.

git commit

Create a commit from current staging area's status.

$ git commit
[master 8a6e1a3] My awesome message.D
2 files changed, 10 insertions(+), 4 deletions(-)
create mode 100644 img/01/branch.png

Returns the commit hash 8a6e1a3.

To edit your latest commit, use git commit --amend.

git status

Show current working dirrectory and staging area.

git mv src destination

Rename src into destination.

git rm file

Delete file from tracking.

git log [file]

Show commit logs.

$ git log

commit 8fbdc87e2f254c1c52ee9c2cae886985421fd3ef
Author: Julien Muetton <themouette@gmail.com>
Date:   Mon Aug 26 13:30:52 2013 +0200

    Init as an npm package

commit f8db4d12eea0174b8e8476c21510133e3bbd27e3
Author: Julien Muetton <themouette@gmail.com>
Date:   Mon Aug 26 13:24:20 2013 +0200

    First commit
$ git log --graph --decorate  # Display network graph
$ git log --stat # show stats
$ git log --pretty=oneline --abbrev-commit # A commit per line

git diff [file]

Show changes between two states (commits, staging, working directory...)

$ git diff
diff --git a/index.html b/index.html
index de3bfb6..b3d2ddf 100644
--- a/index.html
+++ b/index.html
@@ -36,10 +36,16 @@

some awesome content
- some former content
+ some new content

git diff topic..master to compare branches.

git diff HEAD--8a6e1a3 to compare commits.

git stash

Stash the changes in a dirty working directory away.

$ git stash

git stash list to list stashs.

git stash apply to apply latest stash.

git stash pop to apply and delete latest stash.

git blame file

Show what revision and author last modified each line of a file.

$ git blame README

2c2513c7 (Julien Muetton    2013-08-26 18:48:45 +0200   1) # GIT basics
2c2513c7 (Julien Muetton    2013-08-26 18:48:45 +0200   2)
2c2513c7 (Julien Muetton    2013-08-26 18:48:45 +0200   3) ---
2c2513c7 (Julien Muetton    2013-08-26 18:48:45 +0200   4)
2c2513c7 (Julien Muetton    2013-08-26 18:48:45 +0200   5) ## Local first

Transitions

Branches

Linear History

Typical git repository

Tag

A Tag is a label on a commit.

Branches

The top of a linked list has a name: the BRANCH name.

Notes On Tags And Branches

  • HEAD is a symbolic ref, which points to the current branch.
  • Whereas branches moves, tags doesn't

git branch

List, create, or delete branches

(master) $ git branch feature

(master) $ git branch
    * master
    feature

(master) $ git branch -d feature

Only local branches can be managed

git checkout my-branch

Switch working directory to my-branch

(master) $ git branch feature
(master) $ git checkout feature

(my-branch) $ git branch
    master
    * feature

You can use shortcut git checkout -b my-branch

git rebase master

Replay the current branch on top of master.

(feature) $ git rebase master

If merge is not obvious, git asks you to solve conflict manually.

git merge

Join two or more development histories together.

(master) $ git merge feature

git merge Fast Forward

(master) $ git merge feature

git merge --no-ff

(master) $ git merge feature --no-ff

Using --no-ff keeps track of the feature branch.

git cherry-pick

Pick a commit and apply it on current HEAD.

(master) $ git cherry-pick 8a6e1a3

Note on conflicts

$ git merge iss53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

$ git diff

<<<<<<< HEAD:index.html
<div id="footer">contact : email.support@github.com</div>
=======
<div id="footer">
please contact us at support@github.com
</div>
>>>>>>> iss53:index.html

Git is verbose, but for your first conflict...

Enter remotes

Remote

A remote is a mirror of your local repository.

git clone remote [path]

Clone a repository into a new directory.

$ git clone git@github.com/themouette/fossil-core.git

(master) $ git branch -a
    * master
    remotes/origin/HEAD -> origin/master
    remotes/origin/master

You can use http(s) or ssh to communicate with remotes.

Default remote is names origin.

git remote

Manage remote mirrors.

$ git remote
origin

$ git remote show origin
* remote origin
Fetch URL: git@github.com:themouette/fossil-core.git
Push  URL: git@github.com:themouette/fossil-core.git
HEAD branch: master
Remote branch:
    master tracked
Local branch configured for 'git pull':
    master merges with remote master
Local ref configured for 'git push':
    master pushes to master

You can add multiple remotes.

git fetch

Download objects and refs from another repository.

$ git fetch origin

Note that nothing changes in your local direcory.

git pull [--rebase] remote branch

Fetch and merge or rebase remote branch.

$ git pull --rebase origin master

Equivalent to:

$ git fetch origin/master
$ git rebase origin/master

git push remote branch

Update remote refs along with associated objects.

$ git push origin serverfix
Counting objects: 20, done.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (15/15), 1.74 KiB, done.
Total 15 (delta 5), reused 0 (delta 0)
To git@github.com:schacon/simplegit.git
* [new branch]      serverfix -> serverfix

Only fast forward commits can be pushed

Extras

Submodules

Avoid submodules

git submodule add https://github.com/hakimel/reveal.js.git reveal.js

git submodule update --init

Hooks

Extend Git behaviour

Client Side

Commit Workflow

  • pre-commit
  • prepare-commit-msg
  • commit-msg
  • post-commit

Email Workflow

(when using git am)

  • format-patch
  • applypatch-msg
  • pre-applypatch
  • post-applypatch

Other

  • post-checkout
  • post-merge
  • pre-rebase
  • pre-push

Server Side

  • pre-receive
  • update
  • post-receive

Learn more on Git Hooks

Become Social

Common Workflows

Centralized Workflow

Similar to centralized version control, everybody can edit central repository.

Blessed Repository

Developer create a request to merge it's changes.

Release masters chose which requests to merge.

Lieutenant Workflow

In huge projects, lieutenants are trusted individuals in charge of reviewing requests.

Under the hood

Anatomy Of A Repository

.git directory

.git
|-- branches/
|-- hooks/
|-- info/
|-- logs/
|-- modules/
|-- objects/
|-- refs/
|-- COMMIT_EDITMSG
|-- config
|-- description
|-- HEAD
|-- index
`-- packed-refs

Configuration

3 Types Of Objects

  • blob
  • tree
  • commit

Blob

Each FILE that you add to the repo is turned into a BLOB object.

tree

Each DIRECTORY is turned into a TREE object.

Obviously, a tree object can contain other tree objects and blob objects, just like a directory can contain other directories and files.

Commit

A commit is a snapshot of your working tree and a pointer to previous commit.

References

  • tag
  • branch

Resources

Online

slides

videos

Thank you

Questions

http://themouette.github.io/slides-git/?theme=clermontech