ComputersSoftware

Basic Git commands: Crib

In the programming of serious automated systems, as a rule, several developers are involved, who are working on one project. At the same time, it is necessary to somehow realize the possibility of parallel maintenance of the task without prejudice to it. Every team member should know that he is writing code that another programmer can not remove or change later.

It is very important to be able to see the history of changes in the development, thereby quickly and reliably localizing problematic places and errors.

Version control systems are designed to handle these tasks. And one of them is a product called Git.

Version control systems: little theory

If in brief - any version control system allows you to save all changes made to the project file. This makes it possible to monitor the errors in the code and quickly fix them.

Conventionally, version control systems can be divided into three types:

  • Local;
  • Centralized;
  • Distributed.

Types of version control systems

The local variant allows to maintain a database with all the changes in the file of the project being developed. This kind is the most simple and accessible and is able to work on the computer of a programmer or participant.

Centralized version control systems solved such an urgent problem as the work on the project by several developers. Its essence lies in the fact that the files are stored not on the local computer, but on the specific server to which it is connected. Thus, project participants can access different versions of files, and it becomes easier to control who is doing what and when. However, if for some reason the server is unavailable or out of order, serious problems will arise. This can be further aggravated by the fact that it is not always possible to restore all the data back.

Distributed version control systems every time you access a centralized server create a complete copy of all project files on the user's local computer. That is, at any time there can be multiple copies of one project - on the machines of participants and on the server. Given the flexible mechanisms of branching with this approach, it is possible to conduct parallel development in different directions, while remaining within the framework of one project. Git refers to distributed version control systems.

Git: description and features of the system

The Git version control system has its own features. Most systems store files, changing them according to the instructions in the project. That is, for example, the version of the current development under number 3 can contain data about the changes in file A and B. And already version 4 will have A, B and B. Thus, the files change as necessary.

Git works a little differently. Each version of the project will contain variants of all files A, B and B. Regardless of how many of them will be changed. Of course, Git does not store every instance of the file, but uses only a reference to it.

Another important feature is the ability to work directly with local storage. That is, if you want to make edits, then they are implemented directly on the computer of the project participant. This is very convenient when the developer is away from the Internet. Then, when you gain access to the server, you will be able to send all the data to the shared store.

To preserve the integrity of the data, the hash method of each modified file is performed using the SHA-1 method. This allows the version control system to know exactly where, who and when changed the file.

Git: Installation

In order to start working with Git, you need to install it. The version control system is available for use in Windows, Mac OS, Linux.

The version for Windows can be downloaded at: git-for-windows.github.io. After downloading the program must be installed. The installer is simple, so this procedure should not cause problems.

Very often, users encounter such a problem, in which the console writes that Git is not an internal or external command when trying to enter data. The solution is to add the full path to the directory in the PATH environment variable.

First teams

After installing the version control system on the computer, you must configure it. It is worth noting that by this time on the site Github must be registered their account. In order to perform the initial configuration, you need to call the console client, as you will have to enter commands. You can do this by clicking the right mouse button and selecting Git Bash. A console client window should be opened that prompts you to enter the data. In it, you need to run the Git Bash command sequentially:

  • Git config --global user.name '' Name '';
  • Git config --global user.mail '' Email address ''.

At the same stage, you need to configure the method of ending the lines with two commands:

  • Git config --global core.autocrlf true;
  • Git config --global core.safecrlf false.

To initialize Git this is enough. Next, only commands for project management will be used.

Basic Git Commands

  • Init: this command creates a new repository.

Example usage: init name of the project.

  • Clone. Copies an existing repository from a remote source.

This is how the git clone command is used: clone git: //github.com/path to the repository.

  • Add. The most commonly used command in the Git version control system. It performs a simple task - adds the specified files to a special area, called an index or a scene. You can transfer several files or folders to it, which you will later need to add to the repository, or, in Git, "commit".

