Hosting your Git repositories. This tutorial explains how to use Bitbucket as hosting provider for your Git repository and how you can install a Git server on your own machine.
1. Git Hosting Provider
Git allows you to host your own Git server. Instead of setting up your own server, you can also use a hosting service. The most popular Git hosting sites are GitHub and Bitbucket. Both offer free hosting with certain limitations.
2. Authentication via SSH
2.1. The concept of SSH
Most Git (and Gerrit) servers support SSH based authentication. This requires a SSH key pair for automatic authentication.
An SSH key par consists of a public and private key. The public key is uploaded to the application you want to authenticate with. The application has no access to the private key. If you interact with the hosting provider via the ssh protocol, the public key is used to identify a user who encrypted the data during communication with the corresponding private key.
2.2. SSH key pair generation
To create an SSH key under Linux (or Windows / Mac with OpenSSH
installed) switch to the command line and
execute
the following
commands.
The generated SSH
key is by default
located in the
.ssh
directory of the user home directory. Ensure that you backup existing
keys in
this directory
before running the following commands.
# Switch to your .ssh directory
cd ~/.ssh
# If the directory
# does not exist, create it via:
# mkdir .ssh
# Manually backup all existing content of this dir!!!
# Afterwards, generate the ssh key
ssh-keygen -t rsa -b 4096 -C "your_email@youremail.com"
# Press enter to select the default directory
# You will be prompted for an optional passphrase
# A passphrase protects your private key
# but you have to enter it manually during ssh operations
The Eclipse IDE allows you to create an SSH key pair via .

It is a good practice to use a
passphrase to protect your
private key. It
is also good practice to
use
operating system level permission settings
to ensure that only
the
owning user can access the
~/.ssh
folder and its content.
In the above
ssh-keygen
command the -C parameter is a comment. Using your email is good
practice so that
someone looking at your public key can contact you
in
case they have
questions. Including the email enables system
administrators
to contact the person in case of questions.
|
The result will be two files,
id_rsa
which is your private key and
id_rsa.pub
which is your public key.
You find more details for the generation of an SSH key on the following webpages: GitHub Help: description of SSH key creation or OpenSSH manual.
You can specify alternative key names with the
-f
parameter on the
command line.
This is helpful if you have multiple
different
repositories and you
want to have a different key for each
one. For
example, you can name
your SSH keys in domain
name format,
e.g.,
eclipse.org
and
eclipse.org.pub
as well as
github.com
and
github.com.pub.
|
You need
additional configuration in the
.ssh/config
file, because
only
the
id_rsa
will be picked up by
default. The following code shows an
example.
Host *.eclipse.org
IdentityFile ~/.ssh/eclipse.org
Host *.github.com
IdentityFile ~/.ssh/github.com
3. Bitbucket
3.1. What is Bitbucket?
Bitbucket offers free hosting of public and private Git repositories.
Bitbucket allows unlimited public and private repositories. The free plan is limited to 5 users per workspace, i.e., if you have more than 5 developers which need access to your repositories you have to pay money to Bitbucket.
3.2. Creating a repository
You need to create a user via the web interface of Bitbucket. After creating this user you can create new repositories via the web interface.
After creating a new repository on Bitbucket, you can use the following instructions to connect a local Git repository with the Bitbucket repository.
For access via HTTPS, Bitbucket Cloud uses API tokens (they replaced the former app passwords) instead of your account password. Alternatively, you can use SSH as in the following example.
These instructions will be similar to the following commands.
# Global setup:
# Set up git
git config --global user.name "Your Name"
git config --global user.email your.email@gmail.com
# Next steps for a new repository
mkdir gitbook
cd gitbook
git init
touch README.md
git add README.md
git commit -m 'first commit'
git branch -M main
git remote add origin ssh://git@bitbucket.org/vogella/gitbook.git
git push -u origin main
# alternatively for an existing Git repo
# add remote and push
cd existing_git_repo
git remote add origin ssh://git@bitbucket.org/vogella/gitbook.git
git branch -M main
git push -u origin main
4. Own Git server
4.1. Hosting your own Git server
As described before, you do not need a server. You can just use a file system or a public Git provider, such as GitHub or Bitbucket. Sometimes, however, it is convenient to have your own server, and installing it under Ubuntu is relatively easy. For more than a few users, a self-hosted Forgejo, Gitea or GitLab instance is the usual choice, as it adds a web interface, pull requests and user management.
First make sure you have installed the SSH tooling. The following command also installs Git, which the server needs anyway.
sudo apt install openssh-server
If you have not yet installed Git on your server, the following command makes sure it is available.
sudo apt install git
Create a new user for the Git system. The user does not need a password, as access is granted via SSH keys.
sudo adduser --disabled-password git
Now log in with your Git user and create a bare repository.
# Login to server
# to test use localhost
ssh git@IP_ADDRESS_OF_SERVER
# Create repository
git init --bare example.git
Now you can push to the remote repository.
mkdir gitexample
cd gitexample
git init
touch README.md
git add README.md
git commit -m 'first commit'
git remote add origin git@IP_ADDRESS_OF_SERVER:example.git
git branch -M main
git push -u origin main
4.2. Give write access to a Git repository
The typical setup based on the created git user from above is that the public SSH key of each user is added to the ~/.ssh/authorized_keys file of the git user.
Afterwards, everyone can access the system using the git user.
Alternatively, you could use LDAP authentication or other special configurations.
4.3. Security setup for the git user
The Git installation provides a specialized shell that can be assigned to the user.
Typically this shell is located in /usr/bin/git-shell and can be assigned to the Git user via the /etc/passwd configuration file.
If you assign this shell to the Git user, this user can only run Git transport commands (for example receiving and sending pushes and fetches) and no longer gets a normal login shell, which adds safety to your Git setup.
Create your bare repositories before you assign this shell, because you cannot log in with it afterwards.
5. Links and literature
5.1. vogella Java example code
If you need more assistance we offer Online Training and Onsite training as well as consulting