Skip to content

Instantly share code, notes, and snippets.

@timwu-ipevo
Created January 20, 2025 01:44
Show Gist options
  • Save timwu-ipevo/be6d5fc6a58090c3502f67d1455805af to your computer and use it in GitHub Desktop.
Save timwu-ipevo/be6d5fc6a58090c3502f67d1455805af to your computer and use it in GitHub Desktop.

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:

  1. Create a temporary directory outside of ~/.ssh. For example:
mkdir -p /tmp/my_temp_ssh
  1. Generate your temporary SSH key pair in this directory:
ssh-keygen -t ed25519 -f /tmp/my_temp_ssh/temp_key
  1. Add the public key (/tmp/my_temp_ssh/temp_key.pub) to your GitHub account.

  2. 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.

  1. Use GIT_SSH_COMMAND with the full path to your temporary config file:
GIT_SSH_COMMAND="ssh -F /tmp/my_temp_ssh/temp_config" git push

Or, 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
  1. Clean up. When finished, delete the temporary directory:
rm -rf /tmp/my_temp_ssh

This 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment