You can manage temporary SSH configurations without touching your ~/.ssh directory at all by using a completely separate temporary directory and leveraging the -F option with ssh along with the GIT_SSH_COMMAND environment variable.
Here's how:
- Create a temporary directory outside of
~/.ssh. For example:
mkdir -p /tmp/my_temp_ssh- Generate your temporary SSH key pair in this directory:
ssh-keygen -t ed25519 -f /tmp/my_temp_ssh/temp_key-
Add the public key (
/tmp/my_temp_ssh/temp_key.pub) to your GitHub account. -
Create a temporary SSH config file within your temporary directory:
Host github_temp
HostName github.com
User git
IdentityFile /tmp/my_temp_ssh/temp_key
Save this configuration into a file named, for example, /tmp/my_temp_ssh/temp_config.
- Use
GIT_SSH_COMMANDwith the full path to your temporary config file:
GIT_SSH_COMMAND="ssh -F /tmp/my_temp_ssh/temp_config" git pushOr, for multiple Git commands within the same session:
export GIT_SSH_COMMAND="ssh -F /tmp/my_temp_ssh/temp_config"
git push
# other git commands…
unset GIT_SSH_COMMAND- Clean up. When finished, delete the temporary directory:
rm -rf /tmp/my_temp_sshThis approach ensures that your ~/.ssh directory remains untouched, providing complete isolation for your temporary SSH configuration. Remember to adapt paths if you choose a different location for your temporary files.