Understanding Branching in Git
The most useful tool in Git is branches. They allow you to work on a project in parallel, switching between branches as needed, solving various problems. Let's look at an example of what branches are and how they are used in practice.
So, as you already know, by default we have one branch - master
. Usually this branch is considered the main one. However, you can also create additional branches, usually to solve specific project tasks.
When you create a new branch, it will contain copies of our project files. We will write code in them, and then, when the task is completed, we will merge the code of the new branch with our main branch. This way, you can work on several problems in parallel, so that they do not interfere with each other. And, when ready, send the resolved problems to production (that is, for users to use).
At the same time, when switching between branches, Git will change the files in the folder with your project itself. That is, some files will disappear, and others will be shown. The texts of the files will also change if they differ from branch to branch. This is an important point, but beginners do not always understand it. So once again: when you change a branch in the terminal, your files and the texts in them will change in accordance with the branch. Physically! Themselves! Right in the open editor, the text of your file will change to another! This is very cool and convenient.
Let's look at an example of how this is done in practice. Let's say you have a project. The master
branch contains its current production version, which is used by users. Let's say you are developing a new feature. To do this, you created a new branch feature
. In it, you write code, implementing the task you need. Suddenly, the phone rings and your boss says that a serious bug has been found on the project and it urgently needs to be fixed.
To do this, you create a branch fix
, copying all the project code from the master
branch into it. You write the code that fixes the bug, and then merge this code into the main master
branch and push this code to production.
Then you switch to the feature
branch and continue working on your feature. When it's finished, you merge it into the main master
branch.
In the following lessons we will continue to get acquainted with branches in practice.