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.

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

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.

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 [email protected]:/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 [email protected]:/srv/git/my_project.git

Verify new remote


(my_project)$ git remote -v
origin  [email protected]:/srv/git/my_project.git (fetch)
origin  [email protected]:/srv/git/my_project.git (push)

Alternative

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

  1. Log in to the remote server ssh [email protected]
  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 [email protected]:/srv/git/my_project.git
    
  4. Push changes:

     git push origin master
    

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.

References

Summary

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
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.