Distributed version control system git is a set of programs that allow several people to work with the same files at the same time. In doing so, you can track the changes made by each of them, make these changes to remote repositories, and roll them back by reverting files to previous versions.
Choosing a platform and creating a project
There are many different platforms that are based on the git version control system. This article will use the gitlab.com platform.
We will assume that you have already registered in the system. Log in with your username and click New project to create a new project. The following window will open:
Select Create blank project and proceed to the next step. Here we need to set the project identifier Project slug. Check that the visibility of the project is set to Private and create a Create Project:
Adding ssh key to work with gitlab
To simplify remote work with the gitlab platform, add an ssh key. Let’s generate a new rsa key:
$ ssh-keygen -t rsa -b 2048
We agree with the standard path and an empty password for the key:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ivan/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Public private keys will be generated:
Your identification has been saved in /home/ivan/.ssh/id_rsa.
Your public key has been saved in /home/ivan/.ssh/id_rsa.pub.
If you have many keys, then you can specify which one to use with one or another host. To do this, you need to make changes to the ~ /.ssh /config file:
Next, copy the data from the file with the public key ~/.ssh /id_rsa.pub and return to gitlab.com. Add ssh key to gitlab:
- Click on the user icon
- Choose Profile
- SSH Keys
- Insert the contents of the public key
- Specify an arbitrary name and expiration date of the key, if necessary
- Click Add:
Great, now we can work with repositories from the command line using the ssh key.
Installing version control system git
In modern linux distributions, the distributed version control system git is usually supplied with the system. To check if you have it installed, run:
$ git --version
If git is installed, then its version will be displayed. If no such command is found, then installation is required. For redhat like systems:
$ sudo dnf install git
For debian like systems:
$ sudo apt-get install git
Initial git setup
Before using git, you need to do some basic configuration. To understand the basics, it should be noted that there are three types of config files:
- Global in /etc/gitconfig (using –system option)
- Custom, which is located in the home directory of a specific user ~/.gitconfig or ~/.config/git/config (option –global)
- The repository stored in the repository directory .git/config (option –local)
The settings from the configuration file of each next level replace the settings from the previous levels. Thanks to this, you can customize git for a specific user or repository.
Let’s configure git to work under the current user. The required settings are username and email address:
$ git config --global user.name "Ivan Smith"
$ git config --global user.email firstname.lastname@example.org
To see the current settings and in which configuration files they are present, you must run the command:
$ git config --list --show-origin
We’ll get the following output:
file:/home/ivan/.gitconfig user.name=Ivan Smith
Cloning an existing repository
After creating a new project in gitlab, you can see a link to it that looks like this: email@example.com:pocketadmin/my-test, where:
- git – protocol
- gitlab.com – platform site
- pocketadmin – user name (login)
- my-test – project name
Let’s copy the previously created repository:
$ git clone firstname.lastname@example.org:pocketadmin/my-test
After that, a directory with the name of your project will be created in the current directory. Let’s go to it:
Next, we will look at the basic commands for working with the repository.
Adding file change tracking
Let’s create a file in our directory:
$ touch README.md
If we check:
$ git status
Then we will see in the output that the README.md file is not currently being tracked. To add a file under git version control, you need to add a file to track changes:
$ git add README.md
If you want to add all the files in the directory, then you need to use:
$ git add .
If we now rerun git status, we will see that our file will be included in the next commit.
Making a commit
Before pushing changes to a file from the local repository to the remote one, you need to commit:
$ git commit -m 'initial commit'
where the -m option is a comment to the commit, in our case ‘initial commit’.
Pushing changes to a remote repository
Now all that remains is to push the changes to the remote repository:
$ git push
Retrieving changes from a remote repository
To get changes from a remote server run:
$ git pull
Your local repository will be in sync with the remote one.
Getting help with git commands
If you need help with git, you can always get it like this:
$ git help <command>
$ git <command> --help
For example, returning to the settings, you can get help on working with confg:
$ git config --help