User Tools

Site Tools


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.

Install Git on one's PC

These are the steps to install Git:

  1. install git package through Synaptic Package Manager;
  2. set user.name and user.email 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

It's possible to cache username and password in order to avoid their input every time a connection to the remote is established. The cache mode can be set to keep credentials in memory for a given period of time in seconds:

git config --global credential.helper 'cache --timeout 7200'

Create a new remote repository

These the steps to create a new repository in one's Git server:

  1. in one's Git server, create a repository called <name-repo>;
  2. the link to this repository will be like:
    https://<user>@<git-server>/<user>/<name-repo>.git

Clone in local a remote repository

The steps below are intended when the remote repository is already created and it contains a project.

Then:

  1. open a terminal in the directory in which save the local repository and give the command:
    git clone https://<user>@<git-server>/<user>/<name-repo>.git
  2. will be created the folder name-repo, containing the copy of the remote repository.

Record changes to the local and remote repositories

Figure 1: Main Git's commands to operate with repositories.

The steps below are applied when there is some files, in the local repository, ready to be pushed to the remote repository.

  1. Open a terminal in the local repository and add the files to the stage area:
    git add --all

    or

    git add <name-file>

Before commit the files it's possible to do as many changes as necessary to them, and when they are finally ready, run the add command again.

  1. Commit the changes:
    git commit -m '<your message here>'
  2. Push to the remote repositoy:
    git push origin master

    or

    git push origin <name-branch>

    To force files to be pushed in the remote repository, overwriting the existing ones:

    git push -f origin <name-branch>

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, following the steps described in Push to remote repository.

Check the status of the local repository

The following command is used to inspect the status of the local repository respect to the remote one.

git status

See differences

To see if there are any difference between the working directory and the HEAD:

git diff HEAD

List commit logs

git log --oneline

List files committed

git show --pretty="" --name-only HEAD

Update the local repository

To fetch changes from the remote repository and merge them into the local one:

git pull

To force files to be pulled in the local repository, losing changes not already pushed in the remote one:

git fetch --all
git reset --hard origin/<name-branch>

Work with branches

Branches are just pointers to commits, this means that Git doesn't create, and shouldn't be created, a whole new set of files or folders.

The following steps are applied when we want to work on a separate branch different from the mainly one, called master.

  1. Create a new branch in the local repository:
    git branch <name-branch>
  2. Switch to the new branch with:
    git checkout <name-branch>
  3. Push the changes, to the remote repository, following the steps described in Push to remote repository

To know in which branch we are now:

git branch

The next steps are intended when the work on a secondary branch is ready to be imported in the mainly one.

  1. To merge edits from <name-branch> into master branch, switch to this one:
    git checkout master
  2. Merge the work:
    git merge <name-branch>
  3. Push, to the remote repository, the merged commit:
    git push origin master

If merge conflict happens it must be manually solved.

  1. Open the file that has a merge conflict
  2. Here changes are separated by:
    <<<<<<< HEAD
    changes made in master
    =======
    changes made in <branch-name>
    >>>>>>> <branch-name>
  3. Decide what changes to keep;
  4. Push the changes, to the remote repository, following the steps described in Push to remote repository (remember you are in master now).

To delete the branch:

git branch -d <name-branch>
software/git/start.txt · Last modified: 2020/04/06 23:48 by tormec