📜  got push SSH - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:01:03.415000             🧑  作者: Mango

Git Push with SSH - Shell/Bash

As a programmer, Git is an essential tool to manage version control for your codebase. git push command allows you to upload your local commits to a remote GitHub or GitLab repository.

But if you're tired of entering your credentials every time you push, there's an easier way to authenticate your remote repository: SSH.

Setup SSH Key

To use SSH, you need to generate an SSH key. If you already have one, skip to the next section.

ssh-keygen -t rsa -C "Your_Email@example.com"

This command generates your public and private key. The -C flag adds a comment field with your email address to the key file.

You can add a passphrase to your private key. This adds an extra layer of security and can be useful if you're working on a shared machine.

Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Add SSH Key to your GitHub/GitLab account

You need to add your SSH public key to your GitHub or GitLab account. You can do this manually by copying the public key id_rsa.pub and pasting it into the SSH key section of your account settings or use the command line.

# macOS and Linux
pbcopy < ~/.ssh/id_rsa.pub

# Windows (Git Bash)
cat ~/.ssh/id_rsa.pub | clip
Clone your repository

Clone your repository by using the SSH URL. This is different from the HTTPS URL that you'd use for standard authentication.

git clone git@github.com:your_username/your_repository.git
Push using SSH

Finally, you can push to your repository using SSH. You'll be prompted for your passphrase if you created one during the SSH key setup.

git push

That's it! You'll now be authenticated via SSH for all push commands.

Code Snippet
ssh-keygen -t rsa -C "Your_Email@example.com"

# Use the commands below to copy your public key
# macOS and Linux
pbcopy < ~/.ssh/id_rsa.pub

# Windows (Git Bash)
cat ~/.ssh/id_rsa.pub | clip

# Clone repository using SSH
git clone git@github.com:your_username/your_repository.git

# Push to repository using SSH
git push