git_set_identity
This script will set the identity (user.name, user.email, and user.signingKey) in the Git repositories of the given directory.
|
This script is useful if you do not use the same identity in all the projects you work on:
See: longer example |
The following parameters are required:
e-
the email to be used for the identity; can be omitted if
git config get user.emailis set, and you want to use its value; also used for GPG secret key retrieval if-sis not set n-
the name to be used for the identity; can be omitted if
git config get user.nameis set, and you want to use its value
The following parameters are optional:
d-
the directory (
$PWDif not given) containing a Git repository r-
used in conjuction with
-dto set the identity in direct child directories of the given directory$ scripts/git/git_set_identity.sh -r -d rootroot (1) ├── one (2) │ └── three (1) └── two (2)1 identity will not be set 2 identity will be set if a Git repository s-
the path to the SSH public key if you want to use Git SSH signing
|
This script will set up key signing if
In that case, the following Git configuration will be applied in addition to
|
|
If you want to use Git SSH signing, you need to configure the allowed signers file in order for the local verification to work.
|
Usage
$ scripts/git/git_set_identity.sh
$ scripts/git/git_set_identity.sh -s ~/.ssh/id_dsa.pub
$ scripts/git/git_set_identity.sh -r -d ~/workspace/company1 -e user@company1.com
$ scripts/git/git_set_identity.sh -r -d ~/workspace/company2 -e user@company2.com -s ~/.ssh/id_company2.pub
$ scripts/git/git_set_identity.sh -e cameron@howe.name
$ scripts/git/git_set_identity.sh -r -d ~/workspaces/cardiff-electric -e catherine.howe@cardiff-electric.com -n 'Catherine Howe'
$ scripts/git/git_set_identity.sh -s ~/.ssh/id_dsa.pub -a ~/.ssh/git_allowed_signers
Example
Cameron works for three companies and also has personal projects:
$ tree --noreport ~/workspaces
/Users/chowe/workspaces
├── cardiff-electric
│ ├── backend
│ └── frontend
├── comet
│ └── portal
├── mutiny
│ └── giant
└── personal
├── sandbox
└── secret-project
She has three GPG keys—one for each company and one personal use—and one SSH key:
$ gpg --list-secret-keys
/Users/chowe/.gnupg/pubring.kbx
-----------------------------------------
sec rsa4096/49F1A380A5660737 2025-02-02 [SC] [expires: 2029-02-02]
Key fingerprint = 3956 4B7A A0B5 1F88 A8F8 BCBC 49F1 A380 A566 0737
uid [ultimate] Cameron Howe <c.howe@mutiny.com>
ssb rsa4096/8B68480DE279CA88 2025-02-02 [E] [expires: 2029-02-02]
sec rsa4096/EC073F7EFD23C1FA 2025-02-02 [SC] [expires: 2029-02-02]
Key fingerprint = 2DAF 54C7 67F3 FD24 EACF A1B5 EC07 3F7E FD23 C1FA
uid [ultimate] Cameron Howe <cameron@howe.name>
ssb rsa4096/6363EEBF938865CF 2025-02-02 [E] [expires: 2029-02-02]
sec rsa4096/77254FBC1027F48D 2025-02-02 [SC] [expires: 2029-02-02]
Key fingerprint = C09B 7924 274A 9865 545C 6512 7725 4FBC 1027 F48D
uid [ultimate] Catherine Howe <catherine.howe@cardiff-electric.com>
ssb rsa4096/6C2E4F6CB45DD27E 2025-02-02 [E] [expires: 2029-02-02]
$ cat ~/.ssh/id_ed25519-comet-chowe.pub
ssh-ed25519 ABAAC3NzaC1lZDI1NTE5AAAAIEGXWK6+HHmAtv2Sh9nn1WCYOIzgvV7OojYTkKIyuI88 chowe@comet.com
She configured her personal GPG key in her global Git configuration and signs commits and tags by default:
$ cat ~/.gitconfig
[user]
name = Cameron Howe
email = cameron@howe.name
signingkey = 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
[commit]
gpgsign = true
[tag]
forceSignAnnotated = true
gpgsign = true
She also added the SSH key to her global allowed signers file:
$ echo "chowe@comet.com namespaces=\"git\" $(cat ~/.ssh/id_ed25519-comet-chowe.pub)" >>~/.ssh/git_allowed_signers
With this configuration, the following identities will be used for commits and tags:
$ find ~/workspaces/cardiff-electric -mindepth 1 -maxdepth 1 -type d -exec sh -c '(cd {} && echo "$PWD" && echo " $(git config get user.name) <$(git config get user.email)> $(git config get user.signingkey)")' \;
/Users/chowe/workspaces/cardiff-electric/frontend
Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
/Users/chowe/workspaces/cardiff-electric/backend
Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
$ find ~/workspaces/comet -mindepth 1 -maxdepth 1 -type d -exec sh -c '(cd {} && echo "$PWD" && echo " $(git config get user.name) <$(git config get user.email)> $(git config get user.signingkey)")' \;
/Users/chowe/workspaces/comet/portal
Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
$ find ~/workspaces/mutiny -mindepth 1 -maxdepth 1 -type d -exec sh -c '(cd {} && echo "$PWD" && echo " $(git config get user.name) <$(git config get user.email)> $(git config get user.signingkey)")' \;
/Users/chowe/workspaces/mutiny/giant
Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
$ find ~/workspaces/personal -mindepth 1 -maxdepth 1 -type d -exec sh -c '(cd {} && echo "$PWD" && echo " $(git config get user.name) <$(git config get user.email)> $(git config get user.signingkey)")' \;
/Users/chowe/workspaces/personal/secret-project
Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
/Users/chowe/workspaces/personal/sandbox
Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
She now uses this script to configure the identities for the company’s projects:
$ scripts/git/git_set_identity.sh \
-r \
-d ~/workspaces/cardiff-electric \
-e catherine.howe@cardiff-electric.com \
-n 'Catherine Howe'
Catherine Howe <catherine.howe@cardiff-electric.com> C09B7924274A9865545C651277254FBC1027F48D - /Users/chowe/workspaces/cardiff-electric/backend
Catherine Howe <catherine.howe@cardiff-electric.com> C09B7924274A9865545C651277254FBC1027F48D - /Users/chowe/workspaces/cardiff-electric/frontend
$ scripts/git/git_set_identity.sh \
-d ~/workspaces/comet/portal \
-e chowe@comet.com \
-s ~/.ssh/id_ed25519-comet-chowe.pub \
-a ~/.ssh/git_allowed_signers
Cameron Howe <chowe@comet.com> /Users/chowe/.ssh/id_ed25519-comet-chowe.pub - /Users/chowe/workspaces/comet/portal
$ scripts/git/git_set_identity.sh \
-d ~/workspaces/mutiny/giant \
-e c.howe@mutiny.com
Cameron Howe <c.howe@mutiny.com> 39564B7AA0B51F88A8F8BCBC49F1A380A5660737 - /Users/chowe/workspaces/mutiny/giant
The following identities will be used for commits and tags now:
$ find ~/workspaces/cardiff-electric -mindepth 1 -maxdepth 1 -type d -exec sh -c '(cd {} && echo "$PWD" && echo " $(git config get user.name) <$(git config get user.email)> $(git config get user.signingkey)")' \;
/Users/chowe/workspaces/cardiff-electric/frontend
Catherine Howe <catherine.howe@cardiff-electric.com> C09B7924274A9865545C651277254FBC1027F48D
/Users/chowe/workspaces/cardiff-electric/backend
Catherine Howe <catherine.howe@cardiff-electric.com> C09B7924274A9865545C651277254FBC1027F48D
$ find ~/workspaces/comet -mindepth 1 -maxdepth 1 -type d -exec sh -c '(cd {} && echo "$PWD" && echo " $(git config get user.name) <$(git config get user.email)> $(git config get user.signingkey)")' \;
/Users/chowe/workspaces/comet/portal
Cameron Howe <chowe@comet.com> /Users/chowe/.ssh/id_ed25519-comet-chowe.pub
$ find ~/workspaces/mutiny -mindepth 1 -maxdepth 1 -type d -exec sh -c '(cd {} && echo "$PWD" && echo " $(git config get user.name) <$(git config get user.email)> $(git config get user.signingkey)")' \;
/Users/chowe/workspaces/mutiny/giant
Cameron Howe <c.howe@mutiny.com> 39564B7AA0B51F88A8F8BCBC49F1A380A5660737
$ find ~/workspaces/personal -mindepth 1 -maxdepth 1 -type d -exec sh -c '(cd {} && echo "$PWD" && echo " $(git config get user.name) <$(git config get user.email)> $(git config get user.signingkey)")' \;
/Users/chowe/workspaces/personal/secret-project
Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
/Users/chowe/workspaces/personal/sandbox
Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
Prerequisites
-
OpenSSH if you want to use Git SSH signing