Skip to content

Instantly share code, notes, and snippets.

@nardhar
Last active September 4, 2019 03:49
Show Gist options
  • Save nardhar/3738f6f21431044945fcac158fc81df1 to your computer and use it in GitHub Desktop.
Save nardhar/3738f6f21431044945fcac158fc81df1 to your computer and use it in GitHub Desktop.
guide for adding a git deploy key to server, for not adding a user (or using your own account)

Git add deploy key

Note: this only works when your cloned repo uses SSH url, e.g.: git@domain.com:group/repository.git

Tested in Debian with GitLab

Steps

  1. Generate ssh key for repo and enter another filename (not the default one) for saving the key, e.g: /home/user/.ssh/id_rsa_my_repo
ssh-keygen -o -t rsa -b 4096 -C "my@email.com"
  1. Upload the public key to the git repository server in the Deploy keys section, and mark it as read-only (as good practice)
cat ~/.ssh/id_rsa_my_repo.pub
  1. Clone the repository
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_my_repo" git clone git@domain.com:group/repository.git
  1. Add the private key to your /repo/.git/config
cd my_repo
git config core.sshCommand "ssh -o IdentitiesOnly=yes -i ~/.ssh/id_rsa_my_repo -F /dev/null"
  1. Now you can update the code as usual
cd my_repo
git pull
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment