The transition from Sui to Tang (隋末唐初) refers to a period in which the Chinese dynasty Sui Dynasty disintegrated into a number of short-lived states, some ruled by former Sui officials and generals and some by agrarian rebel leaders, and then those states were consolidated into Tang Dynasty, founded by the former Sui general Emperor Gaozu of Tang (Li Yuan). The period roughly started from 613, when Emperor Yang of Sui launched his first of three campaigns against Goguryeo, leading to a number of desertions from the army and the start of agrarian rebellions against Sui, to 628, when Emperor Gaozu's son Emperor Taizong of Tang (Li Shimin) destroyed the Liang state ruled by the agrarian rebel ruler Liang Shidu and reunified China.

As of 611, Sui Dynasty had just enjoyed more than two decades of peace and prosperity, as China had been united under it since it destroyed Chen Dynasty in 589, and aside from border conflicts with Eastern Tujue (which had since become a vassal state under its Qimin Khan Ashina Rangan) and Goguryeo, and one brief internal conflict between Emperor Yang of Sui, who became emperor in 604, and his brother Yang Liang the Prince of Han, the realm had not seen war. When Goguryeo's king Gao Yuan (King Yeong-yang) refused to pay homage to Emperor Yang in 610, Emperor Yang decided to plan a campaign to conquer it, and both he and the people believed that the conquest would be easy.

The logistics of staging the attack on Goguryeo, however, took much human and other tolls, as the building of a fleet and, particularly more so, the shipping of food and other supplies to the base of operations, Zhuo Commandery (涿郡, roughly modern Beijing), caused major disruptions in the farming cycle and major deaths in those conscripted to ship the supplies to Zhuo Commandery. In response, in 611, in northern China, those who were unwilling to be conscripted began to rise as agrarian rebels, led by leaders such as Wang Bo (王薄) and Liu Badao (劉霸道), and while Emperor Yang initially did not consider these rebels serious threats, the local governmental militias were unable to quell them.

Despite this, Emperor Yang launched his first campaign against Goguryeo in 612, crossing the Liao River into Goguryeo territory in spring 612. Emperor Yang personally led part of the army to put the important city Liaodong (遼東, in modern Liaoyang, Liaoning) under siege, while he sent the generals Yuwen Shu and Yu Zhongwen (于仲文) to lead the rest of the army deep into Goguryeo territory, heading toward the Goguryeo capital Pyongyang, joined by the fleet commanded by the general Lai Hu'er (來護兒). Emperor Yang, however, was never able to capture Liaodong, while Yuwen and Yu, advancing nearly to Pyongyang, were crushed by the Goguryeo general Eulji Mundeok at the Battle of Salsu and forced to withdraw with heavy losses. By fall 612, Emperor Yang was forced to terminate the campaign and withdraw as well, with only minor territorial gains. About 300,000 men had been lost in the campaign.

Not deterred, Emperor Yang launched a second campaign against Goguryeo in 613, even though the agrarian rebellions were becoming more numerous and serious. He again headed for Liaodong himself and put it under siege, while sending Yuwen and Yang Yichen toward Pyongyang. While he was sieging Liaodong, however, the general Yang Xuangan, in charge of logistics near the Sui eastern capital Luoyang, rose in rebellion, attacking Luoyang. When Emperor Yang heard the news, he withdrew his forces and sent Yuwen and Qutu Tong (屈突通) back to Luoyang ahead of himself, and Yuwen and Qutu joined with Fan Zigai (樊子蓋) and Wei Wensheng (衛文昇), the commanders of forces that Emperor Yang had left at Luoyang and Chang'an respectively, to defeat Yang Xuangan. Emperor Yang carried out heavy-handed reprisals against actual or perceived adherents of Yang Xuangan, but such actions did not deter further rebellions.

Despite this, Emperor Yang launched a third campaign against Goguryeo in 614. As Lai reached the Yalu River, however, Goguryeo submitted, sending Yang Xuangan's confederate Husi Zheng (斛斯政), who had fled to Goguryeo, back to Sui as a sign of submission. Emperor Yang terminated the campaign, but when he again summoned Gao Yuan to pay homage to him, Gao Yuan ignored his summons. Emperor Yang began to plan a fourth campaign, which, however, he was never able to launch.

Meanwhile, in fall 615, when Emperor Yang was visiting the northern city of Yanmen (雁門, in modern Xinzhou, Shanxi), Qimin Khan's son and successor Shibi Khan Ashina Duojishi, who had been displeased by Emperor Yang's attempt to keep Eastern Tujue submissive by dividing them, launched a surprise attack on Yanmen, putting it under siege. Sui forces, largely still loyal to Emperor Yang, rushed to Yanmen to try to lift the siege, and Emperor Yang himself promised great rewards for those who came to his aid. After the siege was soon lifted, however, he reneged on the promises, causing resentment among the military.

Map of the situation in northern China during the transition from the Sui to the Tang, with the main contenders for the throne and the main military operations

