DEV Community

Cover image for Learn Github && Git in an afternoon (Beginners) πŸ±β€πŸ’»β˜οΈ
Jose Antonio Felix
Jose Antonio Felix

Posted on • Updated on

Learn Github && Git in an afternoon (Beginners) πŸ±β€πŸ’»β˜οΈ

If you want to have your code available in a cloud git repository (for free) and be able to collaborate with other developers, this tutorial is for you πŸ˜ŽπŸ’». Firstly, we have to know some concepts:

Git
If you don't know Git (in basic terms), you need to see the post attached bellow before start this tutorial πŸ‘‡πŸ» jafb321

Learn Git in an afternoon (Beginners) πŸ˜ŽπŸ±β€πŸ’»

Jose Antonio Felix ・ Oct 27 '21

#github #git #beginners #code

Github

Github

Is a platform where you can put your code in a cloud git repository completely free and where other developers could see your code and contribute or use it.

Github logo

Github is very useful and is used by a lot of developers in the world. This tool could allow you to improve as developer and be atractive to software companies.

First of all

We'll learn the Github (and some Git) basics in this tutorial, if you already know Git fundamentals (repository, commits, log, etc) you're fine, but if you don't, please check the post attached on the previous "Git" tab. Good luck!🀠

How to use Github

First, we have few things that we should have/know:

Pre-requisites

Now we can start the tutorial properly, next we'll see some sections where we will:

Create a Github repository β˜οΈπŸ±β€πŸ‘“
When you're in the Github main page (logged), you'll see this button:

Github create repository button

Click on it and you'll see this page:

Github repository create page

This page is very intuitive, but I'll explain the elements on it:

  • Repository name: Is the name wich the people will see in your repositories list, find a cool and explicative one.
  • Description: Here you'll explain the project in a few words. Maybe you could write the objective or the tech used on it.
  • Public/Private: If you want to show your repo to other developers, set public (recommended). If you want to keep the project only for you, set private.

The following 3 options can be skipped for now, we just have to click on create repository button:

Github create repository extra options

Now, We have done our repository successfully, we'll see something like this:

Blank github repository

Don't worry if you don't understand what that is, we'll see about it in the next steps. For now, you just need to know that you have your first Github repository! πŸ±β€πŸ‘“

Configure and push your local repository πŸ§ͺ
We already have our Github repository, but also we need a local Git repository on our pc. If you don't know how to create one, you need to see this post.

Okay, the following is an example local repository with 3 files and few commits:

Basic repository project

We have to upload or push our local repository to the Github repository, is like sync both. For this we have to do the following:

Do you remember the github page of our new repository? there we found this commands:

Github repository push commands

git remote add origin URL_EXAMPLE
This git command allow you to add a new remote repository (URL) to your local repository, that means that you could upload your commits (push), update your local repository (pull) and more.

In this case, we're adding a new remote URL_EXAMPLE with the alias origin.

git branch -M main
This command change the branch name of our repository to "main". The reason of the change is because by default git names the branch "master" and Github need a branch named "main" (to avoid racial discrimination of master and slave).
The branch concept needs a more complete explanation to be understood, but for now isn't necesary, just see it like the main name of this repository.

git push -u origin main
This is the interesting thing.

  • The git push command allow us to push (upload) our commits to the github repo.
  • The origin main indicates that we want to push the changes to the remote named "origin" (setted on the first command) and the branch named "main" (setted on the previous command)
  • The -u is an option to set the origin main options to default on the future git push/pulls calls.

Now we know what this commands are for, so we can type them on the terminal an check the results:
First push to github repo
(When you enter the last command a window will open and it will ask you to login to Github, just enter your account and password and continue. This will happen only 1 time)

If you refresh the repository page, you'll see your files and you could check interesting info like the commits made, the languages used on the repo and it's description.

Github repository

Finally, we'll have our repository on Github! And the best part is comming! In the next sections you'll learn what things you can do with Git and Github.

Push new commits πŸš€
In the last section we have pushed our local repository to the Github. Now, both repositories have the same commits. But maybe we want to make more local changes and then commit and push it to the Github repo again. Okay, Let's do it.

First, we'll make some changes:
File change
Then, add the changes and commit it:
Add and commit the changes

Now, we can get the repository log (history) to see the new commit:
Repository log with new commit
As we can see, the new commit was made successfully. But this commit is only in the local repository, and it need to be pushed to the Github repo.

For this, we can type one of the following commands:
git push
or
git push origin main

Both work fine, the difference is that the second one declare explicitly in wich "remote" and "branch" the commits will be pushed. And the first one just take the default settings.

(If you rememeber, this "remote" and "branch" default settings was been setted in the previous section on the git push -u origin main command, thanks to the -u option) 🀯

When we enter one of this commands, we'll get:
New commit pushed successfully message

