Table of Contents

Git

Git is a tool that offers a version control system in order to record changes to any type of file, over time, so that it's possible to recall specific versions later.

Terminology

The following terms are used:

Install Git on one's PC

These are the steps to install Git:

  1. install git:
    sudo apt install git
  2. set user.name and user.email, which will appear in commits for every repository, with the commands:
    git config --global user.name "<user>"
    git config --global user.email "<my@mail.com>"

Configuration settings can be checked with:

git config --list --show-origin

Normally, the username and password are requested at each remote connection. Their input can be avoided or reduced using one of these two methods, inside a Git directory (they will take effect at the next input):

Create a Git directory

Two ways are possible:

  1. initialize a local directory, currently not under version control, into a Git repository;
  2. clone an existing Git repository, in a local directory.

In both cases, an empty or an existing Git remote repository must exist.

Initialize a Git directory

To start controlling with Git any directory, open a terminal inside it and run:

git init

Then add the link to the remote repository:

git remote add origin https://<git-server>/<user>/<name-repo>.git

Finally, link the local branch master with the remote one:

git push --set-upstream origin master

Clone a Git repository

To start controlling with Git a copy of an existing remote repository <name-repo>, open a terminal where to save the clone and run:

git clone https://<user>@<git-server>/<user>/<name-repo>.git

Create .gitignore file

A .gitignore file specifies the directories or the files that Git should ignore.

  1. Create the .gitignore file within the root directory of a local repository.
  2. List the directories or the files to be ignored:
    # ignore these directories and their content
    <dir>
    # ignore these files
    <path>/<file.ext>
  3. Push the changes, to the remote repository.

Tip: to ignore the content of a directory except for one sub-directory:

!dir/  # do not ignore this directory
dir/*  # .. but ignore its content
!dir/sub-dir/  # .. except this sub-directory