5 ms·
You can also do a per-directory _global_ git configuration, e.g. in .gitconfig, you say: [user] name = Me Myself email = personal@example.
by sirn 7y ago
You can also do a per-directory _global_ git configuration, e.g.
in .gitconfig, you say:
[user]
name = Me Myself
email = personal@example.com
signingkey = D34DB44F
[includeIf "gitdir:~/src/github.com/work_org/"]
path = ~/.gitconfig_work
Then in ~/.gitconfig_work:
[user]
name = Me Myself
email = work@example.com
signingkey = D34DC0D4
[core]
sshCommand = ssh -i ~/.ssh/work_ed25519
I like this way better, because I don't need to remember to specify per-project config, as long I put them in the right directory :-)
- bobwaycott 7y agoThis is fantastic. I had no idea this was possible.
- deleted 7y ago[deleted]
- Copenjin 7y agoExactly, have a similar setup, I would recommend against using the approach suggested in the post if you want to be 100% sure of always using the right account.
- lloeki 7y agoThe article guarantees that by: - requiring a user to be set - not having one filled in the global settings The upside is that even if the folder is moved, the user config still applies. The downside is that you have to set it per repository (but only once). There are pros and cons, but I don't see one being any better or worse than the other in absolute terms.
- rkangel 7y agoThis setup does ensure this. These are the two relevant commands: git config --global --unset user.email (etc.) git config --global user.useConfigOnly true This means that there is no default email set, and that git will error if you try and commit, because there is no email set. I do a lighter weight version of this article by just doing the above. Then when I do the first commit in a repo, I get that error and I do the following command: git config --local user.email me@myworkemail.com (or the my personal email as appropriate). Then I have per repo config that persists unless I blow the repo away completely.
- DrKabab 7y agoI came up with a similar setup recently, and it works great. Also lets you keep the main gitconfig in version control with my other dotfiles
- WhyNotHugo 7y agoWhy do you need to use different name/key/email between work and personal project?
- Terr_ 7y agoWell, the e-mail part is pretty straightforward, since many people will have a personal e-mail and a separate e-mail managed by their workplace IT. If some current (or future) coworker has a question about the code, I don't want them to necessarily e-mail me on my home account. I might not be checking it during work hours, or I might not even be with the company anymore. Similarly, if someone actually wants to contact my e-mail for a hobby-project, I don't want them to send it to my work-account, which over time I might no longer have access to anyway.
- lloeki 7y agoThis also applies to e.g CI systems, that are able to send build success/fails emails to the emails in the commits instead of the default GitHub/GitLab email.
- WhyNotHugo 7y agoActually, both GitLab and GitHub allow configuring different email addresses per team. The notifications don't go to the email in the git commit.
- moooo99 7y ago> Similarly, if someone actually wants to contact my e-mail for a hobby-project Also, some people prefer to not disclose their employer on GitHub hobby projects (or at all on the web). Simiarly, your employer doesn't necessarily need to know about every hobby project you start (as long as you're not violating work-contract clauses).
- DrKabab 7y agoIn many workplaces, you're required to use the company provided email address with git. And the name some people use at work isn't the same as what they use in their personal projects. (For ex, I use my full name at work but a shorter one for personal stuff)
- arialdomartini 7y agoAnother (possibly simpler) approach is to set project specific settings in the `.git/config` file, right in the project directory. As for the OP's example, in: `~/src/github.com/work_org/.git/config` without modifying the root `~/.gitconfig` file.
- fishywang 7y agoWhile this works great if you are using your personal computer to do work. But if you use your employer issued computer to do personal hobby project, then please be very careful. In 90%+ of the cases your contract with your employer have the claus saying (please review your contract to clarify) that they own copyright of all your work while you are under the contract even if it's outside of working hours or on the weekend, with a few exceptions, and using employer equipment (employer issued computer) nullify one of the biggest exceptions. IANAL but I would strongly suggest everyone to only use their employer issued computer to do work related work and move all their personal, hobby projects into their own personal computer, unless you don't care your employer owning the copyright of your personal hobby projects.
- hinkley 7y agoAdvice I’ve gotten: not on company property, or with company resources (equipment, networking, technology). You want to work on your personal project before going home, bring your laptop to work with you, go to a coffee shop (off-property) and work there. That said, when I’m doing PRs for open source, it gets a bit awkward to use my work ID. If I have permission to contribute to dependencies on work time (which has definitely not always been the case), then something like this could be useful.
- fishywang 7y agoMy personal take on open source contribution: If this is related to your work, you are getting paid for working on the contribution by your employer, then your employer should get the credit, thus you should use your work email/id. Otherwise, treat it as your personal hobby project and don't use employer resource to work on it.
- balfirevic 7y agoOnly "should" here is whatever arrangement you agreed to with your employer.
- hinkley 7y ago