Use another SSH key/command for Git

October 05, 2018

  • git

Originally posted by me on Hashrocket TIL

If you need to use a different SSH key for a git command such as git push you can do so by setting the GIT_SSH_COMMAND environment variable like so:

GIT_SSH_COMMAND="ssh -i ~/.ssh/custom_key" git push

This can become a lot easier to use if you define that env var automatically when entering a directory by using direnv.

h/t Thomas Allen


Written by Dorian Karter, a Sr. Software Engineer
Github | Twitter

© 2021, Dorian Karter