This is a very simple Git credential helper that dynamically selects the correct authentication credentials based on:
- The remote URL you're pushing to
- Your configured Git user.email and user.name
- Git calls this script when authentication is needed.
- The script checks:
- The remote URL you're pushing to.
- Your configured user.email (highest priority).
- Your configured user.name (fallback if no email match is found).
- It searches
~/.git-credentialsfor the correct credentials. - If a match is found, it provides the correct username and token for authentication.
This means: ✅ No need to manually switch credentials ✅ Works automatically based on your repo’s Git config or global config ✅ No per-repo configuration required
- Clone this repo and move the script into your
$PATH:
git clone https://github.com/kr-nn/gitauth
cd gitauth
chmod +x gitauth
mv gitauth ~/.local/bin/ # Or /usr/local/bin/nix shell github:kr-nn/gitauthor
nix profile install github:kr-nn/gitauthor
# flake.nix
{
inputs.gitauth.url = "github:kr-nn/gitauth";
...
outputs = let
gitauthPkg = gitauth.packages.${system}.gitauth
in { ... gitauthPkg, ... } : { ... specialArgs = { gitauthPkg }; }
}
...
environment.systemPackages = [ gitauthPkg ];- Set it as your Git credential helper:
git config --global credential.helper "!gitauth"- Make sure your
~/.git-credentialsfile contains your stored credentials:
https://user1:ghp_token1@github.com
https://user2:ghp_token2@github.com
https://user1@email.com:ghp_emailtoken1@github.com
https://user2@email.com:ghp_emailtoken2@github.com
!NOTE: do not use %40 in place of @, the script does this for you
- Set your
user.emailoruser.namein each repo or globally:
git config user.email "user1@email.com"
git config user.name "user1"or
git config --global user.email "user1@email.com"
git config --global user.name "user1"Just push as usual:
git push # or git push originThe script will automatically select the right token based on your user.email or user.name!
Why Use This?
🔹 Simple: No complex setup, just drop it in and it works.
🔹 Automatic: Picks the right credentials for each repo.
🔹 Portable: Works anywhere, no per-repo config needed.