Getting started with Git on Linux
Git is an open-source distributed version control system used by the software developers for small and large projects. It comes with very useful features and it is very easy to learn. In this tutorial we are going to show you how to install Git on a Linux VPS and explain the basics of Git so you can start using it.
1. What is Git
Git is a distributed version control system which is used to track the code changes in software development. Apart from tracking the code changes, Git allows you to revert to a previous stage. The features like cheap local branching, multiple workflows and convenient staging areas make Git one of the most popular version control systems among software developers.
2. Install Git on Linux
First of all, connect to your VPS via SSH.
If you are using an Ubuntu VPS, go ahead and install Git on your system using the commands below:
sudo apt-get update sudo apt-get install git
If you are using a CentOS VPS, you can use the following command to install Git:
sudo yum install git
To check which version of Git is installed on your system, use:
3. Set up Git on Linux
Now that you have Git installed on your Linux VPS, you might like to proceed with the Git configuration. Basically, you will need to configure your name and email address. That way the commit messages will contain the correct information. To update your name and email, you can use the following commands:
git config --global user.name "Your Name" git config --global user.email "[email protected]"
Of course, you need to replace
Your Name and
[email protected] with your own information.
To check the configuration information you can use:
git config --list
4. Create a Git repository
How to create a directory for your first project.
The command above will create a new directory called
git_repo inside your home directory. Feel free to use a different name.
To initialize an empty Git repository in the directory you’ve just created, use the following command:
git init ~/git_repo
To check the status of the working directory and the staging area you can type:
Since the repository doesn’t contain any files, you will see something like this:
# On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track)
Once you create a file, you need to add the file contents to the index. You can do that by using the following command:
However, this command doesn’t affect the repository. To record the changes to the repository, you should run the command below:
For more Git command line options you can use the following command:
This is the list of most commonly used Git commands:
- add Add file contents to the index - bisect Find by binary search the change that introduced a bug - branch List, create, or delete branches - checkout Checkout a branch or paths to the working tree - clone Clone a repository into a new directory - commit Record changes to the repository - diff Show changes between commits, commit and working tree, etc - fetch Download objects and refs from another repository - grep Print lines matching a pattern - init Create an empty Git repository or reinitialize an existing one - log Show commit logs - merge Join two or more development histories together - mv Move or rename a file, a directory, or a symlink - pull Fetch from and merge with another repository or a local branch - push Update remote refs along with associated objects - rebase Forward-port local commits to the updated upstream head - reset Reset current HEAD to the specified state - rm Remove files from the working tree and from the index - show Show various types of objects - status Show the working tree status - tag Create, list, delete or verify a tag object signed with GPG
For more information and usage examples you can read the official Git documentation which is available at https://git-scm.com/doc.
Of course you don’t have to do any of this if you use one of our Git Hosting services, in which case you can simply ask our expert Linux admins to install Git for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post please share it with your friends on the social networks using the share buttons or simply leave a reply below. Thanks.