Instead of an introduction
Hello, future Senior Software Engineer.
Git Basics
Git is a distributed version control system for our code. Why is she to us? For distributed teams, some kind of work management system is needed. Needed to keep track of changes that occur over time. That is, step by step, we see which files have changed and how. This is especially important when you analyze what was done within the framework of one task: this makes it possible to go back. Imagine a situation: there was a working code, everything was fine in it, but we decided to improve something, fix it here, fix it here. Everything is fine, but such an improvement broke half of the functionality, made it impossible to work. So, what is next? Without Git, one would have to sit for hours and remember how everything was originally. And so we just roll back a commit back - and that's it. Or what if there are two developers, who make their changes to the code at the same time? Without Git, it looks like this: they copied the code from the original, did what they needed. The moment comes and both want to add their changes to the main folder. And what to do in this situation?.. I don't even undertake to estimate the time to do this work. There will be no such problems at all if you use Git.Installing Git
Install git on your computer. I understand that everyone has different OS, so I will try to describe for several cases.Installation for Windows
As usual, you need to download the exe file and run it. Everything is simple here: click on the first Google link , install and that's it. For work, we will use the bash console that they provide. To work on Windows, you need to run Git Bash. Here's how it looks in the start menu:

Installation for Linux
Usually git is already installed and included in Linux distributions, as it is a tool originally written for developing the Linux kernel. But there are situations when it is not. To check this, you need to open a terminal and type: git --version. If there is an intelligible answer, nothing needs to be installed. Open terminal and install. I work on Ubuntu, so I can tell you what to write for it: sudo apt-get install git. And that's it: now you can use the git in any terminal.Installation on macOS
Here, too, you first need to check if there is already a git (see above, as on Linux). If not, the easiest way is to download the latest version from here . If XCode is installed, then git will already be automatically installed.Git setup
The git has a user setting from which the work will go. This is a reasonable and necessary thing, because when a commit is created, git takes exactly this information for the Author field. To set up a username and password for all projects, you need to write the following commands:
git config --global user.name ”Ivan Ivanov”
git config --global user.email ivan.ivanov@gmail.com
If there is a need to change the author for a specific project (for a personal project, for example), you can remove --global, and it will turn out like this:
git config user.name ”Ivan Ivanov”
git config user.email ivan.ivanov@gmail.com
A little theory...
To be in the subject, it is advisable to add a few new words and actions to your appeal ... Otherwise, there will be nothing to talk about. Of course, this is some kind of jargon and tracing paper from English, so I will add meanings in English. What are the words and actions?- git repository (git repository);
- commit (commit);
- branch
- merge (merge);
- conflicts;
- spool (pull);
- push (push);
- how to ignore some files (.gitignore).
States in Git
The Gita has several states to understand and remember:- untracked (untracked);
- modified (modified);
- prepared (staged);
- committed.
What does it mean?
These are the states that the files from our code are in. That is, their life path usually looks like this:- A file that is created and not added to the repository will be in the untracked state.
- We make changes to files that have already been added to the git repository - they are in the modified state.
- Of those files that we have changed, we select only those (or all) that we need (for example, we do not need compiled classes), and these classes with changes fall into the staged state.
- A commit is created from the prepared files from the staged state and goes into the git repository. After that, the staged state is empty. But modified can still contain something.

What is a commit
A commit is the main object in source control. It contains all the changes since that commit. The commits are linked together as a singly linked list. Namely: There is a first commit. When a second commit is created, it (the second one) knows what comes after the first one. And in this way you can track the information. The commit also has its own information, the so-called metadata:- a unique commit identifier by which it can be found;
- the name of the author of the commit who created it;
- the date the commit was created;
- a comment that describes what was done during that commit.

What is a branch

Getting started with Git
You can work only with a local repository, and with a remote one. To work out the necessary commands, you can use only the local repository. It stores all information only locally in the project in the .git folder. If we talk about the remote, then all the information is stored somewhere on the remote server: only a copy of the project is stored locally, the changes of which can be pushed (git push) to the remote repository. Here and below we will discuss working with the git in the console. Of course, you can use some graphical solutions (for example, in Intellij IDEA), but first you need to figure out what commands are happening and what they mean.Working with git in a local repository
Next, I suggest that you follow all the steps that I did while you read the article. This will improve your understanding and assimilation of the material. So bon appetit :) To create a local repository, you need to write:
git init

git status

- git add -A - add all files from the state to staged;
- git add . - add all files from this folder and all internal ones. Essentially the same as the previous one;
- git add <filename> - adds only a specific file. Here you can use regular expressions to add according to some pattern. For example, git add *.java: this means that you need to add only files with a java extension.
git add *.txt
To check the status, we use the command we already know:
git status

git commit -m “all txt files were added to the project”

git log

git status

git status

git diff

git add test_resource.txt
git commit -m “added hello word! to test_resource.txt”
To see all commits, we write:
git log

git add GitTest.java
git commit -m “added GitTest.java”
git status

Working with .gitignore
Clearly, we only want to keep the source code and nothing else in the repository. What else could there be? At a minimum, compiled classes and/or files that create development environments. To make git ignore them, there is a special file that needs to be created. We do this: we create a file at the root of the project called .gitignore, and in this file each line will be a pattern to ignore. In this example, git ignore would look like this:
```
*.class
target/
*.iml
.idea/
```
Let's look now:
- the first line is to ignore all files with a .class extension;
- the second line is ignoring the target folder and everything it contains;
- the third line is ignoring all files with the .iml extension;
- the fourth line is ignoring the .idea folder.
git status


git add .gitignore
git commit -m “added .gitignore file”
And now the moment of truth: we have an untracked compiled GitTest.class that we didn't want to add to the git repository. This is where git ignore should work:
git status

Working with branches and others
Of course, it is inconvenient to work in one branch alone and impossible when there is more than one person in the team. There is branching for this. As I said before, a branch is just a movable pointer to commits. In this part, we will look at work in different branches: how to merge changes from one branch to another, what conflicts can arise, and much more. To see a list of all branches in the repository and understand which one you are on, you need to write:
git branch -a

- create a new branch based on the one we are on (99% of cases);
- create a branch based on a specific commit (1%).
Create a branch based on a specific commit
We will rely on the unique commit identifier. To find it, we write:
git log

git checkout -b development 6c44e53d06228f888f2f454d3cb8c1c976dd73f8
A branch is created that will only have the first two commits from the master branch. To test this, we first make sure we have moved to another branch and look at the number of commits on that branch:
git status
git log

git branch -a

Create a branch based on the current one
The second way to create a branch is to create from another. I want to create a branch based on the master branch, so I need to switch to it first, and the next step is to create a new one. We look:- git checkout master - go to the master branch;
- git status - check if it's on the master.

git checkout -b feature/update-txt-files

Resolving conflicts
Before dealing with what a conflict is, we need to talk about merging (merging) one branch into another. This picture can show the process when one branch is merged into another:

git add *.txt
git commit -m “updated txt files”
git log

git checkout master
git merge feature/update-txt-files
git log

git branch -D feature/update-txt-files
As long as it's clear, right? We complicate the situation: now let's say that we again need to change the txt file. But now also in the wizard this file will be changed as well. That is, it will change in parallel, and git will not be able to understand what needs to be done in a situation where we want to merge new code into the master branch. Go! We create a new branch based on master, make changes to text_resource.txt and create a commit for this case:
git checkout -b feature/add-header
... делаем изменения в файле

git add *.txt
git commit -m “added header to txt”

git checkout master
… обновor test_resource.txt

git add test_resource.txt
git commit -m “added master header to txt”
And now the most interesting point: you need to merge the changes from the feature/add-header branch to master. We are on the master branch, so all we have to do is write:
git merge feature/add-header
But we will get the result with a conflict in the test_resource.txt file: 

- between “<<<<<<< HEAD” and “=======” are the master changes that were on that line in the master branch.
- between “=======” and “>>>>>>> feature/add-header” are the changes that were in the feature/add-header branch.

git status

git add *.txt

git commit

Working with remote repositories
The last step is to deal with a few more commands that are needed to work with the remote repository. As I said, a remote repository is some place where the repository is stored and from where you can clone it. What are remote repositories? Examples of darkness:-
GitHub is the largest repository for repositories and collaborative development. I have already described it in previous articles.
Follow my github account . I often exhibit my work there in the areas that I study during my work. -
GitLab is an open source web- based DevOps lifecycle tool that provides a code repository management system for Git with its own wiki, issue tracking system , CI/CD pipeline, and other features. After the news that Microsoft bought GitHub, some developers duplicated their work in GitLab.
-
BitBucket is a project hosting and collaborative web service based on Mercurial and Git version control. At one time had a big advantage over GitHub in that it had free private repositories. Last year, GitHub also opened up this feature to everyone for free.
-
And so on…
git clone https://github.com/romankh3/git-demo
You now have a complete copy of the project locally. To be sure that the latest copy of the project is locally, you need to, as they say, spool the data by writing:
git pull


git add test_resource.txt
git commit -m “prepated txt for pushing”
And now the command to push this to the remote repository:
git push

useful links
- The official dock on the git, is in Russian . Recommended as a reference.
- git
GO TO FULL VERSION