Alberto Álvarez

Posts tagged "git":

18 Jan 2022

Adding multiple remotes for a single git repository

It is sometimes useful to make multiple remote copies of a git repository, for instance, pushing your changes to both GitLab and GitHub. This can easily be done by calling the following commands:

git remote add gitlab url-to-your-gitlab-repository
git remote add github url-to-your-github-repository

The url-to-your-gitlab/github-repository part assumes that you already have created an empty repository in GitLab/GitHub. Now we are ready to push the latest changes using:

git push gitlab branch-name
git push github branch-name

where the branch-name variable refers to the actual name of the branch you need to push.

Tags: git
17 Apr 2020

Deploying my site to the server

I have managed to deploy this website using a really nice blog post by Andrew J. Flemming where he speaks about creating HUB and PRIME git repositories to interact with his server in mediatemple. I followed his steps and just had to change the paths pretty much. Here is a recap.

1. Create the PRIME repository

First I created an empty git repository in the public html folder of the domain (This assumes that you have ssh access to your server). Also, I made an initial commit there.

$ cd /path-to-your-domain/html
$ git init
$ git add .
$ git commit -m "First import"

2. Create the HUB repository

Then I placed the HUB outside the public html folder, not in the home directory as Andrew J. Flemming suggested. I created a folder called repo which sits inside the domain folder. Inside repo, I added another folder called your-domain.git, which will host a bare git repository (notice that you can call this folder any way you want, as long as it ends with .git, your-domain.git is just a placeholder in this case).

$ cd /path-to-your-domain/
$ mkdir repo
$ cd repo
$ mkdir your-domain.git
$ cd your-domain.git
$ git --bare init

3. Remote and Hooks

Our PRIME repository needs to have a remote repository in order to get the files that are part of the website, in this case the folder we just created your-domain.git will act as the remote.

$ cd /path-to-your-domain/html
$ git remote add hub /path-to-your-domain/repo/your-domain.git
$ git remote show hub
$ git push hub master

Then we just need to add two hooks. These will tell the repositories that we created what to do once they receive new code. Two hooks are needed: a post-commit, and a post-update. The post-commit, has to be created in the .git/hooks/ folder of your PRIME repository. We can do this by adding the following code the post-commit.sample file and then removing the .sample part to activate it.

#!/bin/sh
git push hub

If you don't have a post-commit.sample, just create the post-commit file directly and add the previous bit of code.

Now we need a post~update hook that will reside in the your-domain.git/hooks/ folder. This will tell our HUB to go to the PRIME repository and then execute a pull from that location for us.

#!/bin/sh
cd /path-to-your-domain/html || exit
unset GIT_DIR
git pull hub master
git-update-server-info

4. Push local changes to the HUB

In my case, I had some code in my local repository already since I wanted to test how the website would look before deploying it to the server. Then I just had to add a remote to the local repository as follows:

$ cd /path-to-your-local-repository
$ git remote add hub ssh://yourdomain.com@yourdomain.com:/path-to-your-domain/repo/your-domain.git
$ git pull hub master

Notice that in the previous code, the line ssh://yourdomain.com@yourdomain.com: assumes that you have a username (part before the @) and the address of the server you want to access (what comes after the @).

Finally, I was able to push the site I built locally to the server using these commands:

$ git add .
$ git commit -m "Initial commit of my website"
$ git push hub master

The same procedure can be used for other domains in your server as long as you define a HUB for each of them.

Tags: git
Other posts
alberto.am by Alberto Álvarez is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.