Table des matières

Git

https://git-scm.com/docs/gitcore-tutorial

Directories

.git/objects/ : real repository data (identified by SHA-1) .git/refs/ : references to objects (heads of development, revision tags)

Remarquable files

.git/HEAD : Master head : points to default branch .git/index : describes your current working tree

Objects types and content

# RQ : objects are immutable git cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238 : gives the type of the file git cat-file blob 557db03de997c86a4a028e1ebd3a1ceb225be238 : gives the content of the file

Diff commands and comparisons (by Jon Loeliger)

            diff-tree
             +----+
             |    |
             |    |
             V    V
          +-----------+
          | Object DB |
          |  Backing  |
          |   Store   |
          +-----------+
            ^    ^
            |    |
            |    |  diff-index --cached
            |    |
diff-index  |    V
            |  +-----------+
            |  |   Index   |
            |  |  "cache"  |
            |  +-----------+
            |    ^
            |    |
            |    |  diff-files
            |    |
            V    V
          +-----------+
          |  Working  |
          | Directory |
          +-----------+

Configure information about git user

# git config --global user.email "user@domain"
# git config --global user.name "firstname lastname"

Initialise git repository

# mkdir test && cd test
# git init

Populating repository

# echo "Hello World" >hello

Index (cache)

# echo "Silly example" >example
# git update-index --add hello example

Get diff-files : difference between current working directory and index file

# git diff-files : raw summary
# git diff       : readable differences (= git diff-files -p)

Get diff-index

# git diff HEAD (= git diff-index -p HEAD) : differences between a commited tree and the working directory
# git diff-index --cached -p HEAD : compares a tree and the index cache content

Create a tree

# git write-tree : Raw way to create a tree

# tree=$(git write-tree)
# commit=$(echo 'Initial commit' | git commit-tree $tree)
# git update-ref HEAD $commit

Making a change

Process : working tree -> index file -> commited tree

# git rm <file>                           : remove files from index (=git update-index --remove <file>)
# git add <file                           : add files to index (= git update-index --add <file>)
# git update-index hello example          : updates index of files
# git diff                                : show differences with index
# git diff HEAD                           : show the differences with a tree (HEAD pointer tree)
# git commit                              : store a change in the repository, give a commit message !
                                          : all lines beginning with # will be ignored
                                      

Get diff-tree : difference between two trees

# git diff-tree -p HEAD          : difference between the commited tree and the master tree
# git diff-tree -p HEAD --pretty : idem, with metadata of commits

Tags

# git tag my-first-tag         : light tag (branch put in the .git/refs/tags/ diretory rather than .git/logs/refs/heads)
# git diff my-first-tag        : diff between the tag and the working directory
# git tag -s my-major-tag      : real git object, will sign current HEAD
# git tag <tagname> <mybranch> : tag from a branch

Branches

# git checkout -b <mybranch> : creates a branch (pointer into the Git object database in .git/refs/heads/)
# git checkout -b <mybranch> <earlier-commit> : creates a branch from an earlyer commit (tag or branch)
# git branch : displays the current branch

Copy locally a repository

# cp -a repo-src repo-dst
# cd repo-dst
# git reset # does :
                 git read-tree --reset HEAD  # total index rebuild
                 git update-index --refresh  # makes sure to match up all index entries with the checked-out files

Copy remotely a repository

# git clone git://git.kernel.org/pub/scm/git/git.git/ my-git
# cd my-git
# git checkout

Switch between tags and branches

# git checkout <name> : name can be a branch or a tag

Git, SVN way (with user groups) / server

Install git-core and git packages

# adduser git
# mkdir /pub
# mkdir /pub/my-repo.git
# chown -R git:git /pub/my-repo.git
# su - git
# cd /pub/my-repo.git
# git --bare init --shared

Gives SSH accces to this directory from the users. Remark : You can use git-shell in order to limit system exposure. As root:

# USERNAME="myuser"
# KEYSDIR="/data/keys"
# adduser --shell /usr/bin/git-shell --ingroup git --disabled-password ${USERNAME}
# mkdir -p ${KEYSDIR}/${USERNAME}
# cd ${KEYSDIR}/${USERNAME}
# ssh-keygen -t rsa -b 4096 -f ${USERNAME}_rsa_4096 -P ""
# mkdir /home/${USERNAME}/.ssh
# cp ${USERNAME}_rsa_4096.pub /home/${USERNAME}/.ssh/authorized_keys
# chmod 700 /home/${USERNAME}/.ssh
# chmod 600 /home/${USERNAME}/.ssh/authorized_keys
# chown -R ${USERNAME}:git /home/${USERNAME}/.ssh

Now send the ${USERNAME}_rsa_4096 file to your new user Ask him to put a passphrase to the key with :

# ssh-keygen -f <keyfile> -p

Ask him to put the file in ~/.ssh/git_<original key file name>

Git, SVN way (with user groups) / user

If not already done for the user :

# git config --global user.email "user@domain"
# git config --global user.name "firstname lastname"

1) Initial clone

# export GIT_SSH_COMMAND='ssh -i <private_key_file>'
# git clone foo.com:/pub/my-repo.git
# cd my-repo

Remark : either do the export each time you use a new shell, either configure the .ssh/config file

# tree=$(git write-tree)
# commit=$(echo 'Initial commit' | git commit-tree $tree)
# git update-ref HEAD $commit
# git push origin master

3) Make local changes then commit them locally

Do not forget git add/rm/mv

# git diff
# git diff -p HEAD
# git commit

Or :

# git commit -a

4) Fetch changes from remote repository (merge them with local repository - always commit before)

# git pull origin

4) Push the commits to the remote repository

# git push origin master

Then iterate to 3)