Sling Academy
Home/DevOps/How to execute Git commands through SSH

How to execute Git commands through SSH

Last updated: January 28, 2024

Introduction

Secure Shell (SSH) is a cryptographic network protocol used for a secure connection between a client and a server. When dealing with Git repositories, especially on remote servers, utilizing SSH can streamline the process of executing Git commands. In this tutorial, we will explore how to set up an SSH connection and execute various Git commands through it.

Setting Up SSH for Git

To begin with, you need to generate SSH keys on your local machine if you haven’t already done so. This can be achieved with the following command:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

After generating the keys, you will need to add the public key to your Git hosting service. For instance, on GitHub, you would navigate to Settings > SSH and GPG keys, then add your new key.

Basic Git Commands Over SSH

When you have your SSH key setup ready, you can clone a repository using SSH:

git clone ssh://user@host:/path/to/repository.git

Here’s how you can pull changes from the remote server:

git pull origin master

To push changes to the remote server:

git push origin master

SSH Configurations and Shortcuts

You can create an SSH configuration file to manage multiple connection profiles, which is very handy when you need to handle several repositories or servers. You can set up this on your client machine usually under ~/.ssh/config with entries like:

Host myserver
    HostName server.example.com
    User git
    IdentityFile ~/.ssh/id_rsa_myserver
    Port 22

After setting this up, running Git commands becomes easier:

git clone myserver:/path/to/repository.git

Advanced Git Operations Over SSH

SSH also offers the capability to remotely execute commands on your server. A common use case is to check the status of a git repository on a remote machine:

ssh user@server 'cd /path/to/repo; git status'

Another advanced operation would be executing a remote hook or a script post-receive to automate tasks:

ssh user@server 'cd /path/to/repo; git hook post-receive'

Automating SSH Git Commands

To improve the usability of executing Git commands, you can write shell scripts that run SSH git operations. For example, creating a file named git-status.sh with:

#!/bin/bash
ssh user@server 'cd /path/to/repo; git status'

Make the script executable with chmod +x git-status.sh and run it with ./git-status.sh. This helps in automating repetitive SSH and Git command invocations.

Conclusion

Through the proper setup and configuration of SSH, executing Git commands on remote servers becomes efficient and secure. By using configuration files and scripting, you can further streamline your development workflow and take advantage of SSH’s full potential.

Next Article: Git fatal error: The remote end hung up unexpectedly

Previous Article: Git cherry-picking: A detailed tutorial (with examples)

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide