Git Branches Explained
Learn what a git branch is, why git branches are used, where git branches are stored in the .git folder, and some git branch commands.
Table of Contents 📖
What is a Git Branch?
A git branch can be thought of as a representation of a project at a specific time, or a separate version of a project. Specifically, a branch is a pointer to a commit. Even more specifically, a git branch is a file containing the 40 character SHA-1 checksum of the commit it points to. Branches are stored inside the .git/refs/heads folder. For example, if we go to that directory and then list the files we will see the branch names.
cd .git/refs/heads && ls main
We can also see the 40 character SHA-1 checksum by running cat against one of the branches.
cat main e6cfff74fcd072ef8ea8a69669e871531f95c667
When we create a branch from the main branch, the new branch points to the most recent commit of the main branch. For example, if we create a branch called my-branch from the main branch, my-branch will point to the most recent commit of the main branch.
Commits are added to a branch with git add and git commit. For example, adding a commit to my-branch will move the branch forward from the main branch.
When fixing a bug or adding a new feature, a new branch should be used to represent those changes. When the bug is fixed or feature is implemented, the work is merged into the main project (main branch). Note that there is nothing special about the main branch. It is just a regular branch that git creates by default with git init and most people don't change the name. This is what branching means. We are diverging from the main line of development, or the main branch, to continue to work without altering the main branch.
Why do we use Git Branches?
Git branches should be used because they isolate the development process. For example, say wittcode.com has two new features that we want to add to it. The first is a theme toggler and the second is a global search bar. Each of these features should be encapsulated in their own branch. That way both features can be worked on in parallel without the potential of overwriting eachother's code.
Another reason git branches should be used is they keep the main branch free of bad code. When working on a new feature we could introduce a bug. This is fine if it is on a branch we made separate from the main branch, but if we are committing directly to the main branch then this bug would be present in the main branch.
Working with Git Branches
We can work with git branches with the git branch command. To create a git branch, we use the git branch
git branch my-branch
Here, our branch is called my-branch. This does not switch us to the new branch however, to do that we use the command git checkout
git checkout my-branch Switched to branch 'my-branch'
Git knows what branch we are currently on by a special pointer called HEAD. The HEAD pointer points to the local branch that is currently checked out. So here, when we checkout my-branch the HEAD pointer now points to my-branch.
We can list the branches in a repository with the git branch --list command or by simply typing git branch.
git branch --list
- my-branch main git branch
- my-branch main
Note that the asterisk is next to the branch that we currently have checked out. We can rename a local branch with the git branch -m
git branch -m my-branch-rename
We can delete a git branch with the git branch -d
git branch my-delete-branch git branch -d my-delete-branch Deleted branch my-delete-branch (was e6cfff7).
If we want to live life on the edge, we can force delete a branch with the command git branch -D
git branch force-delete-branch git branch -D force-delete-branch Deleted branch force-delete-branch (was e6cfff7).