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.
A Git project commonly consists of three main sections in a Developer environment and a single section in a Git Server:
.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
--bareMake a bare Git repository. That is, instead of creating directory and placing the administrative files in directory/.git make the directory itself the
$ 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
To also specify a different port from ssh default (22):
git remote add origin ssh://[email protected]:2222/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)
An alternative workflow that does not involve cloning the repo locally would be:
- Log in to the remote server
ssh [email protected]
Create an empty bare repo:
mkdir /srv/git/my_project.git cd /srv/git/my_project.git` git --bare init CTRL-d
Back in local host, add the remote:
git remote add origin [email protected]:/srv/git/my_project.git
git push origin master
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
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.
- Code Review