Identity & environment isolation
One laptop, several identities, and no accidents
The problem
Many of us run more than one “self” on the same machine: a day job, a side project, open-source work, a client engagement. Each has its own:
- git author: the name and email stamped on every commit;
- git transport credentials: the SSH key or token used to push;
- GitHub CLI/API login: what
gh, orgh::gh()in R, acts as; - cloud login: the account
gcloud,firebaseorazdeploys with; - AI tool account: which subscription, and which organisation’s data policy, your prompts fall under;
- package registry: where
npm installorinstall.packages()downloads from.
Get any one of these wrong and you can commit company work under a personal email, push to the wrong organisation, deploy into someone else’s cloud project, or send code to an AI tool under the wrong data agreement. None of these fail loudly.
In GxP terms this is ALCOA+ “Attributable”: every record, including a commit, should show unambiguously who created it. Identity isolation is how you make that true by construction, not by remembering.
Principle: scope identity to a folder, not to a machine, and have every session prove its identity before it does anything outward-facing.
The web-repo way
The pattern below is common in JavaScript and TypeScript shops. All of a project’s repos live under one workspace folder, and every identity is pinned to that folder.
1. Folder-scoped git identity with includeIf
Your global ~/.gitconfig keeps your everyday identity. One extra block loads a second config file, but only for repos under the workspace folder:
# ~/.gitconfig (the only global edit)
[includeIf "gitdir/i:~/Projects/Acme/"]
path = ~/Projects/Acme/.gitconfig-acme# ~/Projects/Acme/.gitconfig-acme
[user]
name = Your Name
email = 12345+acme-bot@users.noreply.github.com # GitHub noreply address
signingkey = ~/.ssh/id_ed25519_acme.pub
[core]
sshCommand = ssh -i ~/.ssh/id_ed25519_acme -o IdentitiesOnly=yes
[gpg]
format = ssh
[commit]
gpgsign = true
[credential]
helper = # blank: never reuse another account's token
[url "git@github.com:"]
insteadOf = https://github.com/ # always use SSH, so always this keyWhat each line buys you:
| Setting | Protects against |
|---|---|
gitdir/i: with a trailing / |
Case differences on macOS; the / means “everything under this folder” |
| noreply email | Exposing a personal inbox; commits still link to the right account |
IdentitiesOnly=yes + a dedicated key |
SSH offering your other key first and pushing as the wrong user |
blank credential.helper |
An HTTPS token saved in the OS keychain for another account being reused |
insteadOf |
An https:// URL quietly bypassing the SSH key |
| SSH commit signing | Anyone setting user.email to your address. GitHub shows Verified only for your key |
Check it from inside a repo:
git config --show-origin user.email # shows which file the value came from
ssh -T git@github.com # greets the account that owns the keyHardening dedicated keys with a passphrase
Never leave private SSH keys unencrypted on disk. Add a passphrase to your dedicated key and delegate passphrase caching to the macOS keychain:
ssh-keygen -p -f ~/.ssh/id_ed25519_acme
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_acmeAn empty passphrase is the same as none; it provides no protection if a local file is read or exfiltrated. Once the key is loaded into the agent, SSH commit signing and git operations continue working without interactive passphrase prompts on every commit.
2. CLI logins in the workspace, not in your home folder
Most CLIs store their login in a config folder that an environment variable can override. Point these variables inside the workspace, and the logins can’t collide with your other accounts:
export GH_CONFIG_DIR="$WS/.config/gh" # GitHub CLI
export CLOUDSDK_CONFIG="$WS/.config/gcloud" # gcloud
export XDG_CONFIG_HOME="$WS/.config" # many other CLIs (firebase, etc.)Put these in an activate.sh that you source when you start work, the way you’d activate a Python virtual environment.
3. Guards: print every identity and refuse on a mismatch
The activation script ends by printing each identity, and aborts and rolls back if any of them matches a pattern you never want active here, such as your employer’s domain. The patterns live in a local, never-committed file, so the script itself stays generic.
login="$(gh api user --jq .login)"
if printf '%s' "$login" | grep -Eiq "$BLOCKED"; then
echo "✗ gh is logged in as $login, aborting"; deactivate; return 1
fi
echo "✓ gh: $login"4. No global installs; pin the registry
Tools go into the project as devDependencies, and a project .npmrc pins registry=https://registry.npmjs.org/. Nothing from another context (a corporate mirror in ~/.npmrc, or a globally installed CLI at a different version) can leak in.
5. GUI apps don’t read your shell
Desktop apps and IDE extensions usually don’t source activate.sh. For those, use the tool’s own project-level settings file (for example a local, gitignored settings file that sets the same environment variables). Before any GitHub action, check which account is active again: gh api user --jq .login.
The R way
R already has most of these ideas, spread across a few tools you probably use.
Projects are the “folder scope”
An RStudio project (.Rproj) or a Positron folder sets the working directory when it opens. R then reads a .Renviron from that directory at startup, so it works as a per-project set of environment variables:
# my-project/.Renviron (gitignored)
GITHUB_PAT_GITHUB_COM=ghp_... # the token for THIS project's account
R_CONFIG_ACTIVE=dev.Renviron replaces your personal one
R reads the first .Renviron it finds: the one in the working directory, otherwise ~/.Renviron. It does not merge them. We checked this with two files: the variables from ~/.Renviron disappeared once a project .Renviron existed. If your project needs something from your personal file, copy it across deliberately.
usethis writes the right file for the right scope:
usethis::edit_r_environ(scope = "project") # opens my-project/.Renviron
usethis::edit_r_environ(scope = "user") # opens ~/.RenvironWhich GitHub account does R act as?
gh::gh() and usethis find a GitHub token in this order. We checked this with gh 1.4.1:
GITHUB_PAT_GITHUB_COM(host-specific, the most precise)GITHUB_PAT- the git credential helper, via
gitcreds
A host-specific PAT in the project .Renviron therefore beats whatever token is saved in your keychain. It’s the R equivalent of GH_CONFIG_DIR.
gh::gh_whoami() # which account R is acting as, and the token's scopesGit identity per repo with usethis
usethis::use_git_config(
scope = "project",
user.name = "Your Name",
user.email = "12345+acme-bot@users.noreply.github.com"
)This writes to that repo’s .git/config, so it’s per repo, not per folder. It’s fine for one repo, but it’s easy to forget on the next clone. The includeIf block covers every repo under the workspace automatically, including commits made from the RStudio or Positron Git pane, because both IDEs call your system git.
The situation report is your guard
usethis::git_sitrep() # git user, signing, default branch, GitHub user and tokengit_sitrep() prints the same identities an activate.sh checks. scripts/identity-check.R wraps it in a hard stop you can put at the top of a deployment script (save the companion file in your project as R/identity-check.R):
source("R/identity-check.R")
assert_identity(
expected_email_pattern = "noreply\\.github\\.com$",
blocked_pattern = "@example-employer\\.com$"
)renv isolates packages, not people
renv gives each project its own library and a lockfile, just as node_modules and package-lock.json do. That’s dependency isolation, a separate question from identity isolation, and you need both. renv.lock also records the R version, much as .nvmrc pins the Node version.
Side by side
| Concern | Web-repo way | R way |
|---|---|---|
| Folder scope | includeIf "gitdir/i:…" |
RStudio/Positron project + project .Renviron |
| Git author | workspace .gitconfig-* |
usethis::use_git_config(scope = "project") |
| GitHub account for tools | GH_CONFIG_DIR |
GITHUB_PAT_GITHUB_COM in project .Renviron |
| Verify before acting | activate.sh guards, gh api user |
usethis::git_sitrep(), gh::gh_whoami() |
| Commit signing | SSH signing (gpg.format=ssh) |
Same git setting; the IDE Git pane uses it |
| Dependencies | devDependencies, package-lock.json, .nvmrc |
renv library, renv.lock |
| Registry | project .npmrc |
options(repos = …) in project .Rprofile, or via renv |
Pitfalls we hit
includeIfonly applies once the folder is a git repo. Beforegit init,git config user.emailstill shows your global identity. Check again straight after init.- Desktop apps ignore your shell. An IDE or desktop agent launched from the Dock never sources
activate.sh. Configure the same variables in the tool’s project settings. - A project
.Renvironhides~/.Renviron(see the callout above). - Tools can refuse your runtime. One CLI refused to install on a newer Node than it supports. Pin the runtime per project (
.nvmrc, orrenv.lock’s R version) instead of upgrading or downgrading the whole machine. - The blocked-pattern file is sensitive. It names the identities you’re guarding against. Keep it local and never commit it.
Companion code
View identity-check.R · Download
Defines assert_identity() to stop execution if git user.email or the active GitHub account does not match expected patterns or matches a blocked identity. Uses base R and optionally calls {gh}.
identity-check.R
# Identity guard for R projects.
# Stops with an error if the git author or GitHub account in use is not the
# one this project expects. Source it at the top of any script that commits,
# pushes, publishes or deploys.
#
# nolint start: commented_code_linter. Usage example.
# source("R/identity-check.R")
# assert_identity(
# expected_email_pattern = "noreply\\.github\\.com$",
# blocked_pattern = "@example-employer\\.com$",
# expected_github_login = "my-company-bot" # optional
# )
# nolint end
git_value <- function(key) {
out <- suppressWarnings(
system2("git", c("config", "--get", key), stdout = TRUE, stderr = FALSE)
)
if (length(out) == 0) NA_character_ else out[[1]]
}
github_login <- function() {
if (!requireNamespace("gh", quietly = TRUE)) {
return(NA_character_)
}
# gh_whoami() returns NULL (with a message) when no token is found.
who <- tryCatch(suppressMessages(gh::gh_whoami()), error = function(e) NULL)
if (is.null(who$login)) NA_character_ else who$login
}
assert_identity <- function(
expected_email_pattern,
blocked_pattern = NULL,
expected_github_login = NULL
) {
email <- git_value("user.email")
login <- if (is.null(expected_github_login)) NA_character_ else github_login()
checked <- c(email, login)
checked <- checked[!is.na(checked)]
problems <- character()
if (is.na(email)) {
problems <- c(problems, "git user.email is not set")
} else if (!grepl(expected_email_pattern, email)) {
problems <- c(
problems,
sprintf(
"git user.email '%s' does not match '%s'",
email,
expected_email_pattern
)
)
}
if (
!is.null(blocked_pattern) &&
any(grepl(blocked_pattern, checked, ignore.case = TRUE))
) {
problems <- c(problems, "an active identity matches the blocked pattern")
}
if (
!is.null(expected_github_login) && !identical(login, expected_github_login)
) {
problems <- c(
problems,
sprintf(
"GitHub login is '%s', expected '%s'",
login,
expected_github_login
)
)
}
if (length(problems) > 0) {
stop(
"Identity check failed:\n- ",
paste(problems, collapse = "\n- "),
call. = FALSE
)
}
message(
"✓ git: ",
email,
if (!is.na(login)) paste0("\n✓ GitHub: ", login) else ""
)
invisible(TRUE)
}Sources
Git and SSH
- git config (
includeIf,credential.helper,url.insteadOf,gpg.format): git-scm.com/docs/git-config ssh-add --apple-use-keychain: GitHub Docs: Generating a new SSH key and adding it to the ssh-agent
CLIs and their config-directory variables
- GitHub CLI (
gh),GH_CONFIG_DIR: cli.github.com/manual gh api: cli.github.com/manual/gh_api- gcloud CLI,
CLOUDSDK_CONFIG: Google Cloud SDK: Configurations
R package documentation
- gh (R client for the GitHub API; token lookup order): gh.r-lib.org
- usethis: usethis.r-lib.org
- renv: rstudio.github.io/renv
- 2026-09-26: Removed the end-of-post checklist.
- 2026-09-26: Hardened the dedicated SSH key by adding a passphrase via
ssh-keygen -pand storing it in the macOS keychain agent usingssh-add --apple-use-keychain. Verified that signed commits continue working automatically once the key is resident in the agent. - 2026-09-27: Added a Sources section.
- 2026-09-27: Added a Companion code section with the full script.