First, this yet again is for me to document. Second, isn't it annoying to break some CI because your git decided to pick another credential, or because you forgot to set it in your repo-local settings (the latter more often than the former)
Fixing when it already happened
I am writing this while trying to fix the CI/CD for this very website. I mistakenly pushed with my work's email and now all is broken because authorship issues. Then, let's first fix that.
First, enabling the right credentials.
cd <your repo>
git config user.name "Name Lastname"
git config user.email "name.lastname@example.com"Then ammend the last commit resetting the author now with the correct one.
git commit --amend --reset-author --no-editThen push it, add force because it will complain
git push --forceIf you have commit and pushed more than once, then you will have to rebase, which is something I don't enjoy doing. Good luck. If you have been doing it even longer and it is spreaded over the repository and other's branches, then even more luck. (It is possible still, I read how, but I don't have a way to test it, so won't bluff it here).
Avoiding it
Ok, that fixed it, and the github action deployed the updates and the website is up to date now again.
Setting separate folders for different credentials
To avoid it I will use separate folders, say ~/repos/work/ and ~/repos/personal/ and I will
tell git to use different users for each.
~/.gitconfig:
[includeIf "gitdir:~/repos/work/"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/repos/personal/"]
path = ~/.gitconfig-personalNow create those two small files:
~/.gitconfig-work:
[user]
name = Work Name
email = work@example.com~/.gitconfig-personal:
[user]
name = Personal Name
email = personal@example.com(Optional) Update ssh
You may need to update your ssh configs here:
Generate keys if you don’t already have them:
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_personal -C "personal@example.com"
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work -C "work@example.com"~/.ssh/config:
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Host gitlab-work
HostName gitlab.company.com # your company GitLab domain
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yesThen update the public ssh keys in your clouds and the remotes settings
# For a personal GitHub repo
git remote set-url origin git@github-personal:your-user/your-repo.git
# For a work GitLab repo
git remote set-url origin git@gitlab-work:your-group/your-repo.git