Decentralized project storage and deployment with syncthing and git
Recently I've been looking into organizing my digital files and projects
a little. Storing media and documents was easy enough using syncthing,
and git is obviously nice for programming projects.
But what about combining the two?
Essentially what you need to do is create a git
folder and
sync it using syncthing.
This folder is NOT where you will actally be working on your
projects, it is just the git server. You can initialize projects here
like so:
# in the git server folder
$ mkdir myproject
$ cd myproject
$ git init --bare
Now you use this repository like you do with any other git server:
$ git clone /path/to/git/repo/myproject
$ cd myproject
$ ls
It also works the other way around if you already have a project going on:
$ git remote add origin /path/to/git/repo/myproject
$ git push -u origin master
Now you have decentralized project storage, but you might still want to
mirror your project to services like github. This is trivial using git
hooks.
create the following script in
/path/to/git/repo/myproject/hooks/post-update
#!/bin/sh
exec git push --mirror https://githost.com/username/project.git
Don't forget to make it exectuable with
chmod +x hooks/post-update
if it isn't already.
And that's all! When changes are pushed the
post-update
script will run and mirror the repository to
the third party. You can add your own code to the script to also for
example deploy your application.
Update 20/07/2021:
It should be noted that mirroring like this will override all
changes made to the remote! So you should treat github as a static
mirror.
If you did accept changes in the remote repository and want to
copy them into your local project you can
git pull <mirrorurl>
into your working repo and then
git push
into the remote again.