Git Model

Diagram of git model

git object image

Object & SHA1

  • Each object has a type, a size and a content

  • Each object is identified by a 40-digit SHA1 hash of attributes
    • 6ff87c4664981e4397625791c8ea3bbb5f2279a3

  • Each SHA1 can be shortened to the first digits
    • 6ff87c4664981e4397625
    • 6ff87c4

  • Object type can be blob, tree, commit or tag

Blob

blob
  • Used to store file data

  • Same content = same SHA1 = same blob

  • See it as a file

Tree

tree
  • Associate names to blobs and other trees

  • See it as a directory

Commit

commit
  • Used to snapshot a tree state

  • Has tree, parent(s), author, commiter and comment attributes

  • Not the same as SVN ones:
    • SVN store diffs
    • GIT store full state

Tags

tags
  • Reference an object

  • Has object, type, tagger and comment attributes.

  • Not used for lightweight tags
    • Simple pointer on a commit (like branches)

Branch

branch
  • A branch is simply a pointer to a certain commit.

  • A branch is not aa GIT object (no SHA1)

Basic Operations


Configuration

Set your name and email:

$ git config --global core.name "Me"
$ git config --global core.email "me@company.com">

Display your config

$ git config --global
$ git config --local # in a repository

Staging

Stage your changes:

# Add to index / stage
$ git add file.txt
# Add all modified and new files (tracked or not) to index
$ git add -A
# Partial staging
$ git add -p file.txt

Commits

Create a commit into the current branch:

# Commit from index
$ git commit
# Commit from tracked file list
$ git commit file1.txt file2.txt
# All modified tracked files
$ git commit -a
# Commit from pattern
$ git commit **/*.py

See your repository

See the current status:

$ git status

Retrieve your history

# Log integral 
$ git log
# 5 dernier commits
$ git log -5
# Diff between two branches
$ git log origin/master..master

Branching and Merging


Create a branch

$ git branch feature     # Create the branch
$ git checkout feature # Switch to the new branch
# or in a single command
$ git checkout -b feature
branch creation

Branch diverging

Branches diverge when they have different commits

branch diverging

Merge

Create a merge commit and keep your branch history:

$ git merge feature
merge

Rebase

Re-apply your commits and keeps your history linear:

$ git rebase master 
# or interactive version
$ git rebase -i master
rebase

Working with Remote Repository

  • It's only branches
  • Repository synchronization operations:

$ git fetch 
$ git push
$ git pull # fetch + merge
$ git pull --rebase # fetch + rebase
remote repository

Add a remote

$git remote add origin git://somewhere.git 
$git fetch
add remote

Diverging with remote

It’s just more branches !

diverging with remote

Refrences