Difference between revisions of "Git"

From ITSwiki
Jump to: navigation, search
[unchecked revision][unchecked revision]
(Moving existing repos into git.imm.dtu.dk)
Line 219: Line 219:
 
== Moving existing repos into git.imm.dtu.dk ==
 
== Moving existing repos into git.imm.dtu.dk ==
  
The purpose is to put the local (non-bare) git repository onto <code>git.imm.dtu.dk</code>. Non-bare means that the local repository was created without <code>--bare</code> like so <code>git init ugly</code>, where <code>ugly</code> is the name of the repository.
+
The purpose is to put the local (non-bare) git repository onto <code>git.imm.dtu.dk</code>. Non-bare means that the local repository was created without <code>--bare</code>, like so: <code>git init ugly</code>, where <code>ugly</code> is the name of the repository.
  
 
First create the repository on <code>git.imm.dtu.dk</code> as described in [[#Creating repositories and setting permissions|Creating repositories and setting permissions]]. For this example it will be called <code>pretty</code>.
 
First create the repository on <code>git.imm.dtu.dk</code> as described in [[#Creating repositories and setting permissions|Creating repositories and setting permissions]]. For this example it will be called <code>pretty</code>.

Revision as of 17:13, 1 November 2013

How to get an account

DTU Compute employees and Ph.D's can get a git account by sending an email to IT Service with your SSH public key (as email attachment) which you find in ~/.ssh/*pub. E.g. ~/.ssh/id_rsa.pub.

It is important you you attach the public key to the email and not inline it.

If you don't have a public key then you can create one by

ssh-keygen -N '' -t rsa -f ~/.ssh/id_rsa

It is important that you keep your private key (~/.ssh/id_rsa) and public key safe and have a backup. Should someone get hold of your private key, then all your git repositories can get accessed by that person.

Disk space

By default the git admin get 1GB of disk space and a hard limit of 1,5GB, where all the git admin's repositories combined can not exceed these limits.

This means that if you have created e.g. 3 repos and have added some users to these, and if just one of these users adds a big file by mistake, and exceed the 1GB limit, then no one will be allowed to push to any of the git admin's repos.

If the disk usage is e.g. 900MB, and a user pushes a commit of 700MB, then the file system will stop writing when 1,5GB is reached, and there is a potential data loss for the repos owned by the git admin.

Linus is not shy to say that Git is not designed to contain big binary blobs. Example.

If you have a need for version control on big amounts binary blobs of data (e.g. images), then please contact IT Service and will try to find a solution for this.

Disk usage output

Everytime a git push is made by any of the git admin's users the total amount of used disk space is output.

Example:

mttj@pcmttj:~/git-test/test$ touch 7 && git add 7 && git commit -a -m "test" && git push
[master 25b388e] test
 0 files changed
 create mode 100644 7
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 220 bytes, done.
Total 2 (delta 1), reused 0 (delta 0)
remote:                                                                   <-------------
remote: user4@git.imm.dtu.dk have used 3 MB out of 1000 MB.               <-------------
remote:                                                                   <-------------
To user4@git.imm.dtu.dk:test.git
   78b3caf..25b388e  master -> master
mttj@pcmttj:~/git-test/test$ 

If a user have made a big commit that have exceeded the quota limit then no one can push to any of the git admin's repo's.

Example:

mttj@pcmttj:~/git-test/test$ touch 8 && git add 8 && git commit -a -m "test" && git push
[master e223957] test
 0 files changed
 create mode 100644 8
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 228 bytes, done.
Total 2 (delta 1), reused 0 (delta 0)
remote:
remote: user4@git.imm.dtu.dk have used 3 MB and exceeded quota of 1 MB.   <-------------
remote: No changes have been made to remote repository.                   <-------------
remote:
To user4@git.imm.dtu.dk:testgit
 ! [remote rejected] master -> master (pre-receive hook declined)         <-------------
error: failed to push some refs to 'user4@git.imm.dtu.dk:test.git'        <-------------
mttj@pcmttj:~/git-test/test$ 

Creating repositories and setting permissions

The URL structure is

dtuInitials@git.imm.dtu.dk:repositoryName

where dtuInitials should be replaced by your DTU initials which are 4 or more letters long.

dtuInitials is the git admin and can create as many repositories as needed.

Once your git admin account have been created, you can clone the gitolite-admin repo by

git clone dtuInitials@git.imm.dtu.dk:gitolite-admin

This repository is where you create repositories, add users, and set permissions for the users.

The procedure to create a repo and add a user is:

  • Specify the repo in gitolite-admin/conf/gitolite.conf
  • Specify which users should be allowed and with what permissions
  • Rename the users public key
  • Add the users key to gitolite-admin/keydir/
  • git add, commit, push gitolite-admin

All this will be explained in details in the coming sections.

Example

The git admin is the only one by default who can pull the gitolite-admin repo.

To add a repo edit gitolite-admin/conf/gitolite.conf.

Example:

@staff             =   dilbert alice
@projects          =   mobile fast_algo

repo @projects inv_prob
   RW+             =   @staff
   -       master  =   ashok
   RW              =   ashok
   R               =   wally

Aside from this the public keys from dilbert, alice, ashok, and wally have to be added, and finally everything committed and pushed.

Syntax

In general, everything is space separated; there are no commas, semicolons, etc., in the syntax. Comments starts with #.

User names and repo names must start with an alphanumeric, but after that they can also contain ., _, or -.

Usernames can optionally be followed by an @ and a domainname containing at least one . (this allows you to use an email address as someone's username). Repo names can contain / characters (this allows you to put your repos in a tree-structure for convenience).

There are no continuation lines by default. You do not need them; the Groups section will tell you how you can break up large lists of names in a group definition into multiple lines.

Groups

You can group repos or users for convenience. The syntax is the same for both and does not distinguish; until you use the group name it could really be either.

Here's an example:

@developers     =   dilbert alice wally

Group definitions accumulate; this is the same as the above:

@developers     =   dilbert
@developers     =   alice
@developers     =   wally

You can use one group in another group definition; the values will be expanded right there (meaning later additions will not appear in the second group).

@developers     =   dilbert alice
@interns        =   ashok
@staff          =   @interns @developers
@developers     =   wally

# wally is NOT part of @staff

@all is a special group name that is often convenient to use if you really mean "all repos" or "all users".

Adding and removing users

To add a user, e.g. Alice, obtain her public key (typically $HOME/.ssh/id_rsa.pub and then

mv id_rsa.pub gitolite-admin/keydir/alice.pub
cd gitolite-admin
git add keydir/alice.pub
git commit -a -m "Added alice.pub"
git push

The name of the key (alice in this case) will be the name you should use in gitolite-admin/conf/gitolite.conf.

Adding a key as just described doesn't give the user access to anything. Which repositories and what permissions the user should have is set in the repo definition, which is explained in the next section.

To remove a user

cd gitolite-admin
git rm keydir/alice.pub
git commit -a -m "Removed alice.pub"
git push

This will deny alice access to all repositories despite what permissions are given to alice in the repositories.

Adding and removing repos

To add a new repo, edit gitolite-admin/conf/gitolite.conf and add it, along with at least one user with some permissions. Or add it to an existing repo line:

repo fast_also inv_prob
    RW+     =   dilbert
    RW  dev =   alice ashok
    R       =   @all

The repo line can have any number of repo names or repo group names in it. If you have too many, use a group name:

@myrepos    =   fast_algo
@myrepos    =   inv_prob

repo @myrepos
    RW      =   alice

Finally, you add, commit, and push this change. When done an empty repo have been created on git.imm.dtu.dk.

Accessing a repo for the first time

git clone dtuInitials@git.imm.dtu.dk:repositoryName

make the desired changes and instead of the normal git push do

git push origin master

Here after git push should be used.

Deleting a repo

Remove the repo from gitolite-admin/conf/gitolite.conf commit and push the change and send an email to IT Service and ask then to delete the repo. It have to be done on the server side.

Renaming a repo

Send an email to IT Service and ask then to rename the repo.

When IT Service have renamed the repo, you can rename the repo in gitolite-admin/conf/gitolite.conf and then commit and push the change.

Moving existing repos into git.imm.dtu.dk

The purpose is to put the local (non-bare) git repository onto git.imm.dtu.dk. Non-bare means that the local repository was created without --bare, like so: git init ugly, where ugly is the name of the repository.

First create the repository on git.imm.dtu.dk as described in Creating repositories and setting permissions. For this example it will be called pretty.

Clone the pretty repository

git clone dtuInitials@git.imm.dtu.dk:pretty

The following will create a local repository called ugly, to simulate your existing repository

mkdir ugly
cd ugly
git init
echo abc > text.txt
git add text.txt 
git commit -m "init"
echo abcd > text2.txt
git add text2.txt 
git commit -m "added text2.txt"

Move your ugly repo to the remote pretty repo.

git remote add origin dtuInitials@git.imm.dtu.dk:pretty
git push origin master

Test

cd ../pretty
git pull
git log

and you should new see the change log from your once ugly repo.

At this point you no longer need your ugly repo any more.

Access rules

A rule has the structure

<permission> <zero or more branches> = <one or more users/user groups>

The most common permissions used are:

RW+   pull, push, "push -f", ref deletion allowed
RW,   pull, push, push existing ref, create new ref, fast-forward push a ref. No rewinds or deletes.
R     read only
-     deny access

Example:

@staff          =   dilbert alice wally bob

repo fast_algo
    RW+         =   dilbert   # line 1
    RW+ dev     =   alice     # line 2
    -           =   wally     # line 3
    RW  temp    =   @staff    # line 4
    R           =   ashok     # line 5
  1. Everyone, even wally, can read the repo.
  2. Dilbert can push, rewind, or delete any ref.
  3. Alice can push, rewind, or delete any ref whose name starts with 'dev'.
  4. Alice can also push (but not rewind or delete) in branch temp. This applies to bob also.
  5. If it weren't for line 3, the previous statement would apply to wally also.

Git manuals

Besides the manpages (e.g. man git-push) are TONS of documentation and manuals online. A few examples: