Git & GitHub - Branch & Merging
A beginners guide to git branch and git merge
Table of contents
This article is a continuation of the earlier article Git stage & snapshot. In this article, we'll see how to isolate the codes by working with branches and integrating the changes.
Git Branch
When you're working on a project with a team, it's a good idea to keep your codes separate. Imagine there are four people on the team, and each person is working on a different part of the project. Instead of all of you making changes directly to the main project, each person should create a separate area, called a branch, to work on their part.
This way, everyone can work on their feature without disturbing others. It reduces confusion and makes it easier to see what each person is doing. If someone makes a mistake or an error in their work, it won't mess up the main project because it's kept separate. Once everything is tested and ready, the changes from each branch can be combined with the main project. It helps the team work together smoothly and makes sure the main project stays stable and reliable.
You can create a new branch using the git branch [branch-name] command.
git branch feature1
If you want to view all your branches, use the git branch command.
git branch
This will list all your branches. Also, there will be a * mark next to one of the branches indicating the currently active branch.
Git Checkout
If you are working on multiple branches, then you can switch between the branches using the git checkout [branch-name] command.
git checkout feature2
If you want to switch t your main branch simply use the git checkout main command.
Git Merge
After you've finished coding your part, if you want to combine your work with another branch, like "feature1," you can do it using the following steps:
First, switch to the branch where you want to merge your changes, in this case, "feature1"
Next, use the command "git merge [branch-name]" to merge the changes from the other branch, "feature2"
By following these steps, the changes made in "feature2" will be brought into "feature1," and both sets of work will be combined in branch "feature1".
That's on the git branch and git merge commands. In the upcoming articles, we'll see how to inspect the logs and compare the commits in different branches. If you have any queries, do write it in the comments below and follow me for more such content.