User Tools

Site Tools


Action disabled: media
software:git:start

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:

  • local repository: all the versions of the project saved on one's computer;
  • remote repository: all the versions of the project saved on a Git server;
  • Git server: the name of the server who hosts Git, so that <git-server> can be bitbucket.org, github.com, gitlab.com or similar;
  • untracked file: file that has been created in the local repo but not followed yet by Git;
  • modified file: file that has been modified in the local repo and, therefore, different from the one in the remote repo;
  • staging area: buffer between the local and the remote repository where files are sent after have been created or modified and ready to be redirected to the remote repository;
  • staged file: file, in the staging area, ready to be committed;
  • committed file: file, in the staging area, ready to be pushed in the remote repo;
  • HEAD: pointer used by Git which refers to the last commit on a particular branch.
  • origin: is the default name for a remote branch when is executed git clone;
  • master: is the default name for a starting branch when is executed git init.

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):

  • temporary saving (time is in seconds):
    git config credential.helper 'cache --timeout 7200'
  • permanent saving:
    git config credential.helper store

    Important: now credentials are unencrypted on disk, protected only by filesystem permissions.

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
software/git/start.txt · Last modified: 2023/12/03 11:46 by tormec