What is the difference between HEAD and master in git?

GitOpen SourceLinuxSoftware & Coding

A branch in Git is a series of interrelated commits. When a repository is initialized in Git, a branch will be created by default. This default branch is called the master.

Multiple branches can be created within a Git repository. When a developer starts working on a new feature of the project, he may create a new feature branch and work in isolation from the master branch. Once the feature is completed changes in that branch will be merged to the master branch. In other words, the master branch will be the main line of work. The master itself is a pointer to the latest commit.

The HEAD is a reference that points to the master. Every time you commit, Git updates both master and the HEAD pointers to point to the last commit by default.

Consider an example −

Let us say developer Mr. A commits some code into the repository and the commit hash is Commit#1. It is important to remember that the master pointer is created when the first commit is performed and this pointer will now point to

The following diagram explains this concept.

We can detach the head from to first commit as shown in the diagram below. The master refers to last commit but we have moved the Head back to the first commit. Moving the head around is easy to do in Git.

Also, when we have multiple branches, HEAD helps to find out which branch are we currently working on. The diagram shown below has two branches first is the master branch another is a feature branch. Now to know which branch we working on just check the HEAD pointer.

raja
Published on 20-Feb-2021 13:19:46
Advertisements