Basic Git Commands: Cheat Sheet

In the programming of serious automated systems, as a rule, several developers are involved who are working on the same project. At the same time, it is necessary to somehow realize the possibility of parallel task management without prejudice to it. Each team member should know that he is writing code that another programmer will not be able to delete or change subsequently.

At the same time, it is very important to be able to view the history of changes in development, thereby quickly and reliably localizing problem areas and errors.

git commands

Version control systems are called upon to cope with these tasks. And one of them is a product called Git.

Version control systems: a bit of theory

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

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

  • local
  • centralized;
  • distributed.

Types of Version Control Systems

The local version allows you to maintain a database with all the changes in the file of the developed project. This view is the most simple and affordable and is able to work on the computer of a programmer or participant.

Centralized version control systems have solved such an urgent problem as working on a project with the help of several developers. Its essence lies in the fact that the files are not stored on the local computer, but on the specific server to which it is connected. Thus, project participants can access various versions of files, and it becomes easier to control who and what is involved in the development. However, if for some reason the server is unavailable or fails, serious problems will arise. This can be compounded by the fact that it is not always possible to restore all data back.

git bash commands

Distributed version control systems each time they access a centralized server create a complete copy of all project files on the user's local computer. That is, at any given time, there may be many copies of one project - on the machines of the participants and on the server. Given the flexible branching mechanisms 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 system features

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

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

git is not an internal or external command

Another important feature is the ability to directly work with local storage. That is, if you need 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 gaining access to the server, it will be possible to send all the data to the shared storage.

To preserve data integrity, the hash method of each modified file is applied 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. Version control system is available for use in Windows, Mac OS, Linux.

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

basic git commands

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

First teams

After installing the version control system on the computer, it must be configured. It is worth noting that by this moment your account should be registered on the Github website. In order to perform the initial configuration, you need to call the console client, since you have to enter commands. You can do this by right-clicking and selecting Git Bash. A console client window should open, prompting you to enter data. In it, you need to execute 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 line ending method using two commands:

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

For the initial setup of Git, this is enough. Further only teams for project management will be used.

git console commands

Basic Git Commands

  • Init: this command creates a new repository.

Usage example: init project name.

  • 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 - it 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 need to subsequently add to the repository or, in Git language, โ€œcommitโ€.

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

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

git clone team

  • Diff Shows the difference in state. For example, using this Git command, you can determine if there are changes between the project folder and the index.
  • Commit. It saves the nugget of everything that was in the index directly to the database. As a result of the work of the Git command, a text file will be displayed on the screen in which you can indicate exactly what changes have been made. It will also display information about how many files have been committed and its checksum. The main thing is not to forget that after the change, only the data that was entered into the index by the git add command will get into the database.

Additional Git Commands

  • Reset The functions of this command are indicated by its name. It simply throws out a special intermediate area - the index, the specified file, placed there by chance. You should handle reset with caution when using the command with the - - hard switch, as this will also affect the files in the working folder, which can lead to unforeseen consequences.
  • Rm. Most accurately, this command can be described as the reverse of git add, since it removes files from the index. True, at the same time also from the working folder.

Usage example: git rm some_file.txt.

  • Mv. Used to move the file.
  • Clean Designed to clean the project folder from unnecessary files.

The presented teams are used for the general needs of the project.

Working with repository branches in Git

Git has a special set of commands for managing branches. They are able to join, delete, create branches in Git. The list of commands is presented below.

  • Branch This team has several keys that can be used to flexibly manage branches in the project. Branch is a multi-profile tool for full control over the state of the repository. A simple call to git branch will list all available storage branches. The -v switch added to the command will display which commits have been committed recently. Using -d will delete the specified branch. Branch can not only delete, but also create. Running git branch branch_name will result in the organization of a new branch in the project. It should be noted that in this case, the index of the current working position is different. For example, by creating a branch name, you can actually be in the master branch.
  • To move to the desired item, there is a Git checkout command needed_light that will move the pointer to the desired branch.
  • Checkout As mentioned above, 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 to the last commit. Using a variety of keys in conjunction with a command call allows you to expand its functionality. For example, a call to git log -p -2 will allow you to view detailed information about the changes in each commit. The second key -2 says that you need to show only the last 2 changes. The --stat argument, added to the git log call, will do almost the same thing as -p, but in a more detailed and compact form. Using git log, you can also display information about changes by creating your own display format using the format options of the pretty key. To give a special look, you need to use some semblance of regular expressions. For example, such a record get log --pretty = format ''% h,% an,% ar,% s '' will display a short hash of the commit, 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 transfer all changes from the remote repository 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 receives information from the remote repository, and then merges with the branch currently in use.
  • Push It is from the name of this command that the expression โ€œstartโ€ appeared in the user environment, which means connecting to a remote repository and transferring changes from the local one there.

Remote control 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, in preparation for transferring it over the Network.

How to use this cheat sheet

The materials presented in this article do not reflect all Git commands. The cheat sheet is more likely 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 to remember a team key that was suddenly forgotten or spelling it.

git cheat sheet

In fact, Git's version control system has enormous potential for configuration and management. The abundance of commands and several keys used in them are the best confirmation of this. For those who wish to study in detail all the properties and settings of Git, there are many manuals, including the official one from Github, which describes in detail the system as a whole and all the subtleties of using commands.

Source: https://habr.com/ru/post/K21082/


All Articles