17 ms·
Conditionally setting your gitconfig
- jcoder 5y agoThis is cool. In this specific case, you can also (1) not set the email in .gitconfig, and (2) set an environment variable `EMAIL`. Git will pick it right up. I use direnv for this.
- siddharthgoel88 5y agoHmm, that is just about email. There can be other configs which we want to be different for work and personal. And having config in one file - like .work.gitconfig or .home.gitconfig looks better to me since I can find all the config at one place and do not need to look around all the places. However, that is a personal choice, I understand :-)
- jcoder 5y agoTotally, it’s a great solution. Using the env var is nice for the case where you only need to vary the email. Especially nice if you have multiple orgs to commit under, but the rest of your config is just how you like it for all projects.
- hardwaresofton 5y agodirenv is fantastic -- also good for setting a per-repository githook as well
- LanternLight83 5y agoI was hoping for a way to eg. set diff settings based on installed programs, but seems to have reached a compromise in that including a file which doesn't exist doesn't cause any visible errors, so I can just use a systemd-esque system where I include all the files and create them (as symlinks) when appropriate.
- mplanchard 5y agoLearned about this recently. Very useful for setting up commits to use different emails and commit signing to use different GPG keys depending on the context
- mmcallister 5y agoThis is neat, but I think it's better to use your work provided device(s)/laptop for work, and your personal devices for personal things. Obviously check your contract, and IANAL but people should be cognizant of the fact that your employer _may_ be able to retain copyright on personal projects if you use a work device (then again some contracts may even try to claim that they retain copyright for work done in personal time on personal devices)
- vinceguidry 5y agoI will never again accept a job where I must use a work-provided machine. I spend 8+ hours a day at work. Being forced to spend that time in some crappy, bloated, locked-down OS is 100% a quality of life issue. If I can provision and manage the machine myself, fine. I'll even purchase the hardware. Prefer it that way.
- gravypod 5y agoYou can also find a company that just buys you a computer you would like and gives you root on your machine, right? I've even had people build their own PCs when starting at startups because they wanted specific specs.
- koolba 5y agoHaving a machine that is actually usable is orthogonal to having one dedicated to a job. Computers are dirt cheap, so have a separate one and it’s easy to keep your work products separated. As an added bonus, it gives you one more place to run bloated^Wmandatory Electron apps.
- maccard 5y ago> Computers are dirt cheap, so have a separate one and it’s easy to keep your work products separated. Not everyone has the same "dirt cheap" requirements for hardware for work; I work in games where a medium-range GPU is a requirement, and compiling large projects requires a much higer spec CPU (e.g. everyone on our team has at least a 3970x, which is ~$2000, and comes with a motherboard of $800). There's also space considerations - having two desktop machines works if you live in the US where houses are enormous; meanwhile I live in a 500sqft apartment with a 55 inch wide desk in my second bedroom - putting a second tower, plus extra power plus a KVM plus extra networking is a no-go.
- FrancoisBosun 5y agoI set this up differently: my user gitconfig has my personal email. In my work monorepo, I have a local gitconfig where I set/override the email and GPG settings. Still, this is a neat thing too.
- rswail 5y agoThis allows you to do it from one .gitconfig so that it automagically changes when you're in a work repo as opposed to a personal one. My source lives in a directory called ~/src, bunch of different repos. My work source repos live in ~/src/company so this way, I point the global git config there when in a repo under that directory.
- scns 5y agoThis is great thanks for sharing. My problem i can fix with this is the different forwarding aliases on GitLab and GitHub.
- rswail 5y agoAh, this is awesome. One more thing to set and forget (in a good way). It's like using AWS SSO and containers in Firefox, and standardized profile names in AWS config that allow multiple/cross account access.
- CGamesPlay 5y agoAs a freelancer, I use this technique to handle using my various client email addresses automatically based on directory structure. My old method involved unsetting my user info and an alias to set it on a per-repository basis, but this technique (which I only learned about a few months ago!) is clearly superior.
- Seb-C 5y agoGenerally I prefer to keep both separated, either with two devices or two different OS users. I certainly wouldn't want to keep both on the same profile/device (especially since I have to deal with two profiles for the browser, password manager etc... anyway). But when I need to make a quick exception to this (private project on my work device or work project on my private device), I'm just using the `.git/config` of the repository. It's far easier and reliable.
- jyounker 5y agoTwo devices. One set of dot files.
- noisy_boy 5y agoFor my work, I connect to the office PC via Citrix. Benefits: - My office PC is much more powerful than my laptop and I don't need to deal with its maintenance - I can switch between work and office with couple of clicks - No chances of accidental overlap as I don't see one while using the other (I run full screen); provides mental separation too - I don't need to put Windows on my personal machine (I run Linux) - No work stuff getting into personal machine or vice versa so I don't need to do workarounds such as the .gitconfig setup mentioned in the article The only problem I have observed is that Citrix on Linux seems to have issues exposing the external webcam as an available device when I join a meeting from within the Citrix session (for which we use Citrix (!) WebEx no less!). Cheese on Linux can handle the webcam perfectly fine. When I absolutely must enable Video, I just connect to the meeting directly from my laptop (which is a rare requirement in my case). But this problem could be a show-stopper for those who use video regularly and connect from Linux (my colleagues who use Windows say it works fine for them).
- AdrianB1 5y agoI use a similar setup, except the meetings that I take on the local machine because the sound quality is much better and I avoid problems with the webcam.
- noisy_boy 5y agoThe main issue I've faced while connecting from local machine is when I want to share my office PC's screen. All sorts of issues with resolution (complicated further due to dual monitor), only being able to either share or see the participants but not both and so on. Unfortunate that Citrix don't bother to improve the experience with Linux clients but not entirely surprising either.
- vultour 5y agoI used to connect from both my local machine and from within citrix, use local machine for voice and citrix to share the screen.
- Gehinnn 5y agoBtw if you are interested in configuring line endings in the git config for good, you might find this tool handy: https://hediet.github.io/git-line-endings/ https://hediet.github.io/git-line-endings/ A github action brute forces all combinations of line endings and tries out what happens on checkout etc.
- toadi 5y agoFunny to see a blogpost. This is part from our onboarding. We ask github handle and ask people to use work email to commit to our repo. Hence the conditional so you can keep using your personal email for your own work or opensource.
- gizdan 5y agoI wish this existed for the git remote.
- thamer 5y agoWhat do you mean exactly? If you want to trigger some specific behavior per directory, have a look at `chpwd` for zsh, it's a function you can redefine that gets called every time you enter a new directory. You could for example have a chpwd function that checks if you're under ~/work, if there's a .git directory, and then adjust the remotes as you wish. chpwd docs: https://zsh.sourceforge.io/Doc/Release/Functions.html#Hook-Functions https://zsh.sourceforge.io/Doc/Release/Functions.html#Hook-F... This gist seems to describe a technique for achieving the same thing with bash (I haven't tried it): https://gist.github.com/laggardkernel/6cb4e1664574212b125fbfd115fe90a4 https://gist.github.com/laggardkernel/6cb4e1664574212b125fbf...
- elric 5y agoCan someone elaborate on why this would be preferred over not using a global ~/.gitconfig? You can just use .git/config on a per-repo basis. Which is very convenient if you have multiple identities, and it prevents accidental commits using the wrong identity, because git refuses commits unless a user.email is configured.
- ehnto 5y agoMy thoughts as well. You can set some core commands to be different in your per-project git config which I find more flexible for SSH identities than using a the .ssh/host file. https://git-scm.com/docs/git-config#Documentation/git-config.txt-coresshCommand https://git-scm.com/docs/git-config#Documentation/git-config... git config core.sshcommand "ssh -i /path/to/project/keyfile"
- pferde 5y agoIf you're routinely working on many repositories and cloning new ones, it's more convenient to know that if you clone it into this directory, it will be configured for work. I have used a similar setup for a while now, and it's perfect for me.
- OJFord 5y agoAs soon as work or personal is more than one repo, that's annoying, and gets more annoying the more it is. You could also say 'why does global git config exist at all, you can always just use .git/config on a per-repo basis'.
- selfhoster11 5y agoI'm pretty sure that it's a 10-20 minute job to write a Git subcommand wrapper that checks out a work vs personal project, and then symlinks the correct config file for the repo, if someone chose to go this route.
- shawxe 5y agoBut... how is this easier than the solution proposed by OP?
- trzeci 5y agoI was using that approach for a while, but the problem is if you're in the folder defined in the includeIf - and you clone new repository from there - it will use your root config, not the additional config. includeIf path matching works only if the folder is already under git. A solution is to make a dummy repo in the repo where includeIf points to - but that is annoying a little, as sometimes it confuses IDEs or makes shell (ZSH + addons) slower - as it tries to seek for changes. The best solution I've found is to unconditionally include an additional config in the main config. But the additional config is coming from a dynamic location that I mount in my shell when I work. Basically during the working hour I mount my entire partition which is encrypted under ~/work. And I have my config under ~/work/.gitconfig. I have also there my additional shell config - so during working hours I have everything set up. The cons is that I need to mount/unmount that partition every day.
- _wolfie_ 5y agoI still don't fully get the zero-trust thing. Surely having the service as secured as possible AND behind VPN is safer no? Multi-layer defense is a thing, no?
- amelius 5y agoI personally think it is rather inelegant to reinvent portions of a programming language inside your configuration file format. "IncludeIf" seems to be like that. On top of that, if any modern language would combine "include" and "if" into one statement, then it would be frowned upon.
- xyzzy_plugh 5y agoThis is funny to me, because git config is one of the few bastions of sane configuration. The language is not turing complete, there's no real schema, it's not quite TOML, the tooling to read/write values is sublime. Your custom tools can just start using it however you want. Config is all local, nothing is inherited or executed from remote sources. It's just key=value. Whether value is a URL, a number, a shell command -- totally up for interpretation. Sure, not having a schema has it's downsides but by god, I love git config.
- amelius 5y ago> This is funny to me, because git config is one of the few bastions of sane configuration. Just give it a few years, and we'll have statements like "IncludeIfFileExistsAndSatfisfiesConditionXandY" and you'll be begging for a Turing complete language ;) Also, what happens if your configuration file contains a typo, say "eemail" instead of "email"? My guess is that Git will simply ignore it, whereas in a programming language, you would call "set_email(...)" and you would get an error if the function didn't exist. Now that's what I call sanity.
- OJFord 5y ago> what happens if your configuration file contains a typo, say "eemail" instead of "email"? My guess is that Git will simply ignore Sort of - you will set the config option "eemail", but yes, of course anything looking for "email" configuration will ignore it.
- alexruf 5y agoThere is also the possibility to have a .gitattributes (https://git-scm.com/docs/gitattributes https://git-scm.com/docs/gitattributes) file per path.
- ozim 5y agoThat is overly complicated. Git already handles such situation. For each repo you have config file in .git folder where you can simply add user section just like in global and it will override it. I use that because I work for different customers and have emails for them.
- the_gipsy 5y agoNo, it does make total sense. Instead of repeatedly overriding .gitconfigs, I can now add this rule to ~/.gitconfig.
- joshmanders 5y agoThat may work for some, but some of us work in lots of repos and having to configure it for every repo is cumbersome. I have `~/Code/<me>` for personal, and `~/Code/<company>` for work stuff, I can do this trick and all ~/Code/<company>` repos use my work identity whether I have 1 repo in it or 1000. (Currently have 23).
- blueflow 5y agoMy solution: I don't have a global username and email set up. I have user.useConfigOnly set to true, this causes a hard failure if i try to commit without my identity configured. And when i configure my identity, its on a per-repo basis only.
- rkangel 5y agoI do exactly this. In my case it's just the email I want to vary so it looks like this: [user] name = My Name useConfigOnly = true You get an "Author identity unknown" error when committing to a repo the first time and then you just run this once to set up that repo: git config --local user.email "me@email.net"
- donatj 5y agoOoooooh this is awesome! I have a shell script I use to set my defaults for gitconfig on personal projects, and have my user level gitconfig set up for work, but this is so much cooler. I already keep my personal junk in ~/Projects/donatj so that makes this easy!
- lsaferite 5y agoMy solution to the author's problem is to use `direnv` with `.envrc` files. From those I set several ENV variables based on where in my file structure I'm currently located. Two of those top level variables are `GIT_AUTHOR_EMAIL` and `GIT_COMMITTER_EMAIL` which take priority over the global .gitconfig file values. That allows me to easily use a different identity based on what workspace I'm using.
- jfrunyon 5y ago> It had been my impression that git had no way to support this but that hasn’t been true for some time. Well yeah, but not because of conditional config... I wonder what this person thinks the `--global` in `git config --global` means?
- jyounker 5y agoThanks for posting this. This is the last thing I need so that I can have one set of dot files for both personal and work machines without any horrible hacks.
- mikem170 5y agoFYI, this worked for me only after I added a trailing slash to the gitdir: path.