In git, a branch is a copy of all the files in your codebase. Each branch has an identifying name and its own set of version or commit history. When you create a new repository, the default branch is called main
(or master
on many older repos). Even if you do not create any additional branches, you'll be performing all git commands on a branch called main
.
On October 1, 2020, the default branch name on GitHub repos was changed from master
to main
. Many older web pages, especially answers on web forums, still use master
since the posts were often written years ago. Wherever you see master
, you can mentally substitute main
.
Going forward, Launch School will use main
as the branch name, but be aware that you may encounter master
elsewhere.
Branching is a powerful mechanism in any SCM tool that allows developers to veer off into a tangential or experimental direction, without affecting the primary codebase.
For example, suppose you want to experiment with a new feature, but this feature requires touching a huge amount of code in your codebase. You're not sure if this feature will work, and you want to play around without it affecting your primary codebase. You can create a new branch to perform this work, and throw it all away if it doesn't work out.
To create a branch, we fork it from another existing branch, likely main
. Then we switch to the newly forked branch and start working inside the new branch. The new experimental branch will contain all the historical changes in the main
branch up to the point of the fork. That is, the two branches do not stay in sync automatically going forward (this is good, because we are experimenting in our new branch). If we decide to discard that experiment, we can simply switch back to the main
branch and delete the experimental branch. On the other hand, if we decide the experiment is a success then we may want to keep the code. If that is the case, we can merge commits from the experimental branch into our main
branch (and then delete the experimental branch, if we want to).
It's important to note that only commits, not staged files, are forked and merged. In git, we only move commits around.
It is important that you understand branches can exist both locally and on remote repositories (we'll get into remote repositories later). You can set up your local branches to track
remote branches, which means they will automatically push code to and pull code from the tracked branch on the remote repository. While tracking a branch is possible, it is not required. We'll talk more about this when we talk about remote repositories.
Branching and merging are somewhat big topics. While we don't use them very much at Launch School, it's important to understand them. Seeing them in action can really help. The following videos will help you to review these topics:
Creating new branches
Merging branches
You may have noticed we didn't go over any commands on branching. For now, we want you to understand the concept of branches in git, and not worry about using them. Git can get complicated pretty quickly, so it's important to understand the concepts first.
If you're comfortable with git branching, try to create a few local branches and make a few changes in the new branch. See how your local file system changes as you switch between branches.