Using SSH keys to interact with GitHub provides a secure and seamless way to manage repositories. Here’s a step-by-step guide on generating and using SSH keys to push your code to GitHub, accompanied by essential commands.
Step 1: Generate a Public/Private RSA Key Pair
The first step is to generate a pair of SSH keys that will be used for authentication with GitHub.
Command:
#git #gitbash
ssh-keygen -t rsa -b 4096 -C "example@gmail.com"
-t rsa
: This specifies the RSA algorithm for key generation.-b 4096
: This sets the key size to 4096 bits, which is more secure.-C "techroof360@gmail.com"
: This associates the key with your email address.
Next Steps: After running the command, press Enter
three times to choose the default directory for storing your keys. This will create a public/private key pair stored in the ~/.ssh/
directory of your PC.
Step 2: Start the SSH Agent
To manage your SSH keys, you need to start the SSH agent, which will keep your private key loaded in memory.
Command:
#bash #git
eval $(ssh-agent -s)
This command initializes the SSH agent and gives you the agent pid
, confirming that it’s running.
Alternative Command (for Windows):
#bash #git
start-ssh-agent.cmd
If the first command doesn’t work on your system, use this alternative to start the agent.
Step 3: Add Your SSH Key to the SSH Agent
Now, you need to add your SSH private key to the SSH agent.
Command:
#git #bash
ssh-add ~/.ssh/id_rsa
This adds the private key located in ~/.ssh/id_rsa
to the SSH agent.
Step 4: Add the SSH Key to GitHub
You need to add your SSH public key to your GitHub account to allow access. First, copy the public key with the following command:
Command:
#bash #git
clip < ~/.ssh/id_rsa.pub
This command copies your public key to the clipboard. Once copied, follow these steps:
- Go to your GitHub account.
- Navigate to Settings > SSH and GPG keys.
- Click on New SSH Key, give it a title (e.g., “My PC”), and paste the public key.
- Click Save.
Step 5: Initialize Your Git Repository
Now that your SSH key is set up, navigate to the folder you want to push to GitHub and initialize it as a Git repository.
Command:
#git #bash
git init
This command initializes a new Git repository in the folder.
Step 6: Add Files to the Repository
Add all the files in your current folder to the Git staging area.
Command:
#git #bash
git add -A
The -A
flag adds all files (new, modified, and deleted) to the staging area.
Step 7: Commit Your Changes
Create your first commit, which serves as the initial snapshot of your project.
Command:
#bash #git
git commit -m "first commit"
The -m
flag allows you to add a commit message, which in this case is “first commit.”
Step 8: Add the Remote GitHub Repository
Next, add your GitHub repository as the remote origin for your local Git repository.
Command:
#bash #git
git remote add origin git@github.com:username/example.git
Replace username/example.git
with the SSH URL of your repository.
Step 9: Push the Code to GitHub
Finally, push the code from your local repository to GitHub.
Command:
#git #bash
git push origin master
This command pushes the code to the master
branch of your remote GitHub repository. If your repository uses main
as the default branch instead of master
, replace master
with main
.
Note:
By using these steps ensures that your project is securely uploaded to GitHub using SSH. This method not only provides better security but also eliminates the need to enter your credentials each time you push code.
Coding Filters Enhance Collaboration in Development Teams!
In team-based development environments, coding filters help enhance collaboration by making code more modular and easier to understand. By using filters, developers can work on different parts of an application without interfering with one another, streamlining the development process and improving team productivity.