By creating a new branch in your git repository, you can make a clone of the master branch & make changes to it, while the real master branch stays unchanges & leading. You can then later merge your own branch with the master branch & commit your changes. If you are working on a website, while someone else is also working on it, it might be a good idea to create your own branch. Its very simple & easy to do, and it makes sure your changes are never overwritten by someone else.
Here’s a situation that describes the usefulness of branching:
- You are working on your website
- You have created a branch for your changes. Example: branch “link colors”
You then notice that your logo isn’t aligned with your content, and you want to fix the issue immediately. Because you were on a different branch you can now:
- Switch back to your master branch.
- Fix the logo alignment
- Commit the changes to the Bare repository, and pull them on the live site.
- Checkout your branch “link colors” again, and continue working on your changes.
If you had not used branches, you wouldn’t be able to commit the hotfix so easily, because the files of the unfinished changes would also be committed. This could have broken your complete live site.
To create your new branch you can use the following command:
git checkout -b newbranchname
You will get the message that you have switched to your new branch, and you can start making changes to your project. When you are ready to upload your changes, you will have to merge your branch with the master branch. To do this, we have to switch back to the master branch, as we’ll be merging from there. Use this command:
git checkout master
Now that you are back on the master branch, you can easily merge your changes. To do this type:
git merge newbranchname
The changes you’ve made on the branch newbranchname have now been added to the master branch and can be committed.