Apache permission issues while deploy Kirby with GIT

I am running a Kirby site on a droplet following the steps on this tutorial: How To Set Up Automatic Deployment with Git with a VPS
In order to use the panel and editing the content folder on the server I changed the permissions to: sudo chown -R www-data:www-data directory

But know I am unable to push to the remote repo due the fact the permissions has changed.
I have to manually change that stuff before and after I push to the remote.

How are other users handle that issue?
This is by far the common way to deploy sites in production and I am interested how to solve it!

There a scripts around like this one: Script that changes the permissions

But how to to it automatically with a post-receive hook or similar?

Yes, that code goes into the post-receive hook

But there are sudo commands in it. I can not sudo remotly

Then try without sudo

Not working without sudo

According to your post you are using sudo as a post-receive hook. So how is it working?

I used two hooks, the post-checkout hook and the post-merge hook with the said content, I always did a pull on the remote server from the remote repo, I think, then the two hooks on the server did their job.

The setup described in the tutorial is a bit different and accordingly the hook to use, but the general procedure should be similar.