15. Creating and Experimenting with Branches

Working with code versioning is an essential practice for any modern developer. Git, a distributed version control system, allows multiple developers to collaborate on the same project without stepping on each other's toes. One of Git's most powerful features is the use of branches, which allow developers to work on different features or fixes in isolation from the main project line, known as 'master' or 'main'.

What are Branches?

Branches in Git are mobile pointers that point to commits (repository states). When you create a branch, you are creating a new working environment where you can make changes without affecting the main branch. This is especially useful for implementing new features, bug fixes, and general experimentation.

Creating a New Branch

To create a new branch, the basic command is git branch branch-name. This command creates a new branch based on the branch you are currently on. For example, if you are on the 'main' branch and you run git branch feature-x, a new branch called 'feature-x' will be created from 'main'.

$ git branch feature-x

After creating the branch, you need to switch to it using the git checkout branch-name command. This will make the HEAD (pointer to the active branch) point to the new branch.

$ git checkout feature-x

A faster way to create and switch to a new branch is to use the command git checkout -b branch-name, which does both operations in a single command.

$ git checkout -b feature-x

Experimenting with Branches

With your new branch active, you can start experimenting. All modifications, commits and even new branches created from this branch will be isolated from 'main'. This means you can work freely without the risk of interfering with the main code or other branches under development.

Let's assume you are working on a new feature. You can commit incrementally to your 'feature-x' branch until the feature is complete and tested.

$ git add .
$ git commit -m "Adds initial part of functionality X"

Continue working and making commits as needed. This not only helps you maintain a clear history of functionality development, but also allows you to revert to previous states if something goes wrong.

Merging Branches

Once your functionality is ready and tested, you may want to merge it back into the 'main' branch. To do this, you must first ensure that you are on the branch that will receive the changes (usually 'main') and then use the command git merge branch-name.

$ git checkout main
$ git merge feature-x

This will incorporate the changes from 'feature-x' into 'main'. If there are no conflicts, the merge will be performed automatically. However, if there are conflicts, Git will ask you to resolve them before completing the merge.

Resolving Conflicts

Conflicts occur when two branches change the same part of a file in different ways. Git is not able to automatically decide which version to keep, so it marks the file as conflicting and stops the merge process. You will need to open the conflicting files, resolve the differences manually, and then make a new commit with the resolved changes.

$ git add resolved-file.txt
$ git commit -m "Resolves merge conflicts"

Deleting Branches

After the feature is merged and everything is working as expected, the 'feature-x' branch may no longer be needed. To delete it, use the command git branch -d branch-name.

$ git branch -d feature-x

It is important to note that Git will not allow you to delete a branch that has not been merged unless you force the deletion with git branch -D branch-name. This is a safety measure to prevent loss of work.

Conclusion

Branches are a powerful tool in Git that allow developers to work in parallel, experiment, and make changes without affecting the main code. They facilitate collaboration and help keep the repository organized. With practice, using branches will become a natural part of your development workflow.

Remember that success when working with branches depends on good communication with your team and following good versioning practices, such as frequent commits and merges, in addition to maintainingyour branches updated with the changes from 'main'.

With these skills, you'll be well equipped to contribute effectively to collaborative projects using Git and GitHub, and you'll be able to further explore the advanced functionality these tools offer.

Now answer the exercise about the content:

Which command is used to create a new branch in Git and immediately switch to it?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Merging: integrating branches 16

Next page of the Free Ebook:

Merging: integrating branches

Estimated reading time: 4 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks