Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Setup GitHub repository SSH deploy keys

Setup GitHub repository SSH deploy keys

  1. Create GitHub repository in github and save the SSH repository url

  2. Init git on server in code directory

  • git init
  1. Create SSH keys on the server
  • ssh-keygen -t rsa -b 4096 -C your@email.here
  • Rename the key that doesn't end with .pub to repo-name.deploy.pem
  • Save .pub file content to your local client for use later
  • Move .pem key to ~/.ssh/ or suitable location
  • Change key file permission: sudo chmod 400 repo-name.deploy.pem
  1. Create ssh-config file
  • Navigate to ~/.ssh and touch config set permission sudo chmod 644 config
  • Add entry in config file
Host gitreponame
Hostname github.com
IdentityFile ~/.ssh/repo-name.deploy.pem
  1. Add the content from the public key file i.e. .pub to your github repository, settings > deploy keys > Add deploy keys

  2. Set the remote git repository url on the server for the initalized git repo

  • git remote add origin git@gitreponame:organization-name/repo-name.git or if already existing git remote set-url origin git@gitreponame:organization-name/repo-name.git
  • test with: git remote -v
  1. Add files to commit and push them to the master branch
  • git add .
  • git commit -m 'root commit'
  • git push -u origin master
@blipsman

This comment has been minimized.

Copy link

@blipsman blipsman commented Feb 6, 2020

Could you please explain why do we need to setup SSH deploy keys? I did create them and then added them to my GitHub Account, because I needed access to GitHub from my IntelliJ. So my understanding is - the SSH deploy keys are needed if I need to access the GitHub from IntelliJ or another specific environment ??? Thank you

@holmberd

This comment has been minimized.

Copy link
Owner Author

@holmberd holmberd commented Feb 9, 2020

@blipsman Are you deploying anything on a server? If you don't know what it's for then you probably don't need it.

For local clients general access to github and all of your repositories. Add a SSH key to your account under "Personal Settings" and use a ssh-config entry as the one below.

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/git.pem
@tispokes

This comment has been minimized.

Copy link

@tispokes tispokes commented Feb 29, 2020

What is the difference between "remote" (see https://gist.github.com/Nilpo/8ed5e44be00d6cf21f22#su)
and "deploy keys"?
Were are 4 ppl working on a JS project and want the possibility to push it to live (see link), when needed.

Thank you

@holmberd

This comment has been minimized.

Copy link
Owner Author

@holmberd holmberd commented Mar 2, 2020

@tispokes Can you are explain what you mean with push it live?

@rlfrahm

This comment has been minimized.

Copy link

@rlfrahm rlfrahm commented Mar 27, 2020

You've greatly demystified deploy keys for me. Thank you for this!

@ryan-williams

This comment has been minimized.

Copy link

@ryan-williams ryan-williams commented Aug 12, 2020

Configuration the key via .ssh/config as demonstrated here is nice.

I just learned about:

git config core.sshCommand 'ssh -i private_key_file'

via StackOverflow, which is even better, for my purposes, so wanted to mention it here.

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