Connect with us

Website Tutorials

How to Connect a VPS to GitHub and Move Files

Published

on

How to Connect a VPS to GitHub and Move Files

Let’s say you’ve just received the login credentials for your new VPS and have connected to it for the first time. From here, I’ll show you how to connect to GitHub, download (clone) your existing repo, how to add new files and folders to that repo, commit your changes, and finally upload them to GitHub so that they show up in your project when you access it via a browser.

Let’s get started!

What You Need Before You Start

Before starting, I’m assuming that you have the following:

  1. A GitHub account
  2. An active VPS server

Step #1. Ensure That You Have A Repo On GitHub

If you already have a GitHub repo, skip this section and move to the next one. If not, you can create one easily on GitHub. Once you’ve created your repo, GitHub will give you your repo address as shown here:

Github Repository Address
Github Repository Address

You can always find this address again by visiting your repo page, so you don’t need to worry about losing it.

Step #2. Identifying the Files and Folders on your VPS to Sync

The next step is to create or identify the files and folders on your VPS that belong to your project and want to sync to GitHub. If you don’t have anything yet, and simply want to connect to your GitHub repository, skip to the next step.

On my test VPS system, I’ve created a simple directory structure with a couple of files inside it to get started:

Advertisement
Project Folder on the VPS
Project Folder on the VPS

We’re going to sync this entire folder structure along with the files to GitHub after we connect.

Step #3. Installing Git On The VPS

On a new VPS system, you won’t have git installed, so we have to do so manually. On Ubuntu, enter the following commands:

sudo apt-get update
sudo apt-get install git

If you’re using CentOS, then use the following commands instead:

sudo yum update
sudo yum install git

Some newer CentOS versions use the “dnf” tool instead of yum. If your installations fall into this category, use the following command:

sudo dnf install git

To verify that git is installed, you can check the version number like this:

git --version

Once you’ve installed git, it’s time to configure it.

Advertisement

Step #4. Configure Git and Clone your Repo

To configure git, enter the following two commands:

git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"

Replace “Your Name” and “your-email@example.com” with your actual name and e-mail address. Once you’re done, it’s time to clone your git repo. To do this, use the following command:

git clone https://github.com/bhagwad/test.git

Replace “https://github.com/bhagwad/test.git” with the name of your git repository that you obtained in Step 1. If all goes well, your Linux VPS will connect to your GitHub repo and download it onto your local machine as shown here:

Git Clone Command on Ubuntu
Git Clone Command on Ubuntu

Git will create a new repository in the directory in which you ran the git command. For example, since my git repo is called “test”, there’s a new “test” directory along with the previous files that existed earlier:

New Repository Created on the VPS
New Repository Created on the VPS

And you’re done! You’ve successfully connected to a GitHub repo on your new VPS server.

Step #5. Copying Files to the Git Repo

Let’s say you’ve created a new project or made changes to an existing one, and you want to upload your files and folders to git. Doing so is exactly like copying any other file or folder from one directory to another in Linux.

To copy your files to the git folder, you need two things:

Advertisement
  1. The path to the directory containing your existing project
  2. The path to the git directory

While you might be able to type the path to both of these manually, I find it easier to use the “find” command like this:

find /home/bhagwad/ -type d -name test

This will output the exact path of the git directory called “test” which I know is located in my home directory. The outcome is this:

Get the Path to the Cloned Repository
Get the Path to the Cloned Repository

You can simply copy and paste the output in case you don’t want to type the path manually – and depending on the location of both the git directory and your project folder, it might not be easy.

Once you have both locations, you can use the “cp” command to copy the files from one location to the other like this:

cp -r /home/bhagwad/my_project /home/bhagwad/test

This will copy the “my_project” folder to the git directory. Note that if you don’t want to transfer the entire folder, but merely the files and subdirectories of that folder, then you can use the following instead:

cp -r /home/bhagwad/my_project/* /home/bhagwad/test

Depending on how you’ve set up your project and its structure, you might want to transfer either the entire folder or just the contents.

Step #6. Commit the Changes to Git

The files that you transfer to your git folder haven’t been “committed” to the git repo yet. When you write a Word document, you save your changes frequently, don’t you? Think of committing as the project equivalent. When you “commit” your files, the system keeps a snapshot of your work and notices what’s changed, and when. It creates a point to which you can return at any moment.

Advertisement

Importantly, committing your changes doesn’t share those changes with everyone else using the git repo. The changes are still local to only your machine. Committing your changes is a necessary step to pushing your changes.

To commit all the files currently in the git repo, use the following command:

cd /path/to/your/repo
git add .
git commit -m "Your commit message here"

First, navigate to your git repo, then add everything with the second command. Finally, commit them using the “git commit” command with a message as shown above. Here’s what it looks like:

Add and Commit the Change to the Git Repo
Add and Commit the Change to the Git Repo

Once you’ve committed your changes, it’s time to push to the main repo.

Step #7. Create a Personal Access Token on GitHub

GitHub stopped using username/passwords to push to GitHub in August 2021. Now, you need to create something called a “Personal Access Token” that functions like a password. To obtain one for yourself, log into GitHub and go to your project settings. There, on the bottom-left find the option labeled “Developer Settings”:

GitHub Developer Settings

In the following screen, click “Personal access tokens”, click “Tokens (classic)” and click “Generate a personal access token” like this:

Generate a Personal Access Token
Generate a Personal Access Token

On the following screen, select these three permissions:

  1. repo
  2. read:org
  3. write:public_key

Scroll down and click the button to generate the token. GitHub will show you your token. Note it down safely, because you won’t see it again!:

Personal Access Token Generated
Personal Access Token Generated

This will be the stand-in for our password.

Step #8. Sync Changes to the Git Repo

To sync to the GitHub repo, we need the branch name. Type the following:

Advertisement
git branch

This will give you the branch name to which we want to sync. In my case, it’s called “main”.

Next, push to the repo like this:

git push origin main

This will prompt you for your GitHub username and password. For the password, use the Personal Access Token that you obtained earlier. Git will then sync your changes as shown here:

Github Repo Successfully Pushed to GitHub!
Github Repo Successfully Pushed to GitHub!

Finally, you can verify that your changes have been uploaded to GitHub like this:

GitHub Repo Now Shows the Files from the VPS
GitHub Repo Now Shows the Files from the VPS

And you’re done.

Conclusion

This tutorial has been a step-by-step explanation of uploading your project to GitHub from a newly crafted VPS. I hope you found it useful!

Advertisement

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.