git_set_identity

This script will set the identity (user.name, user.email, and user.signingKey) in the Git repositories of the given directory.

The following parameters are required:

e

the email to be used for the identity and GPG secret key retrieval; can be omitted if git config get user.email is set, and you want to use its value

n

the name to be used for the identity; can be omitted if git config get user.name is set, and you want to use its value

The following parameter is optional:

d

the directory ($PWD if not given) containing the Git repositories

This script will not recursively search the given directory for Git repositories—​only direct child directories are considered.

This script will set up GPG signing under the following conditions:

  1. git config get gpg.program or gpg/gpg2 is installed

  2. a GPG secret key for the given email exists

In that case, the following Git configuration will be applied in addition to user.email and user.name:

commit.gpgSign

set to true

tag.forceSignAnnotated

set to true

tag.gpgSign

set to true

user.signingKey

set to the first secret key returned by --list-secret-keys for the given email

This script is useful if you do not use the same identity in all the projects you work on.

See: Example

Usage

$ scripts/git/git_set_identity.sh
$ scripts/git/git_set_identity.sh -e cameron@howe.name
$ scripts/git/git_set_identity.sh \
-d ~/workspaces/mutiny \
-e c.howe@mutiny.com
$ scripts/git/git_set_identity.sh \
-d ~/workspaces/cardiff-electric \
-e catherine.howe@cardiff-electric.com \
-n 'Catherine Howe'

Example

Cameron works for two companies and also has personal projects:

$ tree --noreport ~/workspaces
/Users/example/workspaces
├── cardiff-electric
│   ├── backend
│   └── frontend
├── mutiny
│   └── giant
└── personal
    ├── sandbox
    └── secret-project

She has three GPG secret keys—​one for each company and one personal:

$ gpg --list-secret-keys
/Users/example/.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]

She has 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

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/example/workspaces/cardiff-electric/frontend
  Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
/Users/example/workspaces/cardiff-electric/backend
  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/example/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/example/workspaces/personal/secret-project
  Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
/Users/example/workspaces/personal/sandbox
  Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA

She now uses this script to configure the identities for the companies' projects:

$ scripts/git/git_set_identity.sh \
-d ~/workspaces/cardiff-electric \
-e catherine.howe@cardiff-electric.com \
-n 'Catherine Howe'
Catherine Howe <catherine.howe@cardiff-electric.com> C09B7924274A9865545C651277254FBC1027F48D - /Users/example/workspaces/cardiff-electric/backend
Catherine Howe <catherine.howe@cardiff-electric.com> C09B7924274A9865545C651277254FBC1027F48D - /Users/example/workspaces/cardiff-electric/frontend

$ scripts/git/git_set_identity.sh \
-d ~/workspaces/mutiny \
-e c.howe@mutiny.com
Cameron Howe <c.howe@mutiny.com> 39564B7AA0B51F88A8F8BCBC49F1A380A5660737 - /Users/example/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/example/workspaces/cardiff-electric/frontend
  Catherine Howe <catherine.howe@cardiff-electric.com> C09B7924274A9865545C651277254FBC1027F48D
/Users/example/workspaces/cardiff-electric/backend
  Catherine Howe <catherine.howe@cardiff-electric.com> C09B7924274A9865545C651277254FBC1027F48D

$ 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/example/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/example/workspaces/personal/secret-project
  Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA
/Users/example/workspaces/personal/sandbox
  Cameron Howe <cameron@howe.name> 2DAF54C767F3FD24EACFA1B5EC073F7EFD23C1FA