Branches serve as vital constructs within the Git version control system. Functioning akin to divergent timelines within a project, branches facilitate the creation of distinct contexts. These contexts provide the flexibility to experiment with novel concepts and simultaneously engage with multiple ideas. Modifications made within one branch remain isolated from others until a deliberate merging of branches is executed, ensuring the autonomy and integrity of each branch's development trajectory.
Git Branching Flow diagram
Master branch
Within the realm of Git, our work perpetually unfolds within a designated branch. By default, the branch is commonly referred to as "master" or "main." However, Git bestows the capability to redefine this default nomenclature according to our preferences. This adjustment empowers us to adopt a branch name that resonates more harmoniously with our project's essence and workflow.
HEAD
In the Git ecosystem, the term "HEAD" designates a pointer that intricately directs us to the present location within our repository. Its principal function involves denoting a specific branch reference, thereby indicating the precise state of the repository we are interacting with. Notably, the HEAD pointer consistently aligns itself with the most recent commit on the designated branch, which is often referred to as the "master" or "main" branch.
Git Branch Command
- To view the existing branch you are currently on, along with the indication of the active branch use the
*
symbol, you can use the following command:
>> git branch
If you are using Git versions after the transition from "master" to "main" as the default branch name, you will see the default branch as "main" instead of "master".
- If you want to list all branches (both local and remote), you can use:
>> git branch -a
This command will provide you with a list of branches, with an asterisk *
next to the branch you are currently on.
- To create a new branch based on the current HEAD without changing your current position (i.e., the branch you're on), you can use the following Git command:
>> git branch <new-branch-name>
Replace new-branch-name
with the desired name for your new branch. This command will create the new branch while keeping you on the current branch.For example, if you are currently on the "main" branch and you want to create a new branch named "feature-xyz" based on the current HEAD.
>> git branch new-feature
After executing this command, you will still be on the "main" branch, but the "new-feature" branch will have been created based on the same commit that the "main" branch is pointing to.
>> git branch bug-fix
Note: We have created a new branch but still working on the master branch
- To switch between branches in Git, you can use the
git checkout
command followed by the name of the branch you want to switch to. Here's the command
>> git checkout <branch-name>
Replace branch-name
with the name of the branch you want to switch to. This command will change your working directory to the specified branch, and you'll be able to continue your work on that branch.
>> git checkout bug-fix
If you've created a new branch using git checkout -b
, as shown in the previous response, you'll already be switched to the new branch. If you want to switch to a different branch after creating and being on a new branch, just use the git checkout
command with the desired branch name.
>> git checkout -n bug-fix
When you create a new branch and make commits on that branch, those commits are isolated to that branch. They won't affect other branches, including the branch you created from. This is one of the core features of Git that enables parallel development and experimentation.
If you create a new branch from another branch, the new branch will include all the commits that are present in the branch it was created from. This forms a linear history of commits that are shared between the two branches up to the point of branching. From that point forward, any commits made on the new branch will not impact the original branch or any other branch.
For example, let's say you have the following commit history on the "main" branch:
If you create a new branch "feature" from the "main" branch at commit "C", the initial commit history of the "feature" branch will be:
Commits A, B, and C are shared between the two branches, but any new commits you make on the "feature" branch, such as commit E, will only exist on that branch.
Remember that Git branches are essentially lightweight pointers to specific commits, allowing you to experiment and develop independently while maintaining a clear history of changes.
- You can use the
git branch
command with the-v
flag to view more information about each branch. This includes the last commit on each branch and the commit message associated with it. Here's the command:
>> git branch -v
This will display a list of branches along with the abbreviated commit hash and the commit message of the most recent commit on each branch. It's a useful way to quickly see what work has been done on each branch without switching between them.
If you want to see more detailed information about a specific branch, you can use the git log
command. For example:
git log bug-fix
This will display a detailed commit history for that branch.