Select Page

Master Git and SSH: A guide to becoming a code management pro

For example, how to maintain your WordPress plugin code in a local Git repository and keep it in sync with several remote repositories (e.g. with a GitHub repo and an SSH repo to be able to work in a team)

Discover the world of Git and SSH! Learn how to create your own Git repository, synchronize with an SSH server and work effectively in a team.

This article was last updated on August, 18 2024.

info
Written by Saskia Teichmann
on November 21, 2023
Sending
User Review
5 (3 votes)
Comments Rating 0 (0 reviews)
Git ∙ Github ∙ SSH ∙ WordPress development
Discover the world of Git and SSH! Learn how to create your own Git repository, synchronize with an SSH server and work effectively in a team.

Welcome to this tutorial, in which we will look at versioning and teamwork in software development. You will learn how to set up a private Git repository and synchronize it with a target SSH directory. This setup is ideal for developing WordPress plugins, themes or other code components where a detailed edit history and team collaboration are crucial.

Master Git and SSH: A guide to becoming a code management pro - WooCommerce & WordPress Developer Hannover
- Master Git and SSH: A guide to becoming a code management pro

Discover the world of Git and SSH! Learn how to create your own Git repository, synchronize with an SSH server and work effectively in a team.

Course Provider: Person

Course Provider Name: Saskia Teichmann

Course Provider URL: https://www.saskialund.de/

Course Mode: Online

Course Workload: PT30M

Course Type: Free

Preparations for this guide

Before you start, make sure that your local setup meets the following requirements:

  1. Git installedCheck whether Git is installed on your system and on the desired remote server. If not, you can download it from git-scm.com download and install.
  2. SSH accessMake sure that you have SSH access to the server you want to work with.
  3. Text editorA basic text editor such as Nano, Vim or a similar program should be installed.
  4. Internet connectionA stable Internet connection is required to interact with remote repositories.

Setting up a private, local Git repository

First, set up your own private Git repository. This repository serves as a central point of contact where all versions of your code are stored.

git init MyProject # Initializes a new Git repository named 'MyProject'
cd MyProject # Changes to the directory just created
git add .             # Adds all current files to the repository
git commit -m "First commit" # Creates a 'commit' with the message 'First commit'

Synchronizing the Git repository with an SSH target directory

Here you synchronize your local Git repository with a remote directory via SSH. This allows you to securely store your work on a remote server and share it with team members.

ssh root@ihr-server.com "mkdir /path/to/target-directory" # Creates a directory on the server
ssh root@ihr-server.com "cd /path/to/target-directory; git init --bare" # Initializes a 'bare' Git repository on the server
git remote add origin ssh://root@ihr-server.com/pfad/zum/zielverzeichnis # Links your local repository with the remote repository. The remote repository is now managed under the identifier "origin". You can also use a different identifier.
git push origin master # Sends your local changes to the remote repository

Working with GitHub

If you prefer to work with GitHub or would like to connect an additional remote repository, you can also connect your local repository to a GitHub repository:

ssh root@ihr-server.com "mkdir /path/to/target-directory" # Creates a directory on the server
ssh root@ihr-server.com "cd /path/to/target-directory; git init --bare" # Initializes a 'bare' Git repository on the server
git remote add origin ssh://root@ihr-server.com/pfad/zum/zielverzeichnis # Links your local repository with the remote repository
git push origin master # Sends your local changes to the remote repository

Automatic pushing of releases (publications) to the SSH target

Automate the deployment of your changes to the server with a post-receive hook:

ssh root@ihr-server.com # Connects to the server
cd /path/to/target-directory # Changes to the target directory
nano hooks/post-receive # Creates or edits the 'post-receive' hook script

Insert the following script to automatically apply changes to the working directory on the server:

#!/bin/sh
GIT_WORK_TREE=/path/to/working-directory git checkout -f # Updates the working directory on the server with the latest changes
chown -R username:groupname /path/to/workingdirectory # Changes the owner of the files in the working directory
chmod +x hooks/post-receive # Makes the script executable
Optional:
chown -R 33:33 /path/to/working-directory # Ensures that the files and directories in the working directory are assigned to a specific server user and server user group. I use 33:33 here because these are the IDs of my web server user and the web server user group www-data.

Drag changes from the SSH target to the local repository

Transfer changes made on the SSH target server to your local repository:

ssh root@ihr-server.com # Connects to the server
cd /path/to/working-directory # Changes to the working directory on the server
git add .  # Adds all new or changed files to the commit
git commit -m "Description of changes" # Creates a new commit with your change description
git push origin master # Sends the changes to the remote repository
git pull origin master # Pulls the latest changes from the remote repository to your local repository

Apply changes from the GitHub remote repository to the local repository

Finally, you will learn how to efficiently integrate changes made in the GitHub remote repository into the local repository. This is particularly useful if you work in a team where several people are working on different parts of a project at the same time, such as when developing complex software or collaboratively creating a WordPress theme or plugin. By regularly updating your local repository, you ensure that you are always up to date with the latest collaborative work.

git fetch origin master # Fetches the latest information from the GitHub repository
git merge origin/master # Merges the changes from the GitHub repository into your local repository

Conclusion: Growing together through sharing and learning

We have now reached the end of this guide, and I hope you find these insights into using Git and SSH useful. Remember that mastering these tools will not only improve your individual software development skills, but also pave the way for more efficient and harmonious team projects.

I invite you to share your thoughts, experiences or questions in the comments. Have you had any experience with Git and SSH? Are there any particular challenges you've encountered or tips you'd like to share with the community? Your feedback is not only valuable to me, but also to other readers.

Your contribution could be the key that helps someone else solve a problem or discover a new perspective. I look forward to hearing from you!

Until next time, stay curious and get involved in the world of coding! 🌟👨‍💻👩‍💻

<span class="castledown-font">Saskia Teichmann</span>

Saskia Teichmann

A WordPress full stack web developer from Germany who likes to create beautiful websites and sophisticated web projects.

In her free time, Saskia enjoys hiking with her family in the Tramuntana mountains on Mallorca, walking in the Herrenhausen Gardens in Hanover or swimming in the sea.

Submit a project requestServing coffee

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Sending