An example of using this Git command looks like this: add some_file.txt.

  • Status. Allows you to view the list of files that are present in the index and working folder. Serves to control and view ready-to-commit data or their modified, but not added, versions into the scene.

  • Diff. Shows the difference of states. For example, using this Git command, you can determine whether there is a change between the project folder and the index.
  • Commit. It keeps a snapshot of everything that was in the index directly to the database. As a result of the Git command, a text file is displayed on the screen, in which you can specify which changes were made. And also information will be displayed on how many files the commit has been subjected to, and its checksum. The main thing is not to forget that after a change to the database only those data that were added to the index by the git add command will get.

Additional Git commands

  • Reset. The functions of this command are indicated by its name. It simply throws out from the special intermediate area - the index, the specified file, placed there by chance. It's worth using the reset command with care when using the - - hard command, as this will affect the files in the working directory, which can lead to unforeseen consequences.
  • Rm. Most accurately, this command can be described as a reverse git add, since it deletes files from the index. However, this also from the working folder.

Example usage: git rm some_file.txt.

  • Mv. Moves the file.
  • Clean. It is intended for cleaning the project folder from unnecessary files.

The presented commands are used for the overall needs of the project.

Working with the repository branches in Git

To manage branches in Git there is a special set of commands. They are able to connect, delete, create branches in Git. The list of commands is presented below.

  • Branch. This command has several keys available, using which you can flexibly manage the branches in the project. Branch is a kind of multiprofile tool for full control over the state of the repository. A simple git branch call will list all available storage branches. The -v option added to the command will display which commits have been committed recently. And using -d will delete the specified branch. Branch can not only delete, but also create. Running git branch name_light will lead to the organization of a new branch in the project. It should be taken into account that the indicator of the current working position is different in this case. For example, by creating the name of a light, you can actually be in the master branch.
  • To move to the desired item, there is a command Git checkout the necessary_light, which will move the pointer to the required branch.
  • Checkout. As mentioned above, it performs a switch.
  • Merge. This command allows you to merge several branches together.
  • Log. The function displays all changes from the beginning of the project and to the last commit. Using a variety of keys in conjunction with a command call allows you to extend its functionality. For example, calling git log -p -2 will show detailed information about the changes in each commit. The second key -2 indicates that you only need to show the last 2 changes. The --stat argument added to the git log call will perform almost the same thing as -p, but in a more detailed and compact form. Also, using git log, you can display the change information by creating your own display format using the pretty key format options. To give a special form, you need to use some kind of regular expression. For example, such a record get log --pretty = format '% h,% an,% ar,% s'' will output a short commit hash, then its author, date and comment of the change. It is very convenient to use when viewing a large number of commits.

Commands for distributed work in the system

  • Fetch. When you enter this git command, the console will migrate all changes from the remote vault to the local one.
  • Pull. The git pull command is a symbiosis of the two listed above - git fetch and git merge. That is, it first gets the information from the remote repository, and then merges with the currently used branch.
  • Push. It is from the name of this command in the user's environment that the expression "run" appears, which means a connection to a remote repository and the transmission of changes from the local repository.

Remote Management Commands

  • Remote. It is a powerful tool for managing remote repositories. Using remote, you can delete, view, move, or create new ones.
  • Archive. The name speaks for itself. The command allows you to create an archive with the necessary changes, for example, to prepare for transferring it over the Web.

How to use this cheat sheet

The materials presented in the article do not reflect all Git teams. The cheat sheet is designed to help beginners who want to master this rather complex product for version control. For people who have been actively using Git for some time, it will help remember the team's suddenly forgotten key or its writing.

In fact, the Git version control system has a huge potential for configuration and management. The abundance of teams and several keys used in them - the best proof of this. For those who want to thoroughly study all the properties and settings of Git, there are a lot of manuals, including the official one from Github, which describes in detail the system as a whole and all the subtleties of using commands.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.birmiss.com. Theme powered by WordPress.