1

I want to track directories and files stored at the server. The initial idea was to set up a bare repository at the server and then push/pull from local machines (where non-bare repos are stored). The problem is that updated files must be present at the server.

Initially I focused on setting up a non-bare repo on the server and pulling from the bare repo but it would require someone to git pull every time a push is made from a local machine. My question is: is it possible to set up bare and non-bare repos in a way that allows me to make changes to the code on a local machine, push those changes and have them at the server automatically. If a non-bare repo at the server side is needed, is it possible to block pushing from it?

balkon16
  • 1,338
  • 4
  • 20
  • 40

1 Answers1

2

Instead of having to use git pull manually on the server, is it possible to use server-side hooks? This could be used to somehow trigger a pull for the non-bare repository on the server.

The post receive hook could be something like

#!/bin/bash

unset GIT_DIR
git -C /path/to/non/bare/repo pull

Another option, if you can afford to have some delay in the update of the server-side non-bare repository, would be to use a cron-job to periodically pull from the bare repository.

Simon Doppler
  • 1,918
  • 8
  • 26
  • I'd go for `eval unset ${!GIT_*}` just to be sure, but yeah, a post-receive deploy is what you want. Create the non-bare clone with `git clone -s bare.git deploy` and the pull will be so close to maximally efficient I don't see any practical reason to use the intended-for-scripting core commands instead. – jthill Feb 21 '19 at 17:35
  • 1
    [`unset $(git rev-parse --local-env-vars)`](https://stackoverflow.com/a/6394777/7976758) – phd Feb 21 '19 at 17:45
  • What does -C stands for in `git -C /path/to/non/bare/repo pull` – balkon16 Feb 22 '19 at 07:48
  • 1
    @balkon16 it does specify the path to which `git` must change before performing the action (it could be replaced with `cd /path/to/non/bare/repo && git pull` – Simon Doppler Feb 22 '19 at 08:13
  • Should I add any hooks in the `.git/hooks` directory of the non-bare repo at the server? I'm asking because I created `post-receive` file in the `hooks` directory of bare repo and put there the following content (semicolon separates lines only in this comment): `!/bin/sh; cd /data/11_prywatne/14_Pawel/repo_pawel_non_bare/deploy || exit; unset GIT_DIR; git pull origin master` but the non-bare repo at the server doesn't automatically pulls from the bare repo. – balkon16 Feb 22 '19 at 08:37
  • I don't see a reason why you should need to put something into the non-bare repository. Just make sure you have made the hook executable. – Simon Doppler Feb 22 '19 at 08:43
  • I just wanted to edit my last comment. I set the `hooks/post-receive` as executable (`chmod +x hooks/post-receive`) but the non-bare repo at the server doesn't get updated automatically. – balkon16 Feb 22 '19 at 08:53
  • When pushing to the bare repository, does the push message contain any lines starting with `remote:`? – Simon Doppler Feb 22 '19 at 09:00
  • @SimonDoppler, in the end the `hooks/post-receive` works as intended. Thank you for your help. However, it turns out there might be a situation that someone edits the code in the non-bare server repo. It is necessary that the next push from the local non-bare repo overwrites those changes. So far there are conflicts that must be resolved manually. I assume there's a way to automatically resolve the conflicts in favour of the local repo version. – balkon16 Feb 22 '19 at 09:11
  • 1
    @balkon16 You can use `git checkout --ours .` to use all local files of the repository. It would require more steps than just a pull but should be doable. I would suggest you look up [this answer for more information](https://stackoverflow.com/questions/2073841/how-can-i-discard-remote-changes-and-mark-a-file-as-resolved) – Simon Doppler Feb 22 '19 at 09:36