Creating a git server from a git repo

Published:
Last modified:
Tags Git , Server , Bare-Repository

Overview

A Git project commonly consists of three main sections in a Developer environment and a single section in a Git Server:

graph TB subgraph GitServer server[Git]==>bare[the Git directory] end subgraph DeveloperRepo git[Git] git==>dir[the Git directory
.git] git==>stage[Staging Area] git==>work[Working Directory] end

In order to “move” a git repo to a git server, a special directory has to be generated first. This is called a bare repository, a directory that only contains the git data but not the project source code files, just like the .git directory.

Then this bare repo should be moved to the server, so the server acts as a common git server.

The repo from which the server was generated needs to add the git server as a remote and every other developer that wants to work in that project can clone it from the server directly.

Process

Current
graph TB subgraph Server srv[git server] end subgraph New Client repo2[ ] end subgraph Client repo[git repo
MYPROJECT] end
Goal
graph TB subgraph Server srv[MYPROJECT bare repo] end subgraph New Client repo[cloned MYPROJECT repo]==>srv end subgraph Client client[MYPROJECT repo]==>srv end

First Alternative

An alternative workflow that does not involve cloning the repo locally would be:

  1. Log in to the remote server ssh user@gitserver.com

  2. Create an empty bare repo:

     mkdir /srv/git/my_project.git
     cd /srv/git/my_project.git
     git --bare init
     CTRL-d
    
  3. Back in local host, add the remote:

     git remote add origin user@gitserver.com:/srv/git/my_project.git
    
  4. Push changes:

     git push origin master
    

Second Alternative

Create bare repository

In order to set up a Git server, you have to export an existing repository into a new bare repository (a repository that doesn’t contain a working directory). This can be done with git clone --bare parameter.

--bare Make a bare Git repository. That is, instead of creating _directory_ and placing the administrative files in _directory_/.git make the _directory_ itself the $GIT_DIR.


$ git clone --bare my_project my_project.git
Cloning into bare repository my_project.git...
done.

Copy repo to remote server

Put the bare repository in the server.


$ scp -r my_project.git user@gitserver.com:/srv/git
$ rm -fr my_project.git

Add server repo as a remote in local repo

After having the repo in the remote server, the git server remote should be added to the local repo so it is possible to pull and push changes to it.


$ cd my_project
# Set a new remote
(my_project)$ git remote add origin user@gitserver.com:/srv/git/my_project.git

Verify new remote


(my_project)$ git remote -v
origin  user@gitserver.com:/srv/git/my_project.git (fetch)
origin  user@gitserver.com:/srv/git/my_project.git (push)

Conclusions

This is a simple way of enabling other users to contribute to your projects and having a centralized repository where your main stuff goes.

Now every other user that wants to contribute should clone the server repo and keep that up to date.

A bash script based in this tutorial is available at https://github.com/marcanuy/centralize-git-repo.

sequenceDiagram participant Alice participant Server participant Bob Alice->>Alice: Create bare repository Alice->>Server: Copy repo to remote server Alice->>Alice: Add remote Bob-->>Server: Clone repo from remote server

References

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


Having a git server and a repo in another computer not present in the server, create a centralized version of the repo in the server.

Clutter-free software concepts.
Translations English Español

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·