DEV Community

.·. Felipe Paz .·.
.·. Felipe Paz .·.

Posted on

Clonando repositório do Github sem senha

Como Configurar uma Chave SSH para o GitHub

Se você quer evitar digitar sua senha toda vez que interage com um repositório privado no GitHub ou precisa configurar uma deploy key para automação, esse guia vai te ajudar a fazer isso do jeito certo.


1️⃣ Criando a Chave SSH

Abra o terminal e rode:

ssh-keygen -t ed25519 -C "seu-email@example.com"
Enter fullscreen mode Exit fullscreen mode

Vai aparecer algo assim:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/seu-usuario/.ssh/id_ed25519):
Enter fullscreen mode Exit fullscreen mode

Pressione Enter para salvar no local padrão ou defina outro caminho se quiser. Depois, pode ser que ele peça uma senha para a chave – é opcional.


2️⃣ Adicionando a Chave ao SSH-Agent

Agora, precisamos garantir que o SSH-Agent está rodando e adicionar a chave:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

Se a chave estiver salva em outro local, ajuste o caminho.


3️⃣ Adicionando a Chave ao GitHub

Copie sua chave pública para adicionar no GitHub:

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Agora vá para o GitHub → Settings → SSH and GPG keys → New SSH Key, cole a chave e salve.

Se for uma Deploy Key (para um único repositório), vá até o repositório no GitHub, depois em Settings → Deploy Keys → Add Deploy Key, cole a chave e, se precisar de escrita, marque a opção Allow write access.


4️⃣ Testando a Conexão

Para garantir que tudo está configurado corretamente, rode:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

Se deu certo, você verá algo assim:

Hi <seu-usuario>! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

5️⃣ Alterando o git remote para SSH

Se você já clonou o repositório via HTTPS e quer mudar para SSH, rode:

git remote set-url origin git@github.com:SEU-USUARIO/SEU-REPOSITORIO.git
Enter fullscreen mode Exit fullscreen mode

Para conferir se foi atualizado:

git remote -v
Enter fullscreen mode Exit fullscreen mode

Agora teste se funciona:

git fetch
Enter fullscreen mode Exit fullscreen mode

Se não deu erro, pronto! Agora suas interações com o GitHub serão feitas via SSH. 🚀


6️⃣ Configurando Múltiplos Usuários no GitHub

Se você trabalha com mais de uma conta do GitHub (exemplo: pessoal e trabalho), pode configurar múltiplas chaves SSH e usá-las de acordo com o repositório. Para isso, siga os passos:

Criar outra chave SSH

Gere uma nova chave para a segunda conta:

ssh-keygen -t ed25519 -C "email-da-outra-conta@example.com" -f ~/.ssh/id_ed25519_outro_usuario
Enter fullscreen mode Exit fullscreen mode

Adicione a nova chave ao SSH-Agent:

ssh-add ~/.ssh/id_ed25519_outro_usuario
Enter fullscreen mode Exit fullscreen mode

Agora copie a chave pública e adicione na outra conta do GitHub:

cat ~/.ssh/id_ed25519_outro_usuario.pub
Enter fullscreen mode Exit fullscreen mode

Configurar o arquivo ~/.ssh/config

Crie (ou edite) o arquivo ~/.ssh/config para diferenciar os usuários:

Host github-pessoal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519

Host github-trabalho
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_outro_usuario
Enter fullscreen mode Exit fullscreen mode

Alterar a URL do git remote

Para um repositório da conta pessoal, use:

git remote set-url origin git@github-pessoal:SEU-USUARIO/SEU-REPOSITORIO.git
Enter fullscreen mode Exit fullscreen mode

Para um repositório da conta do trabalho, use:

git remote set-url origin git@github-trabalho:EMPRESA/REPOSITORIO.git
Enter fullscreen mode Exit fullscreen mode

Agora, toda vez que interagir com um repositório, o Git saberá qual chave usar! 🎯

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay