How to Install Git on Linux and Connect to GitHub via SSH
In this lesson, we're going to walk through how we can configure git on our virtual machine and also connect it with github. As you go through any of the courses that you're going to follow along with you will discover that you need to use version control.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now, this is not an entire section dedicated to how to use git. I'm only going to show you a few basic commands because I have an entire course dedicated to git. This is simply going to show you how to configure it and get it working on your system.

If you do not have a github account already or github.com account and then go to github.com and sign up for a free account and then come back and we'll continue with what we're going to build out.

So to start off I'm going to come over into the terminal you alternatively can just type alt + tab and if you have it open then it's going to pop open the terminal just like this

large

and I'm going to create a project's directory here so I can type mkdir which stands for make directory, projects then I'm going to cd which stands for change directory into the projects and then let's just create another directory and this is going to be at test one. So I'll say mkdir and we'll say hello-git just like this and we'll change into our hello-git directory.

mkdir projects
cd projects
mkdir hello-git
cd hello-git

Now, what git allows us to do is to create code repositories that are on our local machine and then can also be shared on a public site like this. And if you go through the course then you're going to become familiar with exactly how to work with it.

But before we can do that we actually need to get git installed on our systems. I'm gonna give us some more room here and usually the way that you would do this is by typing git init and it says what we would expect. We don't have git on the system yet. So it gives us all error message.

large

I did this on purpose so that you could see that Linux is very helpful with allowing you to install dependencies and other kinds of programs on your system. So right here if I type out what it says because it says "The program git is currently not installed you can install it by typing sudo apt install git". So that's what I'm going to do.

So sudo apt install git it's going to ask for my system password so I'll type that out and what it's going to do is going to go out and pull down the git software and install it on the system for us.

large

So type y and hit enter and now it's going to install all of the dependencies on our system so that we can work with git.

large

So this is a very helpful way of being able to install all kinds of different code libraries and applications and so you're going to be typing app git quite a bit as you work with Linux and this is also going to help you as you progress on your development journey. Because if you work with diploid applications and you deploy them on your own servers usually those are going to be Linux based servers and so all of the commands that were typing right now are going to be the same exact commands that you're going to type and that you're going to go through when you configure your own systems.

So now that we have git installed let's try that same command. I'm going to try git init once again and now if I hit enter now it worked and it says initialized empty git repository in this location.

large

so I'm going to simply type out a single file and so I can type vim and actually this will be another good lesson. Because we do not have vim installed yet. So if I type vim README.md and make sure that you type out exactly like this.

vim README.md

This should give us the same error. Yes, it says a program vim can be found in the following packages and so now we can do the same thing. Type sudo apt install. Just a word just get type the regular vim

sudo apt install vim

and hit enter and will type y again

large

and it's going to go install vim and if you've never heard of vim it is a text editor that you can use inside of the terminal and it's not necessary to learn vim even though I absolutely love it and I use it on a daily basis. It's not necessary that you need it for any of these courses but if you are interested I do have a course on vim as well. I'll explain the commands to type out right now if you've never used them before.

So now I can type vim README.md and then type enter. And now this is going to open up a text file for me right here and if I typed the letter I it'll switch into insert mode.

large

I'm going to type a hash mark and then just say Hi Git just like this.

#Hi Git

Then type the escape character and then colon followed by W and Q

:wq

What that means is it's going to save and then close this file and so that is going to get us back here

large

and now if I type git status this is going to tell us that we have an untracked file right here.

large

Now if you're following along and this is just coming completely out of left field for you do not worry. Like I said this is not a course on git this is simply showing you how to install it on your system. Later on, you'll learn exactly how to use git and version control and all of those kinds of things. But for right now as I'm going through these steps each one of these is ensuring that git is working on your system. So if you're not understanding what's going on right now don't worry just follow along and as long as it's working and you're not getting any errors that means that you're setting up your system right. And later on, you'll get to the point where you understand this.

So now that I've typed this out and it's showing me that we have untracked files which is that ReadMe file. That means that I simply need to add this to my commit so that means I'm going to add it to the project and let git know about it. So I can say gt add and then a dot. And now if I type git status it is going to now be green and it's because we added it into our git repository and now it's ready for us to commit.

large

I'm gonna type control L which is going to clear the screen and take my cursor up to the top. And now I can type git commit dash m

git commit -m

and then inside of quotation marks. I'm going to add a commit message, where I'm just going to say "Initial commit" and the quotation marks hit enter and now lets see what it's saying.

large

So now it says Tell me who you are. So this is a good thing if you have never installed git on your system and because I'm working with a completely fresh install it wants to know exactly who we are. So the way that we can do that is by typing git config --global and then user.email and then type in whatever your email is so I'm just gonna say jordan@devcamp.com and then ask us to do the same thing for our name and for this one I'm just gonna get rid of where it said email I'll say name and them do the same thing here with my full name. Hit enter and that's done.

large

And so now if I go and I type the same message so this git commit -m "Initial comment". And also if you're curious how I'm getting back to these commands if you just hit the up and down arrows that allows you to access your history of everything you've typed inside of the terminal.

So, in this case, I just want to git commit -m once again. And now if I do it it worked.

large

And so hopefully you can see as I'm going through each one of these steps it's perfectly fine when you run into errors. I've been doing this for well over 15 years I run into errors every single day throughout the day. You're going to be running into errors constantly. And part of the reason why keep the errors on here is because there's a good chance that the error that I run into you also will run into and if you can watch me work through them and see the logic behind the error and also how to fix it. Hopefully, they'll help you understand how to configure your systems and work through bugs yourself.

So now that we have that we are ready to connect our system with github. So we have everything in this project this hello git project. And now we're ready to push this up to our profile. So if you are loaded in your account you can click on plus click on new repository

