Today we are going to discuss Git branches.
In Git, branches are a part of your everyday development process. Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug—no matter how big or how small—you spawn a new branch to encapsulate your changes. So it is easy for our development process.
For bug fixes
To add new features
List all of the branches in your repository.
This symbol * shows what repository we currently work.
Create a new branch called icons and slider.
git branch icons
git branch slider
How to change the working branch?
git checkout icons
Now we are in the icons branch, we added a font-awesome file within this branch. After we commit. Then, we have to merge this branch with the master branch. How to do that?
1. Let’s first go to the main branch (master branch).
git checkout master
2. After, merging the icons branch with master
git merge icons
3. Then release a version of our project.
git tag -a v1.0.2 -m ‘adding social icons’
This merge is called Fast-forward merge. Fast-forward merges literally move your main branch's tip forward to the end of your feature branch. This keeps all commits created in your feature branch sequential while integrating them neatly back into your main branch.
Now let’s see the git conflict merge?
Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it. In these cases, Git cannot automatically determine what is correct. Conflicts only affect the developer conducting the merge, the rest of the team is unaware of the conflict. Git will mark the file as being conflicted and halt the merging process. It is then the developers' responsibility to resolve the conflict.
Types of git merge conflicts:
Git fails to start the merge
Git fails during the merge
So, let’s see one Example to easily understand this concept.
Think about this, we created another branch called slider. How to merge this branch with the new main branch called master. Follow these steps:
1. First go to the branch that you need to merge.
git checkout slider
2. Then add your new features to the working directory and add them to the staging area. After committing that change.
git add .
git commit -m ‘adding flex slider’
3. Finally merge your branch with the main branch called master. This merge is called a git merge conflict. In this way, we have some problems. Let’s discuss this.
git checkout master
git merge slider
You can see this below when you merge the branch.
Why does this problem occur?
Because our new changes are based on V1.0.1. But now our HEAD is at V1.0.2. So we have to use a 3-way merge. 3-way merges use a dedicated commit to tie together the two histories. The nomenclature comes from the fact that Git uses three commits to generate the merge commit: the two branch tips and their common ancestor.
How to do that?
First of all, we delete the current change or incoming change or add two changes. After adding these changes in the staging area again and committed.
git add .
git commit -m ‘merge conflicts resolved’
Finally, release the other version of your project.
git tag -a v1.0.3 -m ‘adding slider’
git log –oneline
In this document, we discussed Git's branching behavior and the git branch command. The git branch commands primary functions are to create, list, rename and delete branches. To operate further on the resulting branches the command is commonly used with other commands like git checkout.
I hope this is useful for all of us. If you want to be interested in watching more videos about git. Please watch Appslanka git videos on our youtube channel.
Don't forget to subscribe.