By Simon Clark, Jing Yang and Margot Patrick

After a century and a half trying to steer a neutral course through the messy geopolitics of east versus west, HSBC Holdings PLC has signaled its support for China over the future of Hong Kong.

At a plastic table outside of a Hong Kong metro station, the bank's Asia chief, Peter Wong, recently signed a petition backing a security law China is preparing to impose on the city. Opponents say the law would undo the territory's autonomy, threatening its Western-style pillars of free speech and independent courts. Beijing says the law is necessary to restore order after a year of protests.

The support for China marked an unusual public foray into the politics of Hong Kong. Generally, the bank has stuck to a well-worn playbook for China and other sensitive countries such as Saudi Arabia, according to people familiar with the matter: say as little as possible publicly, while privately assuring governments of the bank's interest in their economic success.

The bank took action after being pilloried by pro-Beijing figures and the state media for not getting behind the law and after having to apologize to Beijing officials last year over information it provided in a U.S. criminal case against a prominent Chinese company, Huawei Technologies Co.

HSBC makes most of its profit in Hong Kong and mainland China, underlining the importance of staying in Beijing's good books. The bank endorsed the legislation by using a social-media account aimed at a Chinese audience.

On Wednesday evening, HSBC posted photographs of Mr. Wong signing the petition on the Chinese social-media account, which isn't easily accessible by the public. An accompanying statement from the bank said it supports laws that will let Hong Kong recover and rebuild its economy while maintaining the principle of one country, two systems. Beijing agreed to keep Hong Kong largely autonomous for 50 years when Britain handed it back in 1997.

Mr. Wong's signing was the culmination of more than a year of internal debate over how to address China's growing influence in Hong Kong and the trade dispute with the U.S. It also showed the bank's commitment to President Xi Jinping's China as it retreats in Europe and the U.S.

"To sign something so politically charged really runs counter to a very deeply entrenched code of behavior," said David Kynaston, co-author of a book on the history of HSBC.

The sprawling global bank has almost as many employees as JPMorgan Chase & Co. and a balance sheet to match. With roots in Hong Kong and Shanghai, it was perfectly positioned to capitalize on the rise of China in the world economy.

The signing prompted criticism from politicians, customers and employees and puts HSBC at odds with Britain, where it is regulated, and the U.S. and other countries that say the law violates Hong Kong's autonomy.

Some called for existing customers to move their accounts to other banks. Pro-democracy activist Joshua Wong on Twitter said HSBC is a "vivid example demonstrating how China will use the national security law as new leverage for more political influence," over Hong Kong's foreign business community.

China's implementation of the new national security law in Hong Kong is "absolutely the right thing to do," said Endong Zhai, a Chinese banker at DC Advisory based in London. He said that all countries protect their security, including the U.S. and the U.K., and that President Trump's criticism of China over the law was overdone.

Privately, Mr. Wong's endorsement has provoked the ire of some employees for taking a stance on a political issue.

"HSBC accompanied generations of Hongkongers. But once there's pressure, they betrayed Hong Kong. This is shameful," said one employee, who asked not to be named for fear of retribution.

The bank's policies ask employees to "avoid topics that could reasonably be considered offensive or inflammatory."

HSBC's relationship with China stretches back to its founding in 1865 by Scottish businessman Thomas Sutherland when he was running the Asian operations of a British shipping company.

The bank lent money to tea and silk traders, and opened branches across Asia to provide financing for new trade flows and railroads.

HSBC's forced exit from China after the Communist party took over in 1949 sent it into other parts of the world for growth. It competed with American banks such as JPMorgan and Citigroup Inc. in helping multinational companies manage cash and trade in new markets.

In the lead-up to Hong Kong's handover back to the Chinese, HSBC diversified further from Asia, buying the U.K.'s Midland Bank in 1992 and moving its headquarters to London the following year.

Yet Hong Kong continued to be a bonanza market for HSBC. Its business of lending to importers and exporters boomed along with China's manufacturing sector in the 1990s and 2000s.

The global financial crisis and the European sovereign-debt crisis undermined the idea that the West was a safer place to do business than China. HSBC ran afoul of U.S. authorities for weak controls in its far-flung operations.

The bank pivoted back to Asia, coinciding with an opening up of China's financial markets and a boom in the Pearl River Delta, the area that knits Hong Kong and southern China's wealthy entrepreneurial heartland.

In 2018, the bank's aspirations in China hit a bump when U.S. prosecutors used information provided by the bank to help build a fraud case against Meng Wanzhou, the finance chief of Chinese telecommunications equipment company Huawei. The U.S. had Ms. Meng arrested in Canada and is seeking her extradition to face charges she misled HSBC and other banks about ties between Huawei and an affiliated company doing business in Iran.

HSBC flagged suspicious transactions involving Huawei to U.S. prosecutors in 2016, The Wall Street Journal previously reported, while the bank was being monitored by the Justice Department as part of a 2012 settlement over sanctions breaches and money laundering. In a Canadian court this year, Huawei lawyers said the DOJ's grip on the bank gave it a motive to present Huawei as the mastermind of the bank's sanctions violations.

When HSBC's involvement in the case became public, bank executives made private apologies to Chinese officials, including one to the Chinese ambassador in London by then-Chief Executive John Flint, according to people familiar with the events.

While efforts were being made to smooth things over, a trade war between China and the U.S. was heating up.

Fearing the bank might have to pick a side, HSBC executives considered how its reputation with customers and employees would fare if it spoke out in favor of China, and crunched numbers to simulate outcomes for its financial future, according to people familiar with the planning exercise.

The pressure intensified in June 2019 as Hong Kongers took to the streets to demonstrate against an extradition bill that would allow people to be sent to China for trial. The bill was set aside, but not formally withdrawn, and the protests morphed into a broader ideological battle against encroaching authoritarianism by China.

At a march Jan. 1, protesters vandalized HSBC branches and sprayed red paint on Stephen and Stitt, a pair of bronze lions stationed outside its Hong Kong headquarters since 1935 and which appear on some of the city's bank notes.

In July, Chinese state media called for the bank to be put on an "unreliable entities" list that would cause it to lose Chinese business. That alerted the bank that it might have to do more, according to people familiar with the matter.

When journalists asked in August about the bank's standing with Beijing, Mr. Tucker said the bank is "totally aligned with the Chinese view of growth and economic prosperity."

But pro-Beijing critics called for more than support of China's economic policies. In a May 29 Facebook post, former Hong Kong Chief Executive Leung Chun-ying called on HSBC directly to express support for the new law or risk losing business.

"HSBC's China business can be replaced by banks from China or other countries overnight," he said.

Write to Simon Clark at simon.clark@wsj.com, Jing Yang at Jing.Yang@wsj.com and Margot Patrick at margot.patrick@wsj.com