Creating a git server from a git repo
Overview
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.
Process
MYPROJECT] end
First Alternative
An alternative workflow that does not involve cloning the repo locally would be:
Log in to the remote server
ssh user@gitserver.com
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 user@gitserver.com:/srv/git/my_project.git
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
git remote add origin ssh://user@gitserver.com:2222/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.
References
- https://git-scm.com/docs/git-clone
- https://git-scm.com/book/en/v2/Git-on-the-Server-Getting-Git-on-a-Server
- https://git-scm.com/book/ch4-1.html
- Code Review
- Creating a git server from a git repo
- Showing Untracked Files In Status After Creating New Directories In GitJanuary 2, 2016
Articles
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
·