Coming a long way into this course about Git and GitHub, we went through repositories and cloning and branches. I hope we are all familiar with these topics and have these concepts on tips. While working with branches in the past tutorials about What are branches? and creating branches locally in Git, you must have noticed a small thing: the default branch. You may also be thinking – What is a default branch? Is there any way to change the default branch?
Let me start this article by recalling the default branch concept with the following image :
As soon as Git Bash is opened in the local working directory, we are already on the master branch. This happens because the master branch is the default branch by default. This happens in GitHub too as when we arrive on our repository’s home page, we are already on the master branch. You can check that out yourself too!
So, the question here is, can we change the default branch? And what would happen if we change or default branch from master to some other branch that we created? This tutorial will answer these questions with the following headings:
- What is a default branch in Git?
- How to set the default branch on GitHub?
- Effects of changing the default branch in Git.
Default Branch in Git
As I mentioned in the introduction of this tutorial, a default branch is a branch from where we start our work whenever we open Git or GitHub. The default branch in GitHub is considered the base branch. All the pull requests and commits are made against this base branch only unless the user specifies any other branch explicitly. In a project, this base branch (often the master branch) is considered as the production branch which is not involved in any type of active development. For different purposes like development etc., new branches are created and work is done on them. The head pointing to the default branch in Git is also often considered as the last point up to which the software is stable or released to the user.
While we already get a default branch set up called a master branch, GitHub provides the user with an option to change that. While we will learn in the next section how to change the default branch on GitHub, it should be noted that it comes with precautions and should be dealt with attentiveness.
How to Set the Default Branch on GitHub?
The default branch on GitHub can be changed through the following steps:
Open your GitHub account.
Navigate to the repository home page.
Go to Settings.
Navigate to Branches in the left navigation panel.
An option for Default Branch will appear on the side panel.
Select “master” dropdown and then select any other branch in the repository.
Select Update to update the default branch.
A cautionary pop-up will advise you that this action has unintended consequences. These consequences we will see in the next section. For now, confirm the updation of the default branch.
A banner with the confirmation will appear near the header of the page.
Now when we navigate to the repository’s home page, you will notice that the branch dropdown will have the value set to dev instead of master.
This way we can change the default branch in our GitHub repository. Let’s see the consequences of changing our default branch in the next section.
Effects of Changing the Default Branch in Git
As mentioned in the previous sections, changing the default branch in Git changes a few things. As GitHub warned us, it will have some “unintended consequences“. Since changing the branch can create some chaos unnecessarily or unknowingly if I say, it is better we should know them beforehand.
Change of Pull and Merge Request Destination
Changing the default branch changes the base branch which is the destination for all the pull and merge requests. As this branch is now considered as the main stable production branch, the pull will merge the other branch to the default branch. To demonstrate this change do the following:
Create a new branch on GitHub (let say def).
Make some changes in the repository on this branch.
Commit these changes. To explore in detail about creating a new branch and making changes to it, please refer to how to create branches on GitHub?
Once you commit the changes, GitHub will sense the changes and will offer the compare and pull request option on the home page.
Click on the button and you can see the destination branch (base branch) has changed from the master for this merge.
And so all the subsequent merges will take place on this branch only. The user should be very careful in performing these operations.
Absence of Local Master Branch on Cloning
Another effect of changing the default branch is while cloning. If we clone the repository after the default branch has been changed on GitHub, it will reflect on the local system and all the local system commits will be against this branch. To see these effects, first of all, I will delete the local working directory from my system.
Execute the following command to remove the directory from your system:
rm -rf <Repo_Name>
Confirm that the directory has been removed from the system.
Alright. Now we can clone this directory from GitHub again. To know more about the cloning process, visit what is cloning and how is it done.
So, clone the repository by the following command:
git clone <repository_link>
Once the cloning is complete, execute the following command to check all the branches in the repository.
git branch
As seen in the image, we just have the dev branch available locally since it is the default branch. We can bring the master branch to the local system by executing git checkout master command but the default branch will still remain to be def.
This is how you can set up a different default branch and perform your operations on it. Changing the default branch is often useful when the active branch is not the master branch. This way all the operations can be done on the new branch and later be merged with the master branch for release. Although it depends on user to user and teams to teams why they need to change the default branch. It brings some unintended changes to the whole system though and the user needs to take care of it since the complete project depends on the branches and commits happening to them.