Published on

Configure Git and Github in Linux

Authors
  • avatar
    Name
    Cristian Encalada
    Twitter
Different types of programming languages

Git is a very popular version control system.

GitHub is a service that allows you to upload, host, and manage your code using Git with a nice web interface.

Many times there is a confussion between Git and GitHub, but they are not the same thing or even created by the same company.

Table of contents

  1. Install Git
  2. Create a GitHub account
  3. Setup Git
  4. Create an SSH key
  5. Link your SSH key with GitHub
  6. Test your SSH key
  7. References

Install Git

Run any of these commands in the terminal depending of your Linux distribution:

Arch Linux

sudo pacman -S git

Ubuntu Linux

sudo apt install git

Verify your Git version

Make sure your Git version is at least 2.28 by running this command:

git --version

Create a GitHub account

Go to GitHub.com and create a new account

During the account setup, it will ask you for an email address. This needs to be a real email, and will be used by default to identify your contributions.

If you don't want your email address to be publicly available, make sure you tick the following two boxes on the Email Settings page after you have signed in:

GitHub email settings

Having these two options enabled will prevent you accidentally exposing your personal email address when working with Git and GitHub.

You may also notice an email address under the Keep my email addresses private option, this is your private GitHub email address.

Setup Git

For Git to work properly, we need to let it know who we are so that it can link a local Git user (you) to GitHub. When working on a team, this allows people to see what you have committed and who committed each line of code.

The commands below will configure Git. Be sure to enter your own information inside the quotes (but include the quotation marks)!

git config --global user.name "Your Name"
git config --global user.email "yourname@example.com"

If you opted to use the private GitHub email address, the second command will look something like this:

Note: The angle brackets (< >) in the code snippet below indicate that you should replace that part of the command with the appropriate information. Do not include the brackets themselves in your command.

git config --global user.email "<#########+username@users.noreply.github.com>"

Remember to use your own private GitHub email.

GitHub recently changed the default branch on new repositories from master to main. Change the default branch for Git using this command:

git config --global init.defaultBranch main

To enable colorful output with git, type

git config --global color.ui auto

You'll also likely want to set your default branch reconciliation behavior to merging.

git config --global pull.rebase false

To verify that things are working properly, enter these commands and verify whether the output matches your name and email address.

git config --get user.name
git config --get user.email

Create an SSH key

An SSH key is a cryptographically secure identifier. It's like a really long password used to identify your machine.

GitHub uses SSH keys to allow you to upload to your repository without having to type in your username and password every time.

First, we need to check if you have an Ed25519 algorithm SSH key already installed. Type this into the terminal and check the output with the information below:

ls ~/.ssh/id_ed25519.pub

If a message appears in the console containing the text “No such file or directory”, then you do not yet have an Ed25519 SSH key, and you will need to create one.

To create a new SSH key, run the following command inside your terminal. The -C flag followed by your email address ensures that GitHub knows who you are.

ssh-keygen -t ed25519 -C <youremail>
  • When it prompts you for a location to save the generated key, just push Enter.

  • Next, it will ask you for a password; enter one if you wish, but it's not required.

After that, the SSH key is be generated properly and the output should look something like this:

Your identification has been saved in /home/cristian/.ssh/id_ed25519
Your public key has been saved in /home/cristian/.ssh/id_ed25519.pub
The key fingerprint is:

The key's randomart image is:
+--[ED25519 256]--+

+----[SHA256]-----+

Now, you need to tell GitHub what your SSH key is so that you can push your code without typing in a password every time.

First, you'll navigate to where GitHub receives our SSH key. Log into GitHub and click on your profile picture in the top right corner. Then, click on Settings in the drop-down menu.

Next, on the left-hand side, click SSH and GPG keys. Or just click the following link

Then, click the green button in the top right corner that says New SSH Key.

Name your key something that is descriptive enough for you to remember where it came from. Leave this window open while you do the next steps.

Now you need to copy your public SSH key. To do this, we're going to use a command called cat to read the file to the console. (Note that the .pub file extension is important in this case.)

cat ~/.ssh/id_ed25519.pub

Highlight and copy the output, which starts with ssh-ed25519 and ends with your email address.

Now, go back to GitHub in your browser window and paste the key you copied into the key field. Keep the key type as Authentication Key and then, click Add SSH key.

That's all! You've successfully added your SSH key!

Test your SSH key

Copy (clone) an existing github repository onto your local machine, click the green “Code” button.

Then select the SSH option, and copy the line below it. NOTE: You MUST click the SSH option to get the correct URL.

GitHub ssh link

For example:

git clone git@github.com:cristian-encalada/cristian-encalada.git

Now, after any change you can push your code without typing in a password every time.

Thanks for reading!

References