Skip to content

Git

Push changes to new SGC Gitlab repo

Create a new branch: (use git branch to check where you are currently)

git checkout -b your-feature-branch
Commit your changes to the new branch. When you're ready to go live, push the branch to Gitlab:
git push origin your-feature-branch
Then go to Gitlab.com and make a Merge Request so Curtis can merge it.

Moving existing commits to a new branch

Create a new branch from the current commit

This will “move” your current work onto a new branch:

git branch my-new-feature
Now you’re on your new branch with your commit.

Reset the protected branch

If you accidentally committed on main, you may want to return main to match the remote state so your local repo isn’t in a weird state.

First, switch back:

git checkout main
Then reset it to match the remote:
git fetch origin
git reset --hard origin/main
This removes your local commit from main (don’t worry — it’s safe on my-new-feature now).

Delete old branches

After the merge request is merged, delete old branches so they're not lying around:

git fetch --prune # remove deleted remote branches
git switch main && git pull
git branch --merged

Delete individual branches with

git branch -d branch_name

Merge Changes When 2 Devs have commits

Rather than git pull which will throw a fatal error:

Click for full error text
Hint: You have divergent branches and need to specify how to reconcile them.
Hint: You can do so by running one of the following commands sometime before
Hint: your next pull:
Hint: 
Hint:   git config pull.rebase false  # merge
Hint:   git config pull.rebase true   # rebase
Hint:   git config pull.ff only       # fast-forward only
Hint: 
Hint: You can replace "git config" with "git config --global" to set a default
Hint: preference for all repositories. You can also pass --rebase, --no-rebase,
Hint: or --ff-only on the command line to override the configured default per
Hint: invocation.
Git failed with a fatal error.
Git failed with a fatal error.
Need to specify how to reconcile divergent branches.

Just do this to automatically fetch the remote changes, then rebase your own commits on top of them to keep the history clean. (It won't add an extra merge commit this way.)

git pull --rebase

OLD: If you do the following, it will create an extra "merge" commit, which is ugly.

git fetch # fetch the commits from remote
git merge # merge with your local commits
Now you can push your changes as usual.

Hosting the SGC git repo on Hostinger

Follow this tutorial to set up a remote git repo on Hostinger. Here's another tutorial that gives addition details, like setting up SSH keys.

How to Add an Existing SSH Key to Hostinger

1. Copy Your Public Key

  • Open your terminal and run the following command to display your public key:

    cat ~/.ssh/id_rsa.pub
    
    (Replace id_rsa.pub with the actual name of your public key file if it's different.)

  • Copy the output (the entire line starting with ssh-rsa).


2. Log In to Hostinger

  • Go to Hostinger and log in to your account.

3. Go to SSH Access Settings

  • Navigate to Hosting and select the hosting account you want to manage.
  • Find SSH Access under the Advanced section in the control panel.

4. Add the Public Key

  • Click on Add New SSH Key (or a similar option).
  • Paste the copied public key into the provided text box.
  • Give the key a recognizable name (e.g., "My Laptop SSH Key").
  • Click Save or Add Key.

5. Test Your Connection

  • Open your terminal and use the following command to test the connection:
    ssh username@server_ip
    
  • Replace username with your Hostinger SSH username (found in the control panel).
  • Replace server_ip with your Hostinger server's IP address.

6. Verify the Connection

  • If everything is set up correctly, you should connect without needing to enter your password.

You’re all set to use your SSH key for secure and password-free access to your Hostinger server!

Server Setup

Create a new bare repository on the Hostinger server (/home/u159938358/domains/shepherdsglobal.org/git/sgc_wordpress.git).

git init --bare sgc_wordpress.git && cd $_

If needed, run git branch -m main (rename the default branch to "main")

Set up the post-receive hook to checkout files to the server so they're accessible to WP. If you get the error "fatal: cannot run hooks/post-receive: No such file or directory" then it's probably a line-ending issue, you need to switch to unix endings.

hooks/post-receive
#! /usr/bin/bash
GIT_WORK_TREE=/home/u159938358/domains/shepherdsglobal.org/public_html git checkout -f

while read oldrev newrev refname
do
    /usr/bin/php /home/u159938358/domains/shepherdsglobal.org/git/email.php "$oldrev" "$newrev" "$refname" "sgc_wordpress.git"
done

Then make the hook executable: chmod +x hooks/post-receive

Local setup (dev machine)

git remote add live ssh://u159938358@217.196.55.99:65002/home/u159938358/domains/shepherdsglobal.org/git/sgc_wordpress.git

[Make a COMMIT]

git push live master

Info

If you get fatal: cannot run hooks/post-receive: No such file or directory make sure you specified the correct /bin/bash and that your line endings are Unix, not Windows! See this stackoverflow answer for more info.

Warning

When you push a commit to live it immediately gets deployed to the production site! (via the post-receive hook). Make sure you've tested your code before pushing.

Merge git repos while preserving history

The custom GeneratePress theme and the sgc-functions plugin were originally in 2 separate repos. They were merged into a monorepo and other files were added as well to simplify things. For future reference, these are the tutorials followed for merging repos: