Today, we’re going to talk about an essential concept for using Git and GitHub: branches. For this post, we’ll assume you are already familiar with the concepts of remote repository, local repository, stage, and commit; that you have already installed Git on your machine and created an account on GitHub. If you're not familiar with these concepts, check out the introductory post about Git in the following link (https://dev.to/ledscolatina/initial-introduction-to-git-and-github-4apc).
What are Branches?
A branch is essentially an independent environment where your commits are organized. Simplified, you can think of branches as parallel timelines where changes are made without interfering with others. Imagine the "multiverse" concept from Spider-Man: each version of the hero has its own timeline, and their actions do not affect the others, but when merged, they can create conflicts. Similarly, branches are separate environments where you can work without affecting the main code.
How do Branches Work?
When a project is created, it typically starts with a branch called master (or main, in the case of a clone), which is where the main code is maintained. If you want to add new features without compromising the existing functional code, the best practice is to create a new branch. This way, you can make commits and tests without affecting the main branch until everything is ready to be integrated (or even discarded, if necessary).
Creating a Branch
Let's imagine that you are starting a project in your local repository and then plan to push it to a remote repository.
When you start the project with git init
, Git automatically creates a main branch for you, called master
(or main
).
git init
From this point, you can add files and make commits as usual. They will be recorded on the master
branch, as if they were steps in a continuous timeline.
When the project is ready to be pushed to the remote repository on GitHub, we will start the step of linking the local repository to the remote one. To do this, you need to have a GitHub account and use some specific commands:
- Create a new repository on your GitHub account.
- Select the "<> Code" button and copy the URL of your remote repository.
- In the git terminal, use the command
git remote add origin [remote repository URL]
to link your local repository to the remote one.
git remote add origin https://github/user/repository_name.git
- To push your files to the remote repository, use a variation of the
git push
command. Since the repository was not cloned and we need to connect it to the remote repository, you need to specify the name of the branch you want to push, as the newly created repository does not yet have any bran ches. The command is:git push origin [branch-name]
.
git push origin master
Navigating Between Branches
Next, let's start a new branch. This allows you to work on a separate branch where your changes won't affect what has already been done on the main branch.
- To create a new branch, use the command
git branch [branch-name]
.
git branch new_branch
- If you want to create and immediately switch to the new branch, use
git checkout -b [branch-name]
.
git checkout -b new_branch
In this way, the files and commits added to this new branch will not be visible on the master
branch, and vice versa.
To switch between branches, simply use the command git checkout [branch-name]
. This is useful when you need to quickly switch between different parts of the project.
Finalizing: Merge
Suppose your modifications were successful, and now you want to integrate the new features into the main branch. For this, a powerful concept called merge comes into play. Want to know how it works? Stay tuned for the next post!
Top comments (1)
Oh my God!! This is soooo important for my work. Thanks for helping me with this!!!