Difference between revisions of "Git"

From ITSwiki
Jump to: navigation, search
[unchecked revision][unchecked revision]
(Access rules)
Line 1: Line 1:
= How to get an account =
+
= Gitlab =
 +
To use git for your own and team projects DTU Compute is providing a GitLab instance with all the benefits it offers. Primarily a place for your repositories, other features are or will become available.
  
DTU Compute employees and Ph.D's can get a git account by sending an email to [mailto:itSupport@compute.dtu.dk IT Service] with  your SSH public key (as email attachment) which you find in <code>~/.ssh/id_rsa.pub</code>. Only RSA keys works as DSA have been [https://security.stackexchange.com/questions/112802/why-openssh-deprecated-dsa-keys deprecated].
 
  
It is important you you attach the public key to the email and not inline it.
+
Login with your DTU credentials here:
  
If you don't have a public key then you can create one by
 
<pre>
 
ssh-keygen -N '' -t rsa -f ~/.ssh/id_rsa
 
</pre>
 
  
It is important that you keep your private key (<code>~/.ssh/id_rsa</code>) 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.
+
https://lab.compute.dtu.dk
  
== 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.
+
== Add your public keys ==
 
+
To access your private repositories, add your public SSH-key to your GitLab account.<br/>
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 you don't have a SSH-key, on Linux/MacOS you can use this command in your favourite shell:
 
+
<pre style="background-color: #374048; color: white; border-style: none; padding: 5px; width: 75%;">
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.
+
ssh-keygen
 
+
Linus is not shy to say that Git is not designed to contain big binary blobs. [http://osdir.com/ml/git/2009-05/msg00051.html Example].
+
 
+
If you have a need for version control on big amounts binary blobs of data (e.g. images), then please contact [mailto:ITservice@compute.dtu.dk IT Service] and will try to find a solution for this.
+
 
+
=== Disk usage output ===
+
 
+
Everytime a <code>git push</code> is made by any of the git admin's users the total amount of used disk space is output.
+
 
+
Example:
+
 
+
<pre>
+
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.compute.dtu.dk have used 3 MB out of 1000 MB.              <-------------
+
remote:                                                                   <-------------
+
To user4@git.compute.dtu.dk:test.git
+
  78b3caf..25b388e  master -> master
+
mttj@pcmttj:~/git-test/test$
+
 
</pre>
 
</pre>
 
+
Press <code>Enter</code> 3 times to create a password-less key pair, located in:
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:
+
 
<pre>
 
<pre>
mttj@pcmttj:~/git-test/test$ touch 8 && git add 8 && git commit -a -m "test" && git push
+
$HOME/.ssh/id_rsa
[master e223957] test
+
$HOME/.ssh/id_rsa.pub
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.compute.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.compute.dtu.dk:testgit
+
! [remote rejected] master -> master (pre-receive hook declined)        <-------------
+
error: failed to push some refs to 'user4@git.compute.dtu.dk:test.git'        <-------------
+
mttj@pcmttj:~/git-test/test$
+
 
</pre>
 
</pre>
 +
The file <code>id_rsa.pub</code> is your public key which you can share with other systems, including DTU Compute's GitLab.
  
= Creating repositories and setting permissions =
 
  
The URL structure is
+
To add your public key in GitLab, follow these steps after login:
dtuInitials@git.compute.dtu.dk:repositoryName
+
  
where <code>dtuInitials</code> should be replaced by your DTU initials which are 4 or more letters long.
 
  
<code>dtuInitials</code> is the git admin and can create as many repositories as needed.
+
1) In the top-right corner click your user icon<br/>
 +
2) Click '''Settings''' and on the left-hand panel click '''SSH Keys '''<br/>
 +
3) Follow the instructions from here<br/>
  
Once your git admin account have been created, you can clone the <code>gitolite-admin</code> repo by
+
== Give access to other users ==
<pre>
+
To share a repository with other users, the user you want to add has to login to GitLab first. Then you can find and add them as a member of your project.
git clone dtuInitials@git.compute.dtu.dk:gitolite-admin
+
</pre>
+
 
+
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 <code>gitolite-admin/conf/gitolite.conf</code>
+
* Specify which users should be allowed and with what permissions
+
* Rename the users public key
+
* Add the users key to <code>gitolite-admin/keydir/</code>
+
* git add, commit, push <code>gitolite-admin</code>
+
 
+
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 <code>gitolite-admin/conf/gitolite.conf</code>.
+
 
+
Example:
+
<pre>
+
@staff            =  dilbert alice
+
@projects          =  mobile fast_algo
+
 
+
repo @projects inv_prob
+
  RW+            =  @staff
+
  -      master  =  ashok
+
  RW              =  ashok
+
  R              =  wally
+
</pre>
+
 
+
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 <code>#</code>.
+
 
+
User names and repo names must start with an alphanumeric, but after that they can also contain <code>.</code>, <code>_</code>, or <code>-</code>.
+
 
