Skip to main content

Git

Article image
Nardastudio article git background
Autor: NardaStudio Data: 20 Nov 2019

At first GIT

The first step to acquire the skills of good operation of the GIT version control system is understanding local repository activities. Connected with it concepts such as working directory, staging area, what is revision, commit, etc. The important thing is branch. There can be many branches in the project. To start with it, it's worth reading one of the first chapters of the book on the official GIT website available                      online for free (1.3 Getting Started - What is Git?).

Main page of the project: https://git-scm.com

At the beggining information that I'm using GIT version 2.17.1 installed on Linux. If you want to check the version, just use the command git version. Installation location of GIT (ex. to access the configuration file) we can check on Linux with command which git.

Help!

Just entering the command git will show to us brief information about basic commands. However, the first important argument, which at first is very much useful for learning GIT is help significantly facilitating the possibility of learning the entire range of available commands. We can also read the tutorials available in the program. Using command itself git help it shows us a list of basic commands with explanations, but that is important to learn about using the command git help along with other arguments.

Example usage:

  • git command help - will show us the help (instructions for use) of the indicated command.
  • git help -a - will show a list of all available commands whose meaning can be checked by the above example.
  • git help -g - writes a list of available guides. It is worth planning to read each of them at the beginning.
  • git command -h - will throw us out an abbreviated version of help related to the specified command.

Configuration and creation of the repository

After installing the GIT software on our computer, it is important to perform the configuration globally. The local configuration is done in the project folder. The file responsible for configuring our repository is the .gitconfig file. The current settings are obtained by calling git config --global -l or git config --local -l. It is worth familiarizing yourself with the available config arguments using the additional help argument.

Remember about:

  • git config --global user.name "User Name" - username configuraton. For larger projects, when working in a company with others, it's best to set your name.
  • git config --global user.email email@email.com - email address configuration for the user.
  • The above information is saved at the time of the commit. It is known by whom the commit was executed.
  • The list of available arguments and their description is available at https://git-scm.com/docs/git-config or after executing git help config.

We plan to initiate a repository and then expand it in our project folder. We call the git init command for this. The .git directory created is responsible for the entire local project repository. Deleting the .git directory will result in the loss of our local repository with the entire change history. However, if we want to exclude individual files so that they are not tracked in our repository, we will need to edit the .gitignore file. This file is important for projects such as Symfony, Drupal, Wordpress.

In the working area

We are working on the project making changes on files content, so to check the current status of changed files (which have been changed or waiting to be added to the waiting room) we check with the git status command.

git add allows us to add changed files to the waiting room (staging area), where they will wait for commit. We can use git add changed_file.txt to add a single file to the waiting room, but using git add . will add all the peaks.

Command git diff file_name will show differences/changes in the mentioned file. Or, depending on the arguments, you can see the changes between commits.

After completing the changes and adding files to the waiting room, we call git commit -m "Our short message about commit". At this point, we confirm the changes and add short info about changes.

To get an overview of the list about saved commits to execute git log command. We can format the list in many ways using the appropriate arguments, e.g. git log --pretty="oneline" or git log --oneline.

Each saved commit has a unique identifier, author, date and short note. IDs or dates allow you to search changes, e.g. git show commit_ID.

A very important element when working with the GIT version control system are branches. Thanks to them we can easily create separate versions of changes. For use to handle branches there are commands like git branch, git checkout, git merge, git rebase. The interactive tutorial Learn Git Branching is the perfect solution for the learning of using branches in practice.

GitHub - remote repositories

To add a remote repository created earlier on the GitHub account for our project, we execute the command git remote add origin https://remote-repo.git. git remote will show already added remote repositories.

A helpful source of knowledge related to GitHub is GitHub Learning Lab.

Examples of commands

  • git clone https://link-to-repo - copy the remote repository to a local directory.
  • git push - we send changes to the remote repository.
  • git push origin master - we send changes to the remote repository whose name/alias is 'origin' and a branch is 'master'.
  • git fetch - we download changes but do not merge them with the local repository.
  • git pull - unlike fetch, it downloads changes and merges them with the local repository.

Readme.md (markdown file)

This is a file that is a description and documentation of the repository. So let's know the basic rules for writing such a file. The Mastering Markdown link will help.

Przydatne linki

This site is not compatible with your device. Launch the page on a computer or newer device.