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

 

Step 1 - 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

Step 2 - Install Git

For Ubuntu 16.04:

$ sudo apt-get install git

For CentOS 7:

$ sudo yum install git

Section 2 - Configure Git

 

Step 1 - Set your username and email address for git

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

Step 2 - 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.

Step 1 - Create the project directory

$ mkdir project1

Step 2 - 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

Step 3 - 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.

Step 4 - Add files to the repository

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

$ git add .

Step 5 - 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.

Step 1 - Generate an SSH key for Github

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

Step 2 - 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.

Step 3 - 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

Step 4 - 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 4 - 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:

Was this article helpful?