Skip to content

Instantly share code, notes, and snippets.

@henkealg
Last active April 21, 2021 22:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henkealg/0fe713ff4b70fdfcd10661a63f9d3bf3 to your computer and use it in GitHub Desktop.
Save henkealg/0fe713ff4b70fdfcd10661a63f9d3bf3 to your computer and use it in GitHub Desktop.
Quick step by step guide to remote git bare repo setup

Quick step by step guide to remote git bare repo setup

For controlled delivery/deployment of your project to custom remote git repositories.

Remote server setup

Replace %%repo-name%% with your custom name for the remote bare repo you are creating. I prefer using /var/git as the recipient folder but this can be any folder for witch the user has read/write access.

On the remote/recipient server Create and navigate to the target folder cd /var/git

Create a new bare repo with the desired name

git init %%repo-name%%.git --bare

Navigate to the hooks folder of the new repo folder

cd %%repo-name%%.git/hooks

Copy the post-update.sample into a new file called simply post-update

cp post-update.sample post-update

Open the post-update file for editing

nano post-update

Replace the contents with the following bash script. /var/www/ needs to be replaced with your preferred web-root or public folder. "master" branch can be renamed onto any other branch you want the bash script to use when the bare repo detects new updates.

#!/bin/sh

cd /var/www/%%repo-name%% || exit
unset GIT_DIR
git pull hub master
exec git update-server-info

Save the edits and exit.

Clone the bare repo into the folder referenced in the bash script

git clone /var/git/%%repo-name%%.git /var/www/%%repo-name%%

Navigate to the folder referenced in the bash script

cd /var/www/%%repo-name%%

Rename the default "origin" reference to "hub"

git remote rename origin hub

Local project setup

Add the new repo as a remote in your local git repository.

The user included in the new remote path has to have read/write access over SSH to the bare repo folder as well as the target web-root/public folder on the remote server.

git remote add stage username@remote-server-domain.example:/var/git/%%repo-name%%.git

"stage" can be renamed to any name you want to use to separate this remote from the others (like origin) in the git repo.

Finally, fetch the new remote into your local git repo.

git fetch stage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment