Table of Contents
This guide will assist you in how to deploy code to your application using Git commands.
Remember that you can deploy your Git code (pull) using the Cloudways console (refer to this guide). However, if you want to use other Git functionalities (such as push, pull, clone, etc), you will need to access your server through SSH.
Important
You need Master Credentials to run Git commands because Application Credentials do not have permission to generate and access SSH keys (stored in the default location). If you are a team member, you can use Git deployment via Cloudways Platform (or request master credential access from the account owner).
Using Git via Command Line
Step 1: Get SSH Access
Connect to your server via SSH. (To do this please, follow this guide.)
Step 2: Generate SSH Key (Master Credentials Only)
Once the SSH connection has been established, you need to generate SSH keys for directly connecting your GitHub or Bitbucket repositories without providing a username or password every time.
Navigate to your desired web application’s webroot (public_html) and then type the following command to generate public and private RSA key pair.
ssh-keygen -t rsa -C [email protected]
#replace [email protected] with your email address.
Hit Enter to save the key in the default location (/home/master/.ssh/id_rsa). Hit Enter twice for no/empty passphrase.
Your SSH keys will be saved at the default location. Run the following command to view the generated public SSH key.
cat /home/master/.ssh/id_rsa.pub
Copy the public key text carefully starting from ssh-rsa and ending in your email address. This text will be used in the next step.
Step 3: Upload the SSH Public Key to Your Git Repository
We are using a Github account for this demonstration. If you prefer another Git service, you will have to find the equivalent way of completing this step.
Log into your GitHub account and choose your desired repository (which you want to connect to your web application). Then, navigate to Settings and click on Deploy Keys.
Click on Add Deploy Key button to add the SSH key we copied in step 3. Paste the content into the Key field on Github. You can give a name to this key in the title field (e.g. Demo).
Check Allow write access option to give write access (push) to your repository
Click on Add Key button to save the SSH key.
Step 4: Deploy Code Using Git Commands
After successful addition of the SSH key to the GitHub repository, you can now execute Git commands on your server (under the application’s public_html or private_html) using the shell access with Master Credentials.
Some useful Git commands are mentioned below:
Git Clone: To get a copy of the code (web files) from the GitHub repository, use the following command.
$ git clone git_url
#replace git_url with your repository, for example [email protected]:cloudwaystestdemo.git
Git init: Use the following command to initialize a local Git repository.
$ git init
Git add: Add the files to your new local repository.
$ git add . #Adds the files to the local repository and stages them for commit.
Git commit: Commit the files that you want to stage in the local Git repository.
$ git commit -m “my first git commit.”
#Commits the tracked changes and prepares them to to be pushed to a remote Git repository.
Git remote add: Run the following command to add the Git URL where your local repository will be pushed.
$ git remote add origin remote_repository_SSH_URL
#adds the new remote
Verify Git remote URL:
$ git remote -v
Git push: Push changes in your local repository to your GitHub.
$ git push origin master
#pushes the changes in the local repository to the remote repository you added as the origin
Git checkout: You can create a branch and switch to it by running the following command.
$ git checkout -b branchname
Or, if the branch has already been created, then:
$ git checkout branchname
Git merge: You can merge the branch into the master branch through the following command:
$ git merge master
Important Notes
SSH keys generated from Cloudways UI will work for Git deployments from the console only. Therefore, you will have to generate SSH keys (via Master Credentials) on your server through SSH in order to run Git commands.
You can upload the same key (public) to your other Git repositories that you want to connect to your web applications hosted on the same server, for the purpose of granting permission to run Git commands.
Git remote address added via SSH will work for both pulling (deploying) and pushing (uploading) your code from/to the remote repository.
If you want to automate your Git deployments on Cloudways, please refer to this guide.
That’s it! We hope this article was helpful. If you need any help, then feel free to search your query on Cloudways Support Center or contact us via chat (Need a Hand > Send us a Message). Alternatively, you can also create a support ticket.
Quick Tip
Try our GitHub-Friendly Platform for a Seamless Development Workflow! Start Today.