Day One - Basic Repositories Concepts
Always Looking for Something Which Makes me Millionaire by night
Hi Guys,
This is our first day with Github. In this section we will have an introduction of Github + a quick set-up
Agenda
- Lab Setup
- Create a Github Profile
- Configure Git locally and Create a repository
Lab Setup
macOS and Linux users already have Git installed on their local machines.
If you are a Windows user, please connect to the Git site and follow the download/installation instructions.
Now it’s to check if the installation went through. Let’s launch this command from a terminal to see the version of Git we have on our system:
git --version
In this Lab we will use Visual Studio Code as IDE.
Is owned by Microsoft and comes with a lot of integrations with Github.
Create a Github Profile
To create a Github account let’s connect to the Github Web Site..
In this lab we will create a free tier account but if you want you can choose another plan based on your needs.
Let’s Sign Up and enter your data. You will follow an easy step by step registration. You will receive a standard registration mail to your mailbox (as usual when you register somewhere).
When you login, you will be redirected to your Github dashboard where you can start playing.
Install also the Github Desktop, that will help you to connect to your repos and have a graphical user interface to easily interact with Github.
Configure Git locally and Create a repository
In order to get all the advantages of Git, let’s configure it. For sure we need to be recognized so, let’s register our git user. Open up a terminal and launch the following two commands:
git config --global user.name "YOUR USERNAME"
git config --global user.email "YOUR EMAIL"
If you did it correctly, launching the following two commands will identify your user on the system:
git config user.name
git config user.email
We did this configuration in order to be able to understand who did a change. When you do some changes on the repo, the change will be marked with your user making easy to identify who did what.
You will create a repository once per project. In order to create a Git Repositoty you have to launch the following command:
git init
It will create an hidden directory (.git) which holds the info of the VCS. To check the status of the repository you can launch:
git status
We can also use Github Desktop (we installed in the previous steps) to have the same or we can directly create a repository on Github and then launch
git clone
to our local machine.
Hands on! Let’s use the terminal
OK, now that we have understood (more or less) how it works, let’s have a quick example.
Let’s open a terminal and launch the following commands:
cd Desktop
mkdir GitCourse
cd GitCourse
git status
In this way we are creating a folder called “GitCourse” on our Desktop, we are inside this folder and we will receive an alert that will tell us that no repositories are set on this folder. Let’s initialize our repo!
git init
If you list all the folders in our GitCouse folder you can see that a new hidden folder is there!
ls -lrtah
Now if we check the status of our repo, you can see that the message changed! Our Git repo is recognized and we can see that we do not have commits to perform.
Hands on! Let’s try Github (create a public repository)
At the previous step we created a local repository using Git. Now we will try to create a repository using Github. Let’s connect to the Github Web Site and Sign In.
Let’s click on New and you will be redirected to a page in which you can specify the details to create a repository.
You can see the Repo owner and now you have to assign a Repository name. In my case it will be “MyGithubRepo”.
You can also specify some metadata like a description to better identify your Repo and why it was created for. Let’s make it Public and add also a README file!
The .gitignore will identify which types of file you do not want to track. Just right now you can ignore it. We will come back on it later on.
You can also choose which license fit your needs and assign the correct one. In this case just leave it as-is (License: None).
Then press “Create Repository” and….SBAM…we are live with our brand new repository on Github (in this case you can also see that an initial commit was made since we created the README file).
Now, if we want to clone the repo on our local machine, you can simply press the “Code” button, copy the HTTPS url and open up a terminal on your machine. At this time you need to point out where you want to clone this repository. In my case would be the “Desktop”.
cd Desktop
git clone https://github.com/francescovolonterio/MyGithubRepo.git
In this way you have a remote Github Repository cloned in your machine!
Hands on! Let’s try Github (create a private repository)
At the previous step we created a public Github repository that can be accessed by everyone.
If we do the step we did before to create the public repository and we create it as “private”, if we try to clone it to our machine should not work.
This because we need to setup a particular token to allow the connection between our machine and our Github. Let’s see how we can deal with it.
We have two options to operate on private repositories:
- Create a personal Access Token (PAT) using the command line and then cloning the repository referencing this token.
- Use Github Desktop and use our username and password to connect with our Github account.
CREATE A PAT:
If you want to create a Personal Access Token you have to connect to Github, under your profile picture press “Settings” and then press “Developer settings”.
At this point, press “Personal Access Token” and then “Generate new token”. You need to provide your login password again and you will be prompted to the page in which you can create the token.
The cool thing of a PAT is that you can set it up for specific scopes and you can also set it to expire.
In my case I will set the scope to “repo” to hae the “Full control of private repositories” as suggested from Github.
Make sure to copy somewhere your token because you won’t be able to retrieve it again. If you loose it, you have to generate a new one as did it before.
In the example we did before, we use the following command to clone our repo because it was public and so no PAT was needed.
cd Desktop
git clone https://github.com/francescovolonterio/MyGithubRepo.git
Now, with our private repo we need to do the following:
cd Desktop
git clone https://token@github.com/francescovolonterio/MyGithubRepo_Private.git
GITHUB DESKTOP:
Open up Github Desktop and Login (File –> Options –> Accounts –> Sign In). The browser will be opened and you need to authorize Github Desktop to operate on your account.
Now, you can press File –> Clone Repository and you should see the list of the repositories you can clone. Just browse where you want to clone the repo and press “Clone”