Despite (or perhaps because of) increasing agrarian rebel activities in northern China, however, Emperor Yang did not return to Chang'an or stay at Luoyang, but went to Jiangdu (江都, in modern Yangzhou, Jiangsu) in fall 616. With his departure from Luoyang, the rebels near Luoyang coalesced under the leadership of Yang Xuan'gan's former strategist Li Mi, who was proclaimed the Duke of Wei and considered the presumptive eventual emperor by most rebel leaders throughout northern China. Li, however, was not able to capture Luoyang and never claimed imperial title.

Meanwhile, Yang Yichen made an attempt to destroy the rebels north of the Yellow River, but while he enjoyed some successes, Emperor Yang and his prime minister Yu Shiji, fearing Yang Yichen's military strengths, recalled him under guise of a promotion, allowing the rebel activities north of the Yellow River to reinvigorate themselves and become difficult to control, under the leadership of Dou Jiande.

By 617, a number of other major rebel leaders also began to control significant portions of territory. These include:

Li Gui, formerly a Sui official, occupying the modern central and western Gansu region, declaring himself the Prince of Liang.

Li Yuan, formerly a Sui official (and Emperor Yang's cousin), occupying the modern central Shanxi region, announcing that he wanted to make Emperor Yang's grandson Yang You the Prince of Dai, then at Chang'an, emperor.

Xue Ju, agrarian rebel, occupying the modern eastern Gansu and western Shaanxi region, declaring himself the Hegemonic Prince of Western Qin.

Zhu Can, formerly a Sui official, roving with his army in the modern southern Henan and southeastern Shaanxi region, first declaring himself the Prince of Jialuolou, and then the Emperor of Chu.

Several of these rebel leaders -- including Li Yuan, Liu Wuzhou, Liang Shidu, Dou Jiande, and Gao Kaidao -- formally submitted to Ashina Duojishi and received Eastern Tujue military aid, with Ashina Duojishi's strategy apparently to keep China divided. In winter 617, Li Yuan captured Chang'an, declaring Yang You emperor (as Emperor Gong), while honoring Emperor Yang as Taishang Huang (retired emperor); these declarations were not recognized by most of Sui territory, which still recognized Emperor Yang as emperor. Li himself became regent with the title of Prince of Tang.

Meanwhile, Emperor Yang, while realizing that the empire was in turmoil, felt secure under the protection of the elite Xiaoguo Army (驍果) at Jiangdu, and while he sent his general Wang Shichong to Luoyang to try to defend Luoyang against Li Mi's attacks, appeared to do little to quell the rebellions otherwise. Not wanting to return to northern China, he considered officially moving the capital to Danyang (丹楊, in modern Nanjing, Jiangsu), south of the Yangtze River). Meanwhile, the Xiaoguo Army soldiers, who were predominantly northerners and missed their homes, began to desert, and this met with heavy-handed punishment from Emperor Yang. In fear, the remaining Xiaoguo Army officers formed a plot, supporting the general Yuwen Huaji the Duke of Xu (Yuwen Shu's son) as their leader. In spring 618, they carried out a coup and killed Emperor Yang. Yuwen declared Emperor Yang's nephew Yang Hao the Prince of Qin emperor, but keeping power himself as regent. He abandoned Jiangdu and headed back north, commanding the Xiaoguo Army.

Soon, news of Emperor Yang's death reached various parts of the empire. At Chang'an, Li Yuan responded by having Emperor Gong yield the throne to him, establishing Tang Dynasty as its Emperor Gaozu. At Luoyang, seven of the leading officials declared another grandson of Emperor Yang, Yang Tong the Prince of Yue, emperor, and Yang Tong was recognized as Sui's emperor by most of the commanderies that still recognized Sui sovereignty. With both the Sui administration at Luoyang and Li Mi fearing Yuwen's northward advancement, they formed a temporary alliance in which Li Mi recognized Yang Tong as his sovereign. After Li Mi repelled Yuwen, however, Wang, who opposed the alliance, seized power from the other officials and became regent, and the alliance with Li Mi was broken. Later that year, in a surprise attack, Wang defeated Li Mi, forcing Li Mi to flee to Tang. Li Mi was later killed by Tang forces when he tried to reestablish his own independence.

