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:
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 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.Git
If you don't know Git (in basic terms), you need to see the post attached bellow before start this tutorial ππ»
Learn Git in an afternoon (Beginners) ππ±βπ»
Jose Antonio Felix γ» Oct 27 '21
#github
#git
#beginners
#code
Github
Github
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:
Click on it and you'll see this page: This page is very intuitive, but I'll explain the elements on it: The following 3 options can be skipped for now, we just have to click on create repository button: Now, We have done our repository successfully, we'll see something like this: 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! π±βπCreate a Github repository βοΈπ±βπ
When you're in the Github main page (logged), you'll see this button:
Okay, the following is an example local repository with 3 files and few commits: 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: In this case, we're adding a new remote Now we know what this commands are for, so we can type them on the terminal an check the results: 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. 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.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.
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. 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.
git push
command allow us to push (upload) our commits to the github repo.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)-u
is an option to set the origin main
options to default on the future git push/pulls calls.
(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)
First, we'll make some changes: Now, we can get the repository log (history) to see the new commit: For this, we can type one of the following commands: 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 When we enter one of this commands, we'll get: Now, if we go to the Github repository page again and refresh it, we'll see our new commit(s) π.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.
Then, add the changes and commit it:
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.
git push
or
git push origin main
git push -u origin main
command, thanks to the -u
option) π€―
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: 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: Now we just have to type this command: We'll get something like this: This should have created a new folder called like the repository cloned. In this case, the folder is Then this folder will have all repository files: Now we can open a terminal on this folder and type 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: 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.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".
In my case, I want to clone it in D:\personal-projects>
folder.
git clone https://github.com/JAFB321/devto-learn-github.git
D:\personal-projects\devto-learn-github
.
git log --oneline
or git status
to check the new local repository:
To update the desktop pc repository, you must pull the new changes from the Github repo. To do that, we'll clone one of our Github repo 2 times in our pc. 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 And push them: Note that you don't need to type Once this is done, we have the Github repository and To update Both work fine, the difference is the same of the When we enter one of these comands, we'll get: The elements inside blue rectangle are the changes summary of the commits downloaded. If we run 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 π±βπ.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).
How to solve the problem
We'll create 2 folders to clone the repo 1 time on each folder:
example-repo1
:
-u origin main
because is a cloned repository, and these options were set in the cloning process automaticaly.example-repo1
updated, but example-repo2
is outdated because it doesn't have the 2 last commits.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
git push
command where origin main
are the "remote" and "branch" explicity defined.git log --oneline
we'll see these new commits:
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 π€.
(If you want to understand the meme, don't forget follow me to learn about git branches, merge, fork and more π)
Top comments (5)
Thanks for sharing.
This is great. Thanks for sharing.
Thank you Danish!!
What a great article!
I wish I had found an article like this when I was a junior developer. ππΌ
Thank you very much Adalberto !! I try to do my best to help all beginning developers π±βπ