|
| 1 | +--- |
| 2 | +title: Setting Up SSH Authentication for GitHub |
| 3 | +intro: 'Learn how to securely connect to GitHub using SSH keys for seamless repository access and code pushing' |
| 4 | +versions: |
| 5 | + free-pro-team: '*' |
| 6 | + enterprise-server: '*' |
| 7 | + enterprise-cloud: '*' |
| 8 | +type: tutorial |
| 9 | +topics: |
| 10 | + - SSH |
| 11 | + - Authentication |
| 12 | + - GitHub |
| 13 | + - Security |
| 14 | +--- |
| 15 | + |
| 16 | +# Step-by-Step: Setting Up SSH Authentication for GitHub |
| 17 | + |
| 18 | +This guide explains how to set up SSH and push code to GitHub step by step. |
| 19 | + |
| 20 | +## 1. Generate SSH Key |
| 21 | + |
| 22 | +Create a new SSH key for authentication. |
| 23 | + |
| 24 | +```bash |
| 25 | +ssh-keygen -t ed25519 -C "[email protected]" |
| 26 | +``` |
| 27 | + |
| 28 | +Press Enter to save the key in the default location (~/.ssh/id_ed25519). |
| 29 | + |
| 30 | +Add a passphrase for extra security (optional). |
| 31 | + |
| 32 | +## 2. Add SSH Key to SSH Agent |
| 33 | + |
| 34 | +Start the SSH agent and add your private key for automatic authentication. |
| 35 | + |
| 36 | +```bash |
| 37 | +eval "$(ssh-agent -s)" # Start the SSH agent |
| 38 | +ssh-add ~/.ssh/id_ed25519 # Add the private key |
| 39 | +``` |
| 40 | + |
| 41 | +## 3. Copy Public Key |
| 42 | + |
| 43 | +Copy your public key to add it to GitHub. |
| 44 | + |
| 45 | +```bash |
| 46 | +cat ~/.ssh/id_ed25519.pub # Show the public key |
| 47 | +``` |
| 48 | + |
| 49 | +## 4. Add SSH Key to GitHub |
| 50 | + |
| 51 | +Go to GitHub: Settings > SSH and GPG Keys > New SSH Key. |
| 52 | + |
| 53 | +Paste your public key into the provided box and give it a title. |
| 54 | + |
| 55 | +## 5. Test SSH Connection |
| 56 | + |
| 57 | +Verify the connection to GitHub. |
| 58 | + |
| 59 | +```bash |
| 60 | + |
| 61 | +``` |
| 62 | + |
| 63 | +You should see a message like: Hi username! You've successfully authenticated, but GitHub does not provide shell access. |
| 64 | + |
| 65 | +## 6. Set SSH URL for Existing Repository |
| 66 | + |
| 67 | +If the repository already exists and uses HTTPS, change it to SSH. |
| 68 | + |
| 69 | +```bash |
| 70 | +git remote set-url origin [email protected]:username/repository.git |
| 71 | +``` |
| 72 | + |
| 73 | +## 7. Clone Repository (Optional) |
| 74 | + |
| 75 | +To download a repository using SSH: |
| 76 | + |
| 77 | +```bash |
| 78 | +git clone [email protected]:username/repository.git |
| 79 | +``` |
| 80 | + |
| 81 | +## 8. Make Changes and Push |
| 82 | + |
| 83 | +Stage your changes: |
| 84 | + |
| 85 | +```bash |
| 86 | +git add . |
| 87 | +``` |
| 88 | + |
| 89 | +Commit your changes with a message: |
| 90 | + |
| 91 | +```bash |
| 92 | +git commit -m "Your commit message" |
| 93 | +``` |
| 94 | + |
| 95 | +Push the changes to GitHub: |
| 96 | + |
| 97 | +```bash |
| 98 | +git push origin main |
| 99 | +``` |
0 commit comments