Creating Your First GitHub Repository with SSH Authentication π±π
Set up Git and GitHub
π Install Git
First things first, you need Git on your machine:
- Windows: Download the Git installer from the official Git website and follow the installation prompts.
- Linux: Use your distribution's package manager with
sudo apt-get install git
With Git installed, open a terminal and configure your user name and email address:
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
π± Creating a GitHub account
If you haven't already, head over to GitHub and sign up.
Link Git with GitHub
To connect your local Git environment with GitHub, you can use HTTPS or SSH. While HTTPS is straightforward, SSH offers a more secure and convenient method of authentication.
π HTTPS method
HTTPS stands for Hypertext Transfer Protocol Secure. It is the protocol used for secure communication over the internet.
It does not need any additional configuration, so you can start using it immediately after setting up your Git and GitHub accounts. Another advantage is that it works in almost any network environment without needing special permissions.
However, it requires frequent authentication (entering your username and password) which can be cumbersome for those who plan to push to GitHub frequently.
π SSH method
SSH stands for Secure Shell. It is a protocol used for secure login and communication over an unsecured network. When using SSH with Git and GitHub, you create a pair of SSH keys (a public key and a private key) and add the public key to your GitHub account. This setup allows secure, password-less communication with GitHub.
It is secure and it is very convenient for frequent use as once set up, you don't need to enter your credentials again for every push or pull.
However, it some corporate restricted networks block SSH traffic, which could require additional configuration or switching to HTTPS.
π§ HTTPS is user-friendly and easy to set up, making it a good choice for beginners or for those who don't use Git very frequently. SSH, while requiring a bit more initial setup, offers a more secure and convenient workflow for those who are frequently interacting with GitHub.
ποΈ Step-by-step guide to set up SSH keys
-
Open terminal: Git Bash on Windows or the default terminal on Linux
-
Generate a new SSH key: Enter the following command, replacing
your_email@example.com
with your GitHub email address:ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
When prompted to "Enter a file in which to save the key," press Enter to accept the default file location.
-
Start the SSH agent: Before adding your SSH key to the agent, ensure the ssh-agent is running by executing:
eval $(ssh-agent -s)
-
Change the file permissions of the private key: SSH requires that the private key file is not accessible by others for security reasons.
chmod 600 ~/.ssh/id_rsa
-
Add your SSH key to the SSH agent: Run the following command:
ssh-add ~/.ssh/id_rsa
-
Copy the SSH key: Run this code to display your SSH key in the terminal, which you can then manually copy.
cat ~/.ssh/id_rsa.pub
-
Add Your SSH Key to GitHub: Go to your GitHub account, navigate to Settings > SSH and GPG keys > New SSH key, paste your key into the field, give it a title, and click "Add SSH key."
Creating your first repository
-
Log in to GitHub and create a new repository:
-
Push an existing repository from the command line:
-
Open terminal and change the current working directory to your local project.
-
Initialize the local directory as a Git repository, if you haven't already:
git init
-
If you do not have any files in your project yet, you can add simple
README.md
:echo "# Project Title" > README.md
-
Add the files in your new local repository. This stages them for the first commit:
git add .
-
Commit the files that you've staged in your local repository:
git commit -m "First commit"
-
Copy the SSH URL of your repository from GitHub, which looks like
git@github.com:username/repository-name.git
. -
Add the URL as a remote repository for your project:
git remote add origin git@github.com:username/repository-name.git
-
Push the changes in your local repository to GitHub:
git push -u origin main
-
Now that you've set up SSH authorization and created your first repository, you can start collaborating on projects more securely and efficiently π«.