Merging branches in Git
Typically, new branches are created as forks of the main branch to solve some problem. Once the problem is solved, we can merge the contents of the side branch into the main one. This is done with the merge command.
Let's look at this process in more detail. Let's say we have a branch master and a branch test.
Let's switch to the test branch, make changes to the file, stage it, and save the commit:
git switch test
git add file.txt
git commit -m 'commit from test'
After that, let's return to the master branch:
git switch master
Now let's merge the contents of the test branch into our master branch:
git merge test
After running this command, Git will ask us to enter a merge comment. After entering the comment, a message about a successful merge will appear in the terminal.
However, after the merge, the test branch will not disappear. Its contents will merge into the main branch. The main branch will change after the merge, but the test branch will remain the same. We can continue working with the test branch, periodically sending its contents to the main branch.
It is important to pay attention to the terminology here. In literature and in life, we talk about merging two branches or about merging branches. And when we say this, it seems that there were two different branches and they merged into one - a common one. In fact, we do not merge branches, but pour the contents of one branch into another. In life, they do not say this (and you do not say it), but always understand the essence of the process - we pour, not merge.
Let's say you have a branch master. Create another branch problem. In the new branch, create a file, stage it, and commit it. Merge your branch problem into the branch master.
Change your file in the problem branch. Stage it and commit it. Merge your problem branch back into the master branch.