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 Sh
2 min read
How to Clone Web Project From GitHub in Pycharm Using Git?
PyCharm is one of the most popular Integrated Development Environments (IDEs) for Python development. It provides robust support for version control systems like Git, making it easy to manage your code and collaborate with others. Cloning a web project from GitHub into PyCharm is a simple process th
1 min read
How To Undo Pushed Commits Using Git?
Undoing pushed commits in Git is a common task that developers encounter when they need to rectify mistakes or change their commit history. Depending on the specific scenario and requirements, there are different approaches to undoing pushed commits. Here, we will discuss various methods to achieve
2 min read
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage their code efficiently. To use Git effectively, you need to configure it properly using the git config command. This setup ensures that Git recognizes your identity, preferred settings, and workflow preferences. How the git config
3 min read
How to Clone a Project From GitHub Using Eclipse?
Cloning a project from GitHub using Eclipse is a simple process that allows you to seamlessly integrate your development environment with your GitHub repositories. Whether you're collaborating on a team project or exploring open-source repositories, Eclipse provides a convenient way to clone and wor
2 min read
How to Clone a Project From GitHub using VSCode?
Cloning a project from GitHub is often the first step for developers looking to contribute to open-source projects or work collaboratively with their team. While there are various ways to clone a GitHub repository, using Visual Studio Code (VSCode) adds a layer of convenience and integration. In thi
2 min read
How to use GIT in Ubuntu ? (Part -2)
Git is a powerful version control system that helps developers track changes in their code, collaborate with others, and manage project versions effectively. Using Git on Ubuntu is simple and efficient. In the previous article, we learnt how to use basic GIT. In this article, we will try to learn so
5 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