# How to Connect a VPS to GitHub and Move Files

> If you've just gained access to your VPS, here's how to connect to GitHub and start moving files back and forth from a repository.

- Author: [Bhagwad Park](https://www.namehero.com/blog/author/bhagwad-park/)
- Published: 2023-08-16
- Updated: 2024-11-07
- Category: [VPS Hosting](https://www.namehero.com/blog/category/vps-hosting/), [Website Development](https://www.namehero.com/blog/category/website-development/)
- Web page: https://www.namehero.com/blog/how-to-connect-a-vps-to-github-and-move-files-to-it/

[![](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16123428/vpsgithub-1024x576.jpg)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16123428/vpsgithub.jpg)

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](https://www.namehero.com/vps-hosting)

## 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](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/14123823/Github-Repository-Address.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/14123823/Github-Repository-Address.png)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:
  [![Project Folder on the VPS](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/14124224/Project-Folder-on-the-VPS.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/14124224/Project-Folder-on-the-VPS.png)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
```

I’ve written an earlier article on how to [ensure that you can run sudo commands on Linux](https://www.namehero.com/blog/how-to-allow-a-linux-user-to-run-sudo-commands-with-namehero/).

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.

## 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](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/14125907/Git-Clone-Command-on-Ubuntu.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/14125907/Git-Clone-Command-on-Ubuntu.png)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](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/14130025/New-Repository-Created-on-the-VPS.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/14130025/New-Repository-Created-on-the-VPS.png)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:

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](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16112828/Get-the-Path-to-the-Cloned-Repository.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16112828/Get-the-Path-to-the-Cloned-Repository.png)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.

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](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16115239/Add-and-Commit-the-Change-to-the-Git-Repo.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16115239/Add-and-Commit-the-Change-to-the-Git-Repo.png)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”:
  [![](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16120106/Github-Developer-Settings.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16120106/Github-Developer-Settings.png)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](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16120220/Generate-a-Personal-Access-Token.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16120220/Generate-a-Personal-Access-Token.png)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](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16120353/Personal-Access-Token-Generated.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16120353/Personal-Access-Token-Generated.png)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:

```
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!](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16121835/Github-Repo-Successfully-Pushed-to-GitHub.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16121835/Github-Repo-Successfully-Pushed-to-GitHub.png)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](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16121904/GitHub-Repo-Now-Shows-the-Files-from-the-VPS.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2023/08/16121904/GitHub-Repo-Now-Shows-the-Files-from-the-VPS.png)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!

## Related posts

- [Bash File Descriptors Explained](https://www.namehero.com/blog/bash-file-descriptors-explained/)
- [Curl Headers, Cookies, and Authentication](https://www.namehero.com/blog/curl-headers-cookies-and-authentication/)
- [Linux Process Management: ps, htop, kill, and nice](https://www.namehero.com/blog/linux-process-management-ps-htop-kill-and-nice/)
