The heavenly stems and earthly branches are calendrical units, whose origin is uncertain as they may have stemmed from religious patterns dating back to the Shang oracle bones of the second millennium BC. They were initially used to designate just days and only later, double hours, months and years.
They are divided into yang and yin and assigned specific polarities, phases and spatial directions.

Both stems and branches alternate between yang and yin polarity. There are two branches for each element separated by an earth branch. Therefore, earth is represented four times among the branches. The Earth mentioned here does not refer to the counterpart of Heaven, but to the earth as soil (one of the Five Phases). The four branches, chou 丑, chen 辰, wei 未, and xu 戌, are associated with the “four corners” representing the inter-cardinal directions. These four transitional nodes from one cardinal direction to another were seen as the moment when the qi of earth returns to the centre before transforming into the next element/season.
Each year (and month, day and double hour) is designated using a combination of one stem and one branch. Each temporal sequence starts with the first yang stem jia 甲 and the first yang branch zi 子 and continues with the following yin stem yi 乙 and yin branch chou 丑 etc. After 10 steps the sequence of the stems will start again from jia 甲 and, twelve steps later, the sequence of the branches will start again from zi 子. Sixty steps exhaust all possible combinations of stems and branches. Therefore, every sixty years, every sixty months, every sixty days and every sixty double hours, the sequence begins again from jia/zi 甲子.
This sexagenary cycle can be imagined as a mechanism made of four concentric clocks ticking at different speeds. When the year-clock has completed one step, the month-clock has gone through twelve steps; when the day-clock has completed one step, the hour-clock has gone through twelve steps (the twelve double hours). After a full cycle of sixty months/hours the year/dayclock has moved by five steps (five years/days).