Push Codes from Linux (UBUNTU) To GitHub using SSH KEY
Last Updated :
17 May, 2024
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>
Generate SSH keyStep 2: Register a new private key using this command.
eval "$(ssh-agent -s)"
Register private keyStep 3: Now add the private key to ssh-agent by executing this command.
ssh-add ~/.ssh/id_rsa
add the private key to ssh-agentStep 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 [email protected]:<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 [email protected]:<your username>/<new repo name>.git
Create connection to GitHubStep 6: Now we have to view the public ssh key and copy it for further process.
cat ~/.ssh/id_rsa.pub
View Public KeyStep 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 [email protected]
Testing the connectionNow 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
move to the main branchStep 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.
Add file for pushingStep 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
Pushing changes to main branchFinally, we are successfull pushing our code to the github.
Push Codes from Linux (UBUNTU) To GitHub using SSH KEY
Similar Reads
How to Push Code to Github using Pycharm? Git is an open-source version control system. It means that whenever a developer develops some project (like an app or website) or something, he/she constantly updates it catering to the demands of users, technology, and whatsoever it maybe, Git is a version control system that lets you manage and k
2 min read
How to Push Folders From Local PC to GitHub using Git Commands? Git offers a powerful command-line interface for managing code repositories, understanding how to push folders from your local PC to GitHub repositories using Git commands can enhance your workflow and streamline the process of sharing code with collaborators or deploying projects. In this article,
2 min read
How to Generate Public SSH or GPG Keys Using Git Commands? Generating public SSH or GPG keys is important for securing your Git operations and ensuring the authenticity of your commits. In this guide, we'll walk you through the process of generating these keys using Git commands, making it easy to track along and understand.What Are SSH Keys?SSH (Secure She
2 min read
How to Connect to Amazon Linux Instance Using SSH? Pre-requisites: AWS, SSH TOOLS AWS stands for Amazon Web Services, and it is a cloud computing platform provided by Amazon. AWS provides a range of cloud-based services, including computing power, storage, databases, analytics, machine learning, and many other resources that businesses and individua
3 min read
How to Install, Configure and Use GIT on Ubuntu? Git is a powerful version control system widely used for tracking changes in source code during software development. This article will walk you through the steps to install, configure, and use Git on an Ubuntu system. Table of Content Installing Git with Default PackagesInstalling/Downloading Git f
5 min read
How to Add SSH Key to Your GitHub Account? An SSH (Secure Shell) key is a pair of cryptographic keys used for authenticating and securing network communications over the SSH protocol. SSH keys are commonly used to securely access remote systems, transfer files, and execute commands on remote machines without passwords. Components of an SSH K
3 min read
How to Push Anything to GitHub using Git Bash? GitHub has become the go-to platform for collaborative software development, offering powerful tools for version control, collaboration, and project management. Git Bash, a command-line interface for Git on Windows, provides a quick way to interact with GitHub repositories. In this guide, we'll lear
3 min read
How to Create a Pull Request on GitHub using Android Studio? Creating a pull request is an important part of collaborating on projects hosted on GitHub. It allows you to propose changes to a repository, enabling others to review, discuss, and merge your changes. Hereâs a step-by-step guide on how to create a pull request on GitHub using Android Studio. Steps
2 min read
Creating Pull Requests from VS Code on GitHub Creating pull requests is an important part of collaborative software development. A pull request (PR) allows you to propose changes to a codebase, which can then be reviewed and merged by others. While GitHub's web interface is a common way to handle pull requests, Visual Studio Code (VS Code) offe
5 min read
How to Login to SSH Without A Password Using Private Key? SSH or Secure Shell or Secure Socket Shell is a cryptographic network protocol. Two computers can share sensitive data over an insecure network using SSH. It can be used for strong password authentication and encrypted communication using a public-private key pair. SSH is commonly used for remotely
4 min read