How to use Git for Version Control on Your VPS or Dedicated Server

How to use Git for Version Control on Your VPS or Dedicated Server

Git is a Version Control System (Version Control System) created by Linus Torvalds, creator of the Linux kernel, because he needed an open source, distributed VCS for the group of developers working on the Linux kernel.

GitHub, a hosting service for Git projects, has helped spread the concept of version control beyond software development. Writers use Git to collaborate on projects from technical documentation to books.

For example, Fork the Cookbook uses Git to facilitate collaboration, encouraging you to make a copy of the entire cookbook then submit recipes back, illustrating how Git works in a nutshell.

Each collaborator makes a copy of the repository (the project files) on their local computer, works on their respective parts of the project, first committing additions and changes to their local repository (repo for short), pushing those changes to the remote repo.

A way for someone with a VPS hosting account to start learning Git and to practice with Git would be to create a local repo on their on their virtual server, practice committing changes to the local repo, committing those changes to the remote repo (in this case Github).

Glossary

Working Directory – The root directory of the project you’re tracking with Git.

Commit – A snapshot of the files in the working directory.

Index (i.e., staging area) – The git add command adds files to the git index staging these changes before you commit them to the local repo (repo on your VPS in this case).

Branches – Every Git repo has a master branch. You can create a branch to work on something without immediately merging your changes into the master branch.

Section 1 –
Update your server and install git

Update your virtual server

If you’re using Ubuntu 16.04, update your package indexes.

$ sudo apt-get update

If you’re using CentOS 7, update and upgrade your system, then free up the disk space taken by these processes with clean all.

$ sudo yum update
$ sudo yum upgrade
$ sudo yum clean all

Install Git

For Ubuntu 16.04:

$ sudo apt-get install git

For CentOS 7:

$ sudo yum install git

Section 2 –
Configure Git

Set your username and email address for git

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

Check your configuration

$ git config --list

The git config –list command should return the configuration you added in Step 1.

user.name=YourUserName
user.email=example@example.com

Section 3 –
Using Git

In this section, you’ll learn the basics of using Git for version control with a project.

Create the project directory

$ mkdir project1

Create some files for the project

First, change directories to the project1 directory.

$ cd project1

Then, create a few files and a directory.

$ touch file1 file2

Initialize git in the project1 directory

$ init git

This should return the following:

Initialized empty Git repository in /home/user/project1/.git/

The git repository is empty because you haven’t yet added any files for git to track for changes.

Add files to the repository

To add all the files in the current working directory to this git repository:

$ git add .

Make your initial commit with a commit message

All commits have to have a commit message. Typically, the commit message would describe what additions or changes you’ve made to the project.

$ git commit -m "Initial Commit" -a

This command will return something like this:

[master (root-commit) c018c96] Initial Commit
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1
 create mode 100644 file2

Section 4 –
Working with a remote repository

You’ll use Github as the remote repository. If you haven’t established an account with Github, do that before continuing. If you’re doing any sort of web development, you’ll want a Github account.

Generate an SSH key for Github

$ ssh-keygen -t rsa -b 4096 -C

Copy the public key to Github

Change directory to ~/.ssh

$ cd ~/.ssh

Open the public key with cat.

$ cat id_rsa.pub

Next, copy the public key to the clipboard.

Create a key on Github

  1. Click on your profile picture in the upper right
  2. Select settings
  3. Click on SSH and GPG keys
  4. Click on the “Create SSH Key” button
  5. Give the key a “title” to help you identify it
  6. Paste the public key into the “key” field
  7. Save the key

Create a passphrase

The first time you try to use the ssh key you’ve just created, ssh will prompt you to create a passphrase. A passphrase is a sentence that substitutes for a password. Passphrases are typically more secure than a password.

You have already committed changes to your local repository. Now, commit them to the remote repository.

$ git remote add origin git@github.com:username/project1.git
$ git push -u origin master

Section 5 –
Basic Git commands

In this section, you’ll learn the basic Git commands.
git init – Use this command to start a new git repo. Always run git init within the root directory of the project you want to start tracking with Git.
git clone – Use clone to make a copy of a local or remote repo. For example, to clone a local repo called “project” one:

$ git clone projects/project1

To clone a remote repo called project2 on GitHub.

$ git clone git@github.com:github_username:github_username/project2.git

git commit – Use commit to commit changes to your local repo.

git add – Add files to the local index (staging area).

To add all the files and directories in the current working directory.
git add .

To add a single file to the index.

$ git add <filename>

git commit – Commit changes to the local repo.

To add a message (recommended best practice).

$ git commit -m "Message describing changes"

To commit changes you’ve committed using the git add command and any files you’ve changed since you ran git add.

$ git commit -a

git push origin master – Push changes you’ve made to the remote repository (e.g., to a Github repo).

git status – The status of files you’ve changed, including what files you’ve changed but haven’t yet committed.

git remote add origin <remote_server> – Use this command to connect your local repo to a remote repo.

git remote -v – List all remote repos to which your local repo is connected.

Conclusion

This Git primer introduced you to the concept of version control and showed you how to get started using Git.

The most effective way to learn Git is to start using it. A good next step would be to install Git on your local computer (if you haven’t yet), establishing local repos for projects. As you use Git more, it’ll become second nature to track all your projects.

 

Check out these top 3 Dedicated server hosting services:

Hostinger
$2.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7
IONOS
$1.00 /mo
Starting price
Visit IONOS
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.3
Ultahost
$2.90 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8

How to Change Your SSH Port from the Default Port

Follow our step-by-step guide to change your SSH default port to a more secure o
3 min read
Idan Cohen
Idan Cohen
Marketing Expert

How To Transfer cPanel Accounts from Shared Hostings to VPS or Dedicated Server

This tutorial will help you implement full cPanel backup before transferring it
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to Connect to a Server by Using SSH on Linux and Mac

Most servers in the world are run on Linux servers. They’re dependable, afford
4 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

Use Python to Automate Routine Tasks on Your VPS or Dedicated Server

Here we'll use basic knowledge of Python to teach you how to leverage the extrao
6 min read
Md. Ehsanul Haque Kanan
Md. Ehsanul Haque Kanan
Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top