Now, if we go to the Github repository page again and refresh it, we'll see our new commit(s) 😎.

Clone a repository πŸ‘
In the last sections, we have created new repositories and push our local commits to github, but, if we want to download a Github repository to our local computer, what do we have to do? Well, this process is called "cloning".

To clone a repository we have to know some things:

  • We need the real URL of the repository wich we want to clone. For get it, we have to go to the github repository page and copy this:
    Link to clone a github repository

  • We can clone whatever public repository that we want, this means that we can go to the page of any repository of any user and copy it's link and clone it on our pc, this is awesome 🀩 because we are able to download every open source code , for example we can go to the oficial Microsoft Visual Studio code repository, clone it, change it's code and modify it as we want 🧨

For this example, we'll clone this Example repository. So, go and copy it's URL.

Now, we have to open a terminal and go to the folder where we want to clone the repo:
Terminal in a folder
In my case, I want to clone it in D:\personal-projects> folder.

Now we just have to type this command:
git clone https://github.com/JAFB321/devto-learn-github.git

We'll get something like this:
Git repository clone result

This should have created a new folder called like the repository cloned. In this case, the folder is D:\personal-projects\devto-learn-github.

Then this folder will have all repository files:
Repository folder files

Now we can open a terminal on this folder and type git log --oneline or git status to check the new local repository:
Cloned local repository

Now we can make changes and commit them as we want. But, can we push this new commits to the original Github repo? πŸ€” In this case, you won't be able to do that, Why?🀨 because the repository isn't yours, you don't have permission to push changes to repositories that you aren't the owner (or where you don't have explicit access).

For get permission and do that, we have 2 options:

  • Request write access to a Github repository to the owner.
  • Make a copy of the Github project on your own Github repositories or Fork a project.

Both options are material for another tutorial, we won't see them in this one.

Obviously, if you clone a repository wich you're the owner (like the repo that you created on the first sections) you are able to push your changes to the Github repo without problem.

Update local repo with last changes πŸ”
Imagine you have a Github repo, and you clone it on you laptop πŸ’» and your desktop pc πŸ–₯️. You go to the laptop and do some changes and make a couple of committs, then you push that commits to the Github repo. Now, you have 1 local repository outdated (in your desktop pc).

To update the desktop pc repository, you must pull the new changes from the Github repo.

How to solve the problem

To do that, we'll clone one of our Github repo 2 times in our pc.
We'll create 2 folders to clone the repo 1 time on each folder:
Github cloned repos Folders

Cloning a repo on 2 folders

We have 2 copies of the same Github repository on our pc. I cloned my own repository but you must clone one of your own Github repos (for example the repo you created in first sections).

Now, we'll make some new commits on the example-repo1:
Commits on example repo 1

And push them:

Push new commits of example repo 1

Note that you don't need to type -u origin main because is a cloned repository, and these options were set in the cloning process automaticaly.

Once this is done, we have the Github repository and example-repo1 updated, but example-repo2 is outdated because it doesn't have the 2 last commits.

To update example-repo2 we need to pull this new commits from Github repo. For that we have to type one of the following commands on the terminal in example-repo2 repo:

git pull
or
git pull origin main

Both work fine, the difference is the same of the git push command where origin main are the "remote" and "branch" explicity defined.

When we enter one of these comands, we'll get:

Git pull new changes from github repo

The elements inside blue rectangle are the changes summary of the commits downloaded. If we run git log --oneline we'll see these new commits:

Updated example repo 2 log

Now we have the 2 repositories updated and ready to work 😎. But take care about pull and push your commits on 2+ copies of the same cloned repository, because if you make different local commits on each repo separately and try to push them at same time you could get a merge error πŸŒͺ️, but don't worry there is an easy solution for those kinds of problems wich we'll see on future Git tutorials πŸ±β€πŸ.


Buy Me a Coffee at ko-fi.com

There are a lot of useful and interesting things that you can do with Git + Github, in this tutorial you have learned the most important topics πŸ₯‡.

You have learned a lot but there are still more things to learn wich can help you to be a very efficient developer, contribute to the open source community and grow in the IT world πŸ€–.

git meme
(If you want to understand the meme, don't forget follow me to learn about git branches, merge, fork and more 🎈)

Top comments (5)

Collapse
 
chuksmbanaso profile image
ChuksMbanaso

Thanks for sharing.

Collapse
 
mrdanishsaleem profile image
Danish Saleem

This is great. Thanks for sharing.

Collapse
 
jafb321 profile image
Jose Antonio Felix

Thank you Danish!!

Collapse
 
adal profile image
Adalberto Prado

What a great article!
I wish I had found an article like this when I was a junior developer. πŸ™ŒπŸΌ

Collapse
 
jafb321 profile image
Jose Antonio Felix

Thank you very much Adalberto !! I try to do my best to help all beginning developers πŸ±β€πŸ