large

and I'm just going to say hello-git now. This is the same name that I gave this in the terminal in our local machine but it doesn't have to be. You could call this a thing that you want it to be but I usually like to keep the naming similar and the reason for that is because there have been many times where I will have an old repository here in github that I find that I want to work on again and if I have the naming different it's going to be very hard to find on my local machine. So that's a reason why I usually like to have the name the same but it doesn't matter for the sake of it actually working.

So I want this to be public. If it's private then you're going to have to pay for a private repo. I do not want to initialize it with a readme notice that this is the file that we already created. So I'm going to click Create repository

large

and as you can see right here you have a number of different options and sets of instructions

large

and as you may or may not notice if you are very detail oriented. I have actually come back and I've redone this part of the video. I originally built out the rest of the guide using HTTPS and then I decided I wanted to change it up and show you ssh is the more secure protocol it does require more steps. But I figured that it would be better to do that. It will take us a little while longer but from now on you are going to be able to use this on all of your projects with your virtual machine.

The first thing that we need to do before we can set this up is we need to tell github about our machine. And so the way we're going to do that is we're going to create a public key so that whenever we push from our machine here to github it's automatically going to know us. And so this is going to be a much more secure way of doing it.

If you do not want to follow along then as soon as we perform this next step which is to create our public key and set it inside of github then you can just follow along using HTTPS. One of the other reasons why I wanted to do this is because if you ever want to use two-factor authentication then you're going to have to use the SSH. And so I didn't want to shortchange you and I felt bad after I finished the guide because I felt like it would be better to give you the full and most secure way of doing it. So I am back here and we're going to do it.

Now in order to set up your SSH key, we're going to go up here to your icon and then click on settings.

large

Now, this is going to give you all of your various settings that you have on your system and the next thing you're gonna do is click on SSH and GPG keys.

large

Now I'm going to blackout part of the screen here because I can't show you all of my SSH keys and everything like that because I have done work for a number of organizations that require security and that I have confidentiality agreements. So I click on this blackout part of the screen and then I'm going to show you obviously the elements that you need so if you right click right here it's going to give you instructions on how to generate an SSH key. So open this up in a new tab

large

right here gives you a little guide on what all of SSH is and so I definitely recommend that you explore it and kind of learn a little bit more about it.

large

But we only need it for a few things. So the very first thing is it's going to ask you and check to see if you have any existing SSH keys. Now you can click on this and you can run this command.

large

But if this is a new virtual machine that you're going to use then you shouldn't have anything on it. So if you paste this in and we're going to paste with control+ shift + V. Not just control + V. And if you hit enter you can see that we don't actually have any full public keys so we can run it again in a little bit. If you have a public key it's going to look something like this where it has the extension of pub at the very end of it.

large

But like you saw we didn't have that. So since we do not have any existing keys we need to create a new one so I'm going to generate this one and it gives you all of the instructions here

large

so I'm going to copy this code all the way up to where it says your_email@example.com. Copy that and let's paste it in and then I'm going to type my email. You type your email into it and then hit return. It's going to generate it and it asks what file to save the key to.

large

just hit enter because we don't need to give anything besides the default. It's going to ask for a key phrase I'm not going to type any key phrase in. Just hit enter, the same passphrase again, hit enter again and it has generated the key for us.

large

So that is all that you need to do.

Now that we have that. If you were to go back and type in this ls -al ~/.ssh command now you're going to see that we have an id_rsa and an id_rsa.pub key right here.

large

That is exactly what we need.

The next thing we need to do is we need to add this to the SSH-agent I'm going to copy this and then paste that in.

large

And then also add this. So copy this.

large

And so that identity has been added

large

Now we can add it to our github account so this can give you all of the different instructions you'll need.

large

it knows that we're on a Linux machine and so this shows you how to copy this on your clipboard. So there are a few ways you could do this but they give you some nice instructions and so in case you're following along and you have it on mute or anything then I'm going to just use these so I'm going to paste this in.

large

And what this is going to do is it's going to install the library xclip. And so this isn't related to git or github this just gives us access to the clipboard so type in our password it's going to go install it on the system.

large

And now that it's there now we can copy this. So we're going to say xclip select this clip and this is going to be copied to our system clipboard so I'll copy this.

large

Switching back paste this in. So now it's copied on the clipboard and now what we can do is switch back and you can follow along with these instructions but you can also follow along as I'm doing it.

large

I'm gonna black out part of the screen again. And now I'm going to click on this new SSH key

large

So here I'm going to give it the title of devenvcourse and then you should be able to click here and I'm going to black this out just because you should only give your key out to people who really need it and in this case I'm giving it to github. And so I'm just going to paste it in assuming that you just pasted in a very long string with your email at the very end of it. That means that yours is working correctly and so I'm going to click on add SSH key

large

If everything works then we shouldn't get any errors and the next page should have whatever you named it and the fingerprint listed there at the bottom. So if all of that worked we can now come back to our project. So the way you can do that is by clicking here click on profile

large

and then from here you can click on repositories

large

and I'm going to click on hello-git

large

and click on whatever one you're wanting to add. And now we can use SSH to push our code up. So I'm going to come back to the terminal control + shift + v to paste this in. Hit enter and then because we have our SSH keys installed github now knows about our computer.

large

so it knows that we are authenticated and you can imagine that this is kind of almost like we gave a very secure password to github and it's a password just for our computer so we don't have to type it in. We are able to have our virtual machine have that connection with github and so if I just click the hello-git name again. Now you can see we have that code that read me file that we created right here.

large

If you click on raw it'll show you the actual raw little hash symbol and then Hi Git.

large

large

and that is it.

So if you went through that congratulations you have a full professional installation of git and github on your system including the full SSH key integration.