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
- Step #1. Ensure That You Have A Repo On GitHub
- Step #2. Identifying the Files and Folders on your VPS to Sync
- Step #3. Installing Git On The VPS
- Step #4. Configure Git and Clone your Repo
- Step #5. Copying Files to the Git Repo
- Step #6. Commit the Changes to Git
- Step #7. Create a Personal Access Token on GitHub
- Step #8. Sync Changes to the Git Repo
- Conclusion
What You Need Before You Start
Before starting, I’m assuming that you have the following:
- A GitHub account
- 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:
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:
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.
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 "[email protected]"
Replace “Your Name” and “[email protected]” 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 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:
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:
- The path to the directory containing your existing project
- 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:
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:
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”:
In the following screen, click “Personal access tokens”, click “Tokens (classic)” and click “Generate a personal access token” like this:
On the following screen, select these three permissions:
- repo
- read:org
- 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!:
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:
Finally, you can verify that your changes have been uploaded to GitHub like this:
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!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Antyage says
You are very great man.
I am a blockchain and full stack developer and using github on vps.
I learned a lot of about git.
I hope you to be well and fun everyday.
Thank you for your service.