Hosting a Git repository on Nextcloud

I would like to host one or more Git repositories on my Nextcloud instance.

I searched the Internet and started experimenting with Git HTTP protocol to push the repository. Unfortunately this didn’t work. I got an error (“error: no DAV locking support on …”).

Here is a support thread: Webdav - lock on file doesn't work - #2 by amcguinness
and a Github issue: WebDav file locking to prevent overwrites · Issue #1308 · nextcloud/server · GitHub discussing WebDAV file locking, with no statement if WebDAV locking will be supported or not.

Is there any other possibility, to host a Git repository on Nextcloud? (e.g. via SSH protocol)

1 Like

Well, not directly. But you could simply put the Git directory into a synced drive using the Nextcloud desktop client so it is the same on each machine (would recommend this only for single user operation). If you want a real Git server, then better use Gitlab/Gitea and so on, Nextcloud is not the right tool in that case.

We have a nextcloud running and it seems exactly the right tool to host private Git repositories. Why use something like bitbucket is my idea too. Is it not considered at all?

1 Like

Nextloud is definitly not designed as a SCM tool (source code management).
What is your use case?

I was thinking about Supporting protocol-v2 next to webdav so we can share private repositories from NextCloud with external developers. We do this with ssh now and have no easy way to see who has rights to some repository from 2 years back. Mistakes are easily made in our current setup and I want to improve this. NextCloud provides us with the right sharing tools we also like to use for Git repositories.

You do have tolls to aid you in the git setup so that you know who has access to what with both private and public repositories. Like gitlab for example. Using nextcloud for it would not be optimal (unless someone decides to write an app for it :stuck_out_tongue:

3 Likes

This is old but it would be extremely cool if there would be a (very simple) git server in NextCloud, just for my personal code, no fancy features needed.

1 Like

Perhaps you can install git and then use “local” external storage in nextcloud.

https://docs.nextcloud.com/server/latest/admin_manual/configuration_files/external_storage_configuration_gui.html

1 Like

-> https://gitea.io/en-us/

1 Like

I don’t think using Nextcloud for Source Code Management is a good idea.

Webdav and git both use https, but that does not mean they are compatible. You could upload a git repository on the nextcloud server, but git wouldn’t be aware of that.
Nextcloud by itself doesn’t support ssh, so no chance that will work.
A git server does much more that a webdav server.

Your best option is probably to use github.com oder gitlab.com, or if you really want to host it yourself you could use Gitea or (if you have more that 8 GB of free memory) Gitlab.
All these options have many features a git over a Nextcloud app would never get like continuous integration.
The access control options wouldn’t be enough either, because you couldn’t protect branches …

Developing a nextcloud app for this purpose would not be a good idea, because there are already excellent options like github or gitlab.

I know all this, and I agree. But, a very simple way to back up my own code using Git to my own server would be so cool. I could also just sync a git repo using the sync client of course.

Anyway, I would not set up gitea or anything. But I’d use a simple nextcloud plugin a lot. I now use Gitlab but it is overkill for my simple home automation scripts.

I have git repos on the same server as Nextcloud and use ssh to access the git repos separate from Nextcloud. Works great and I see no benefit of further integration.

2 Likes

Do they synchronize correctly?

I might not have been clear, I meant “on the same 'nix server”, not in the same file store of a Nextcloud server. Nextcloud (or any general file synchronization agent) should NEVER be used to synchronize git repo’s because it does not synchronize all client repo’s simultaneously.

For example, if I commit to a local repo in my ~/Nextcloud directory, another developer could be committing to their local repo at about the same time, and there will be a race between our Nextcloud clients to see whose commit gets synchronized to the Nextcloud server first.

The best outcome is that Nextcloud will notify the loser there has been a sync collision (a file was modified locally at the same time it was modified on the server), and that the collision will need to be corrected manually. The worst outcome is that the loser will lose their commit entirely (their local repo will be overwritten by the copy on the server from the winning user), or the server repo will be corrupted with some files from the first user and some from the second user. Uggg! This is NOT what you want to have happen.

Only a git push command should be used to update an upstream server. The upstream repo will be locked and the push will be done in an atomic action. This is the only way to preserve repo integrity!

Here is my setup, note that it requires having ssh login capability to the server.

  1. create a login user, the git repo will be stored in their user directory. You can put more than one git repo in the user directory, but all the repos will have common access (i.e. control is to the user directory, not to the repos in the directory).

  2. create a public/private ssh key to access the git repo with (more correctly, to access the user account that has the git repos), and add the public key to the /home/username/.ssh/authorized_keys file.

You can use a single ssh key and give the private key to each real user who needs access, or better, each real user has their own ssh key and you add all their public keys to the authorized_keys file (this way you can easily remove access from a single user by removing their public key from the authorized_keys file).

  1. create a bare repo in the user directory. E.g. sudo -u username git init --bare repo_name.git

Now you have two choices, depending on whether you have started writing code yet.

4a. If you have not started development yet, commit a README.md file from the server so the repo has at least one commit. Then from your dev workstation, clone the repo using the ssh protocol. After that you write code, commit locally, and push your commits to the upstream origin repo as you would with any other cloned project.

4b. If you have already started writing code (i.e. if you already have a repo with commits), add an origin or upstream ssh protocol remote to your local repo and push it to the empty repo on the server.

This approach is easy to setup and has worked well for me. If I need something more complex, I would probably use GitHub - unless I really wanted full control and then I would host Gitlab on the server.

I thought I had written a blog post I was going to refer to you, but it seems not. There is a lot of information on the net (google something like “use ssh to host a remote git repo”), but feel free to DM me if you are having trouble.

Cheers,
Dale

1 Like

Thanks, very much. Due to what you state, is a decent summary of that that git is merely superior? I realize that git is potentially more slow to operate, but git appears to be significantly more reliably able to synchronize than nextcloud-client.

The problem doesn’t have to do with speed or reliability per se, the problem is that Nextcloud doesn’t know about git repositories and deals with the files inside a git repository individually. To Nextcloud, a git repo is just a directory of files, and because it doeesn’t know about git and git commits, it can’t synchronize the group of files that make up a git commit automically (meaning in one step, without being interrupted and before doing anything else).

If you are the only person who will be accessing a git repo in your Nextcloud directory, and you don’t switch computers faster than the Nextcloud agents synchronize, then go ahead and use Nextcloud, it will work just fine and will be simpler than setting up the ssh user access control I described.

I expect it would be possible to write a Nextcloud app that does understand git repositories, and might even use git commands hidden under a nice Nextcloud UI. However, I don’t think the amount of work would be practical or justifiable to even start to approach what GitHub offers now. However, I’ve long wanted more formal document control in Nextcloud for ad hoc documents (more of a a traditional document control submit and approval workflow), and you’ve given me some ideas…

Cheers,
Dale

1 Like