In “git”, what is HEAD and Why is it important?
A HEAD, in git, is a reference to the currently checked out branch.
HEAD is important in a case where there is a conflict in merging. In such a case, it helps identify which part of the conflicting file is from the current branch and which is from the branch you are trying to merge with.
For example, we have 2 branches “master” and “leaf”:
- we make a file “mat.txt” in master, which consists of “Today is a good day”. We add and commit.
- we then checkout to leaf and make the same file “mat.txt” in leaf, which consists of “Today is a bad day”, we add and then commit.
- when we merge leaf with master, we will get a conflict error which shows the difference in master being at the “Today is a good day.” line, and the difference in HEAD(being the leaf which we are currently in) being at “Today is a bad day.” line.
- The conflict would not be merged until one or both are edited to be the same and the committed.