Multi-account git setup

Mar 1, 2026

If you work in software-related roles, it's likely that you can't do your work without git. Collaboration is going to be a very painful experience if you just store source code in a shared drive.

On initial setup, you'll get asked to enter your name and email to be used for a git commit. You can supply a random name or invalid email and it'll be fine, because these info are not used for verification, but rather for git commit metadata.

The fun starts when you also work for a company, where you are expected to use $companyEmail for company-related git operations. You can run git log (in a terminal, on your repo) to check which email is attached to your commits. You'd be surprised, just from random checks from my friends' repos, I've seen everything from $companyEmail, $studentEmail, $machineHostname. This means if you don't want your personal email to get exposed, you'll have to set your git email to [email protected] (this is for github, but other git forges - gitlab, codeberg, etc - should have something similar).

The git email itself is used when you view git history on git forges, it'll set a hyperlink back to your profile, assuming all your associated emails are added to your git forge profile.

To use different git name/email for each repo/groups, you can utilize git profiles. From my nix config, it looks like this:

# profiles/github
[user]
	email = "[email protected]"
	name = "Karn Wong"
	signingkey = "ssh-ed25519 xxxxxxxxx [email protected]"

And specify git profile to use per base directory:

includeIf."gitdir:~/Git/" = { path = "profiles/github"; };
includeIf."gitdir:~/Forgejo/" = { path = "profiles/forgejo"; };

As long as you keep your personal and work repos in different base directories, it'll use the correct git name/email.

You can also set git config per repo, but I tend to work on multiple repos so this setup is less work for me.

But this begs the question - how can you be sure that a commit is actually created by the author and not someone impersonating them? Thankfully you can sign git commits, basically you can add gpg or ssh key as a signing key under your git forge settings. If you set up everything correctly, when you view a commit on git forge it should display something like "this commit is verified."


ว่าด้วยเรื่อง git

ถ้าได้เครื่องใหม่มา สิ่งแรกที่ต้องทำก่อนจะ git commit ได้ คือ ต้องใส่ git name และ git email จะเป็นชื่อไม่จริง หรือเมล์ปลอมก็ยังได้ เพราะข้อมูลตรงนี้ไม่ได้เอาไป verify ใดๆ ต่อ แต่มันจะโดนโปะไปใน git commit เพื่อให้รู้ว่า ใคร commit มา

ทีนี้ ก็เข้าใจได้ว่า บางทีก็ต้องใช้ git หลายที่ ทั้งส่วนตัว บริษัท หรืออื่นๆ ซึ่ง ในทางปฏิบัติ สามารถใช้ git profiles เพื่อระบุได้ ว่า ตอนไหนควรใช้ name/email ของบริษัท ตอนไหนเป็น name/email ส่วนตัว

ยกตัวอย่าง งานบริษัท อยู่ ~/Git/work และของส่วนตัว อยู่ ~/Git/personal git profiles ก็ทำมาสองอันแยก แล้วปักให้แต่ละ profile activate โดยอิงจาก repo path บนเครื่อง

เช่น ถ้า repo อยู่ ~/Git/personal/foo มันก็จะใช้ personal git config

ทีนี้ จะดูยังไง ว่า git email ของเราเป็นอันไหน ง่ายสุดไปดูใน git config ในเครื่องตัวเอง แต่ ที่จะสนุกกว่าคือไปดู git log (พิมใน terminal นะ) ใน repo

จากที่ลองสุ่มๆ ดู public repos คนอื่น เจอหมดทั้ง $companyEmail, $studentEmail, $machineHostname ($name-MacBook-Pro.local)

แล้ว git email มีผลตอนไหน คำตอบคือ จะมีผลตอนที่มันขึ้นบนหน้า git forge ui ว่า commit นี้ของใคร แล้วถ้าเมล์ตรงกับ user ใน git forge มันจะ redirect ไปที่ user profile นั้น

เผื่อมีคนสงสัย ว่า ในเมื่อตอนใส่ git name / email มันไม่ได้เช็ค จะดูยังไงว่าอันไหนเจ้าตัว commit มาจริง หรือโดนคนอื่นย้อมแมว ขอนำเสนอนวัตกรรม git commit signing ซึ่งสมัยก่อนต้องใช้ gpg key แต่ตอนนี้ใช้ ssh key ในการ sign commit ได้แล้ว

วิธีคือ ไปทำ ssh key มา แล้วเพิ่ม key เข้าไปใน git profile config (บน git forge, เช่น github) เผื่อผูก key ตัวนี้เข้ากับ profile ตัวเอง ซึ่งถ้า sign commit แล้วเซ็ตทุกอย่างถูกต้อง บนหน้า ui จะขึ้นว่า "this commit is verified" หรืออะไรเทือกๆ นี้

git ใครบอกว่าง่าย (ขอเถียงคนนึงละ)

https://karnwong.me/posts/rss.xml