Shulchan Aruch YD 214:2
קַבָּלַת הָרַבִּים חָלָה עֲלֵיהֶם וְעַל זַרְעָם; וַאֲפִלּוּ בִּדְבָרִים שֶׁלֹּא קִבְּלוּ עֲלֵיהֶם בְּנֵי הָעִיר בְּהַסְכָּמָה, אֶלָּא שֶׁנּוֹהֲגִין כֵּן מֵעַצְמָם לַעֲשׂוֹת גָּדֵר וּסְיָג לַתּוֹרָה; וְכֵן הַבָּאִים מִחוּץ לָעִיר לָדוּר שָׁם, הֲרֵי הֵם כְּאַנְשֵׁי הָעִיר וְחַיָּבִים לַעֲשׂוֹת כְּתַקָּנָתָן, וְאַף בִּדְבָרִים שֶׁהָיוּ אֲסוּרִים בָּהֶם בְּעִירָם מִפְּנֵי מִנְהָגָם וְאֵין מִנְהַג הָעִיר שֶׁבָּאוּ לָדוּר בָּהּ לֶאֱסֹר, הֻתְּרוּ בָּהֶם אִם אֵין דַּעְתָּם לַחֲזֹר.
Acceptance [of a prohibition] by the populace affects [i.e. is binding on] them and their descendants, even in those things the the people of the city didn't accept with a [formal] agreement, rather they practiced such on their own in order to create a fence and safeguard for the Torah. Similarly, newcomers from outside the city who come to reside there, they are like the [original] residents of the city and are obligated to act according to their [the original residents'] enactment. [Similarly,] even those things which were [originally] forbidden to [the newcomers] in the town from which they came according to their [the newcomers' original] custom, if [those things] are not customarily forbidden in the city to which they have moved, then [those things] are permitted to them, provided their intention is not to return [to their original city - i.e. their move is only temporary].
The Be'er Hetev adds qualifiers, based on Tosafoth and the Rosh, distinguishing binding customs sanctioned by a talmid chakham from nonbinding "inferior" customs, though even in the case of the latter, one may be obligated to avoid contention:
כתקנתן. מיהו היינו דוקא במנהג חשוב שנהגו כן ע''פ ת''ח אבל במנהג גרוע אינו חייב לעשות כתקנתן שלא בפניהם או בפני ת''ח רק בפני ע''ה חייב לנהוג כן כדי שלא יזלזל כ''כ התוספות מיהו כתב הרא''ש דאם יש לחוש למחלוקת אסור בכל ענין שאין לשנות מפני המחלוקת ועיין בא''ח ס''ס תצ''ו נתבאר כל חוץ לתחום אין נותנים עליו חומרי המקום שהלך לשם וע''ל ס''ס פ''ג (ועיין כנה''ג שהביא חלוקי דינים לענין המנהגים מנהג שנעשה לכבוד התורה דוחה אפילו שבות דרבנן מהרי''ק שורש ט' ועיין במשא מלך בדיני המנהגות. ועיין בח''מ (צ''ל בח''י) סי' קכ''ו דשם יבואר אם הבן מחוייב לנהוג חסידות ופרישות שנהג אביו. ושם יבואר דין ק''ק שנחרב ונתפזרו הב''ב אם עדיין עליהם כל חומרות המקום שנחרב ועיין הלק''ט סי' ק''ג ע''ש):