+
Usernames can optionally be followed by an <code>@</code> and a domainname containing at least one <code>.</code> (this allows you to use an email address as someone's username). Repo names can contain <code>/</code> 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|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:
+
<pre>
+
@developers    =  dilbert alice wally
+
</pre>
+
Group definitions accumulate; this is the same as the above:
+
<pre>
+
@developers    =  dilbert
+
@developers    =  alice
+
@developers    =  wally
+
</pre>
+
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).
+
<pre>
+
@developers    =  dilbert alice
+
@interns        =  ashok
+
@staff          =  @interns @developers
+
@developers    =  wally
+
 
+
# wally is NOT part of @staff
+
</pre>
+
 
+
<code>@all</code> 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 <code>$HOME/.ssh/id_rsa.pub</code> 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 <code>gitolite-admin/conf/gitolite.conf</code>.
+
 
+
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 <code>gitolite-admin/conf/gitolite.conf</code> and add it, along with at least one user with some permissions. Or add it to an existing repo line:
+
<pre>
+
repo fast_also inv_prob
+
    RW+    =  dilbert
+
    RW  dev =  alice ashok
+
    R      =  @all
+
</pre>
+
 
+
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:
+
<pre>
+
@myrepos    =  fast_algo
+
@myrepos    =  inv_prob
+
 
+
repo @myrepos
+
    RW      =  alice
+
</pre>
+
 
+
Finally, you add, commit, and push this change. When done an empty repo have been created on git.compute.dtu.dk.
+
 
+
== Accessing a repo for the first time ==
+
 
+
git clone dtuInitials@git.compute.dtu.dk:repositoryName
+
 
+
make the desired changes and instead of the normal <code>git push</code> do
+
 
+
git push origin master
+
 
+
Here after <code>git push</code> should be used.
+
 
+
== Deleting a repo ==
+
 
+
Remove the repo from <code>gitolite-admin/conf/gitolite.conf</code> commit and push the change and send an email to [mailto:ITservice@compute.dtu.dk 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 [mailto:ITservice@compute.dtu.dk IT Service] and ask then to rename the repo.
+
 
+
When IT Service have renamed the repo, you can rename the repo in <code>gitolite-admin/conf/gitolite.conf</code> and then commit and push the change.
+
 
+
== Moving existing repos into git.compute.dtu.dk ==
+
 
+
The purpose is to put the local (non-bare) git repository onto <code>git.compute.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.compute.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>.
+
 
+
Clone the <code>pretty</code> repository
+
git clone dtuInitials@git.compute.dtu.dk:pretty
+
 
+
The following will create a local repository called <code>ugly</code>, 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 <code>ugly</code> repo to the remote <code>pretty</code> repo.
+
git remote add origin dtuInitials@git.compute.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 <code>ugly</code> repo.
+
 
+
At this point you no longer need your <code>ugly</code> 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:
+
<pre>
+
@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
+
</pre>
+
 
+
# Everyone, even wally, can read the repo.
+
# Dilbert can push, rewind, or delete any ref.
+
# Alice can push, rewind, or delete any ref whose name starts with 'dev'.
+
# Alice can also push (but not rewind or delete) in branch temp. This applies to bob also.
+
# If it weren't for line 3, the previous statement would apply to wally also.
+
 
+
= Possible issues
+
 
+
If you get permission denied, then try
+
 
+
<pre>
+
chmod 600 ~/.ssh/id*
+
chmod 700 ~/.ssh
+
</pre>
+
  
= Git manuals =
 
  
Besides the manpages (e.g. <code>man git-push</code>) are TONS of documentation and manuals online. A few examples:
+
1) Select your project you want to share<br/>
 +
2) In the left-hand panel select '''Settings -> Members'''<br/>
 +
3) Under '''Select members to invite''' search for the username<br/>
  
* http://tfx.dk/doku.php?id=article:git
+
== More to come ==
* http://gitref.org/
+
* http://spheredev.org/wiki/Git_for_the_lazy
+
* http://book.git-scm.com/
+
* http://www.kernel.org/pub/software/scm/git/docs/
+
  
 
[[Category:IT]]
 
[[Category:IT]]

Revision as of 19:48, 31 October 2018

Gitlab

To use git for your own and team projects DTU Compute is providing a GitLab instance with all the benefits it offers. Primarily a place for your repositories, other features are or will become available.


Login with your DTU credentials here:


https://lab.compute.dtu.dk


Add your public keys

To access your private repositories, add your public SSH-key to your GitLab account.
If you don't have a SSH-key, on Linux/MacOS you can use this command in your favourite shell:

ssh-keygen

Press Enter 3 times to create a password-less key pair, located in:

$HOME/.ssh/id_rsa
$HOME/.ssh/id_rsa.pub

The file id_rsa.pub is your public key which you can share with other systems, including DTU Compute's GitLab.


To add your public key in GitLab, follow these steps after login:


1) In the top-right corner click your user icon
2) Click Settings and on the left-hand panel click SSH Keys
3) Follow the instructions from here

Give access to other users

To share a repository with other users, the user you want to add has to login to GitLab first. Then you can find and add them as a member of your project.


1) Select your project you want to share
2) In the left-hand panel select Settings -> Members
3) Under Select members to invite search for the username

More to come