Meanwhile, after Xue Ju died in early 618 and was succeeded by his son Xue Rengao, the Tang general Li Shimin the Prince of Qin (Emperor Gaozu's son) defeated and killed Xue Rengao, annexing his Qin state into Tang. At the same time, Dou Jiande further consolidated his holdings north of the Yellow River and killed Yuwen (who had poisoned Yang Hao and declared himself Emperor of Xu), but was unable to get Luo Yi to submit to him, and Luo subsequently submitted to Tang. Around the same time, Zhu, facing heavy resistance from the populace against his cruelty, vacillated between submitting to Yang Tong's Sui regime and Tang, eventually surrendering to Sui.

In summer 619, Wang had Yang Tong yield the throne to him, ending Sui and establishing a new state of Zheng as its emperor.

Around the same time, Li Gui's official An Xinggui (安興貴) captured Li Gui in a coup and surrendered the state to Tang. Tang, however, was facing a threat on a different front, as Liu Wuzhou made a major attack south, taking over much of modern Shanxi, which Tang had controlled, and appearing poised to further attack Tang's capital Chang'an. Around the same time, the lower Yangtze region, which had been in a state of confusion ever since Emperor Yang's death, was coalescing around three different competing figures -- the former Sui official Shen Faxing, who declared himself the Prince of Liang and controlled much of the territory south of the Yangtze; the rebel leader Li Zitong, who controlled Jiangdu and the surrounding regions, declaring himself the Emperor of Wu; and Du Fuwei, who submitted to Tang and was created the Prince of Wu.

In late 619, Tang forces, commanded by Li Shimin, began a counterattack against Liu Wuzhou. By summer 620, Li Shimin had defeated Liu, who abandoned his territory and fled to Eastern Tujue. His Dingyang state was integrated into Tang.

After defeating Dingyang, Li Shimin had his sights set on Zheng. He advanced to the Zheng capital Luoyang and put it under siege. Many Zheng cities surrendered to Tang, forcing Wang to seek aid from Dou Jiande's Xia state. Dou, reasoning that if Tang destroyed Zheng, his own Xia would be cornered, agreed, and he advanced south toward Luoyang, seeking to lift the siege. Around the same time, Du (now using the name Li Fuwei, having been granted the imperial surname of Li by Emperor Gaozu) defeated Li Zitong, who in turn defeated Shen, forcing him to commit suicide. Li Zitong now had Shen's former territory, while Li Zitong's former territory was held by Li Fuwei in Tang's name.

In spring 621, with Dou approaching, Li Shimin advanced east to the important Hulao Pass and held position there. When Dou engaged him, he defeated Dou and captured him. In fear, Wang surrendered. Emperor Gaozu executed Dou while exiling Wang (although Wang was subsequently killed by the Tang general Dugu Xiude (獨孤修德), whose father had been executed by Wang). Wang's Zheng state and Dou's Xia state were annexed by Tang, although former Xia territory soon rose under the leadership of Dou's general Liu Heita, who declared himself the Prince of Handong, and modern Shandong, which had been controlled by the agrarian leader Xu Yuanlang but had successively submitted to Zheng and then to Tang, rose as well under Xu, who declared himself the Prince of Lu.

Also in 621, Emperor Gaozu's nephew Li Xiaogong the Prince of Zhao Commandery attacked Xiao Xian's Liang state, putting the Liang capital Jiangling under siege. Xiao, not realizing that relief forces were approaching, surrendered, and most of his state was annexed by Tang, while some of Xiao's army submitted to Lin Shihong. Around the same time, Li Fuwei defeated Li Zitong, forcing Li Zitong's surrender, and Li Zitong's Wu state was also annexed by Tang.

In spring 622, Li Shimin defeated Liu Heita, forcing Liu to flee to Eastern Tujue, but Liu returned later that year with Eastern Tujue aid, reoccupying the former Xia territory. In winter 622, Li Shimin's older brother Li Jiancheng the Crown Prince defeated Liu again, and in spring 623, Liu, in flight, was betrayed by his official Zhuge Dewei (諸葛德威) and executed by Li Jiancheng. Earlier, Lin had died, and his Chu state dissipated, with the cities gradually submitting to Tang, and soon after Liu's death, Xu, who had repeatedly been defeated by Tang forces, was also killed in flight. By this point, other than Liang Shidu and Gao Kaidao in the extreme north, China was largely reunited, if somewhat nominally, under Tang rule.

In fall 623, however, with Li Fuwei at Chang'an, Li Fuwei's lieutenant Fu Gongshi rebelled at Danyang, declaring himself the Emperor of Song and controlling the territory formerly under Li Fuwei's control. By 624, Li Xiaogong had defeated and killed Fu, reintegrating Song territory into Tang, while Gao, faced with a coup led by his subordinate Zhang Jinshu (張金樹), committed suicide, and his Yan state was also integrated into Tang.

Meanwhile, Liang Shidu, protected by Eastern Tujue aid, was holding up against Tang attacks, and Tang itself was continually harassed by Eastern Tujue raids. After Li Shimin ambushed and killed Li Jiancheng and another brother, Li Yuanji the Prince of Qi in 626 and effectively forced Emperor Gaozu to yield the throne to him (as Emperor Taizong), however, Tang began to turn the situation around. By 628, with Eastern Tujue in internal turmoil due to disagreements between the Jiali Khan Ashina Duobi (Ashina Duojishi's younger brother) and the subordinate Tuli Khan, Ashina Shibobi (阿史那什鉢苾, Ashina Duojishi's son), it was no longer able to protect Liang Shidu, and under Tang siege, Liang Shidu's cousin Liang Luoren (梁洛仁) killed Liang Shidu and surrendered. All of China was now under the rule of Emperor Taizong.