Open In App

Push Codes from Linux (UBUNTU) To GitHub using SSH KEY

Last Updated : 17 May, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

GitHub, one of the most popular platforms for version control and collaboration, offers multiple authentication methods for interacting with repositories. SSH keys provide a secure and convenient way to authenticate and push code from a local machine to GitHub. In this article, we'll learn the step-by-step process of pushing code from Linux (Ubuntu) to GitHub using SSH keys.

What is an SSH Key?

The safest way to connect to GitHub and push/pull code is by using an SSH key. SSH (Secure Shell) is an encryption protocol used to provide secure remote login between computers. SSH key is a pair of cryptographic keys that you can use to authenticate a user to a remote system. Generating an SSH key gives you a public key and a private key. The public key must first be published on GitHub, whereas the private key must be kept on your personal machine. With an SSH key based on the protocol that you use to connect with GitHub, you can easily validate your identity and push/pull code from GitHub, without entering your username and password.

Steps to Push Codes from Linux (UBUNTU) To GitHub using SSH KEY.

Step 1: Go to the project folder you wish to push to GitHub. From the folder generate an ssh-key by opening the terminal in that folder and executing this command.

ssh-keygen -t rsa -b 4096 -C <your email>
20
Generate SSH key

Step 2: Register a new private key using this command.

eval "$(ssh-agent -s)"
21
Register private key

Step 3: Now add the private key to ssh-agent by executing this command.

ssh-add ~/.ssh/id_rsa
22
add the private key to ssh-agent

Step 4: Create a new Repo on you github from the official website. Let's name the repo "Testing".

Step 5: Now we have to create a connection between our computer and the github. For this type the following command.

  • "git init" initializes a new Git repository in the current directory, allowing you to track changes to your code.
  • "git remote add origin git@github.com:<your username>/<new repo name>.git" links your local Git repository to a remote repository on GitHub, named <new repo name>, owned by <your username>, allowing you to push and pull changes between the two.
git init  && git remote add origin git@github.com:<your username>/<new repo name>.git
23
Create connection to GitHub

Step 6: Now we have to view the public ssh key and copy it for further process.

cat ~/.ssh/id_rsa.pub
24
View Public Key

Step 7: Now after copying the public ssh key, head towards your github profile settings and click on the " ssh key " option. Click on "add" option and paste the copied ssh key. For more details on how to add ssh key to your profile, you can view the Github SSH Key Documentation.

Step 8: The next step is to test the connection, if its working or not. For this type the following command.

ssh -T git@github.com
25
Testing the connection

Now we are successfully authenticated to github. After this we can perform the pushing tasks.

Step 9: To push your code to the github, firstly move to the main branch. "git branch -M main" is a Git command that renames the current branch to main.

git branch -M main
26
move to the main branch

Step 10: Now we are going to add the file that we have to push.

git add Testing

"Testing" is the file name for my code.

27
Add file for pushing

Step 11: Now we will be pushing our file "testing" to the branch "main".

"git push --force origin main" is a Git command that pushes changes from your local repository to a remote repository (in this case, origin) and updates the main branch.

git push --force origin main
28
Pushing changes to main branch

Finally, we are successfull pushing our code to the github.

29
Push Codes from Linux (UBUNTU) To GitHub using SSH KEY

Next Article

Similar Reads

three90RightbarBannerImg