1

I have 2 private repository (not branches) A and B of which I have full access to.

Whenever A makes a commit, I want B to cherry-pick or merge the new commit into it (assuming no merge conflicts), so that B will always be up-to-date with the latest changes from A.

How do I accomplish this with Git hook or something else?

A shell script is less flexible as it means you have to commit/push with command line rather than GUI (eg. with SourceTree).

Avery235
  • 4,756
  • 12
  • 49
  • 83
  • Do you want a mirror? – LinFelix Jan 16 '18 at 14:45
  • @F.V. repository `B` has some commits that `A` doesn't have. But I want all commits from `A` to automatically apply/merge to `B` if there's no conflict. – Avery235 Jan 16 '18 at 15:56
  • this is one of those things that one must ask _why_ would someone want to do this? It seems likely this is a solution to a problematic problem. – thekbb Jan 16 '18 at 20:21
  • @thekbb the manager requires me to maintain 2 websites (with different domains) with different themes for different clients (stupid, I know). I use Netlify for automatic deployment so the 2 versions must reside in different Github repositories. – Avery235 Jan 17 '18 at 01:28

1 Answers1

2

In repository A create a post-commit hook that switches to repo B and runs git pull.

The kind of "switching" depends on where B resides. If it's on the same computer the hook can just cd /path/to/B, but don't forget to clear GIT_DIR and GIT_WORK_TREE env vars before running git pull. If B is on another server the hook should ssh there.

phd
  • 82,685
  • 13
  • 120
  • 165
  • A `post-receive-hook` is also a possibility. Since there is development allowed on both repositories, where `B` is _not_ a mirror, merge conflicts are possible. You might want to catch those? – LinFelix Jan 16 '18 at 20:26
  • `post-receive-hook` — depends on how commits are added to `A`. *merge conflicts are possible* — the OP have said *assuming no merge conflicts* and I prefer to stop here. :-) – phd Jan 16 '18 at 21:27
  • Git hook will not take effect if someone else pushes to the repo since it's locally hosted? Is there a way for it to work for commits made by others to the remote repo? – Avery235 Feb 16 '18 at 07:52
  • Is the only solution to host an API server and set up web hook on the remote repo? – Avery235 Feb 16 '18 at 08:06
  • [Server hooks](https://git-scm.com/docs/githooks#_pre_push) run when someone pushes to the repo. – phd Feb 16 '18 at 13:08