Language
Category
Search

How to upload a new project to GitHub with Linux and SSH, no password

Initialize Git on the local machine, change the name of the default branch, create an SSH key and add it to GitHub, upload the project and learn how to synchronize the repositories

At Terminal By Rudi Drusian Lange
Published on
Last updated

Before you start

Create a Github account and add a new repository. This part is relatively simple and Github itself helps you through the process.

Installation environment

In this article the configurations were carried out using Linux Slackware 15.0 and with the native tools Git version 2.39.4 and OpenSSH 9.3p2. The commands used in this material should work on any other Linux distribution with Git and SSH installed.

Getting started

Assuming a project called telazul, let's first initialize Git in its directory:

Shell

# enter the directory
cd telazul/

# initialize Git
git init

# adds all files in the current directory
git add .  

# commit
git commit -m "Initial commit: adding files"

# rename the main branch from master to main
git branch -m master main 

The above commands are used to initialize Git in the project directory, add the file tree and save its current state with an informative message.

These are steps for using Git, regardless of whether the project will be used on GitHub or not. Remember that Git works in a distributed way, meaning that you don't need a central server to use it or even an internet connection.

In the end we renamed the default local branch from master to main in order to maintain compatibility with GitHub, which uses the name main for the default branch. Without this change, an alternative branch called master is created when we upload the project.

Setting up SSH access on GitHub

The project will be transferred using SSH, which is a secure and practical way to transfer files over the internet. 

Let's create the key pair that will be used as credential to log in securely to GitHub without having to enter a password.

# creates the key pair using the ed25519 algorithm
ssh-keygen -t ed25519

When prompted press to keep the default location of the keys and to use a blank password. If you already have keys created that are compatible with GitHub, you don't need to create a new one.

Between computers using Linux, the public key can be transferred using the ssh-copy-id command. On GitHub, you need to copy and paste the key into the platform's graphical interface. Copy the text of the newly created key, remember to use the file ending in .pub, which contains the public key:

cat /home/username/.ssh/id_ed25519.pub

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEDbDtQNhBvGd292FgKDRgqhiXGiLHo32ZYXCSS017d5 username@hostname

Copy the line starting with ssh-ed25519, go to GitHub, click on the menu with your avatar , then Settings > SSH and GPG Keys.

You can also use the direct link: https://github.com/settings/keys.

Click New SSH Key, add a title to the Title field, paste the public key into the Key field and click Add SSH Key.

The configuration should look something like this:

github-ssh-authentication-keys

Uploading the project to GitHub

The following command adds the alias origin to the remote repository with the same content as the project. You can add multiple remote repositories and origin is the default name used by Git.

git remote add origin git@github.com:your-username/your-repository.git

Remember to run the command from your project folder and change the your-username and your-repository values with your information.

Finally, the next step is to make a copy of the project to the remote repository on GitHub:

git push -u -f origin main
  • u (or --set-upstream) sets origin as the reference. This allows you to use the git push and git pull commands without having to inform the remote repository.
  • -f (or --force) will overwrite everything in the remote repository, used when the command refuses to complete the update.

Warning! Using the -f parameter will delete all the files in the GitHub repository. Use it when starting a new project when the repository has files such as LICENSE and README and doesn't accept the push command.

Updating the project

Go to the GitHub site and create the README file with some content. To synchronize these changes in the remote repository with the local one, go into the project folder and run:

git pull

As mentioned before, when uploading the project with the git push command using the -u parameter, the repository on GitHub will be used as a reference and it will not be necessary to indicate the origin name in the command. The update will be applied to the current local branch.

Check that the README file created remotely is now present in the project's local folder. Edit the file in the local repository, add a new line in README and then:

git commit -a -m "update readme"

git push

The -a parameter automatically prepares all files that have been modified or deleted, but new files will be ignored. Use git add for new files. The -m indicates the commit message. git push updates the remote repository on GitHub with the recent changes in the project's local folder.

Sources

This is not my original language and I don't speak it very well. I used my little knowledge and translators tools to compose the text of this article. Sorry for possible spelling or grammatical errors, suggestions for corrections are appreciated and can be sent to the contact email in the footer of the site. My intention is to share some knowledge and I hope this translation is good enough.