A Complete GitHub Tutorial for Beginners…
Hello everyone, In this Github tutorial we will be learning about Github, how to create repository in Github, how to push code to Github and How to pull code from Github. This github tutorial is designed exclusively for people who want to start the topic from scratch. In this Github tutorial we will be covering github, however, to make the most out of this github tutorial we recommend you to take the course on git here before moving to this Github tutorial. We have explained the concept of branches, commit, repository, push and pull in more detail over there. However we will also be covering those things in brief in this github tutorial also.
Though in this tutorial we will be talking about Github only, other sites like Gitlab and Bitbucket work in a similar way, so if you are comfortable working with Github you can easily migrate to Gitlab or Bitbucket. In addition, Github is also the most widely used web-based hoisting service for version control using Git and almost all of the open source technologies have their repositories on Github because it has been providing a culture and environment for open source since its inception.
To continue make sure you have an account on Github.
Okay Now Let’s Start with Github Tutorial:
1) Creating Repository:
A repository is just a place where you store all of your code belonging to a particular project. A repository consists of various files and folder which house your code. You have various repositories associated with you account, each of which houses a different project. You can also clone someone else’s repository, and then a copy of that repository is associated with your account too where you can make changes to that repository.
To create a repository on github-
- Go to your Home Page
- Click on the “+” icon in the Navigation bar as shown in the figure below
- Click on “New Repository” in the dropdown menu
- Now, Enter a Repository name “newrepo”, say. Add an optional description. Select if you want your repository to be public or private. For now, check the “Initialize this repository with a README” and then click on “Create repository”.
A public repository basically means that anyone can access or read your code. A private repository on the other hand does not show your code to anyone except the collaborators.
Congratulations!! We just created a Git Repository on Github.
The link highlighted in this image is the link of your remote git repository to which you can push code from your local copy of git repository. If you would like to take a look at our tutorial for git and how to push it on remote repository you may consider the following link.
2) Adding Code
Over time you may need to change the source code of your files. You can add code to your files, remove it or edit it using the Github User Interface. It is literally as simple as it sounds because of the nice User Interface Github provides to its users.
To add create a new file-
- Go to the repository in which you want to create a new file
- Click the “Create new file” button as highlighted in the figure.
- Enter the full name of your file (including the extension, if required).And start writing the contents in the file.
- Add a commit message(this is compulsary for every commit you make) and click on the “Commit new file” option.
Congratulations. You just added a file to your github repository and created your first commit in this Github repository (2ndactually because the 1stcommit is made when you create a new repository).
In a similar fashion you can add or remove code from your files by clicking on the file, changing the code, and making a commit.
3) Creating Branch
A branch allows you to work on different versions of the same project at the same time. This allows users or developers to try new changes on a different branch and merge them with master branch (which is the main branch) if those changes are required. If those changes are not required then the main branch of your repository, i.e. the master branch is still safe as the changes were never made to the master branch.
If you would like to know in detail about master branch git merge and branches, you can check our git tutorial here.
- To create a branch, click the button which shows “Branch: master” and enter the name of branch you want to create say, “branch1”. And then click “Create branch:branch1”
- Congratulations!! You just created a new branch “branch1” and are now switched to it.
- To change branch, Click on the Branch button again and select the branch to which you want to move to.
The changes you make in a branch are present in that branch only and will not be shown in other branches unless you merge that branch with another branch.
4) Cloning Repository
In this section we will discover how you can make a copy (or clone) a repository on Github into your computer. It is a very useful operation.
To clone a Github repository into your system-
- Copy the URL of your remote git repository (make sure it is HTTPS only and you are not using SSH)
- Go the the local git repository you want to push to Github and open it in terminal/command prompt.
- Enter the following command in your terminal.
Git clone <url of remote repository>
Congratulations!! You successfully cloned a git repository into your computer
5) Pushing Repository
Pushing a repository to your Github account is another useful operation you can perform. Basically, Pushing code means that you made some changes in your local copy of the repository and you want the remote repository (say your Github repository) to reflect the same changes. If you wish to have a deeper know how of pushing a repository and other concepts of git, you can check it out here.
To push a repository-
- To push a local repository to a remote location you first need to give the location of remote server. This is automatically set if you cloned the repository from a remote location. However. If you did not clone the repository from a remote server, you can simply add the location of the remote server by the following command.
This command tells git to add a add the location of the remote server provided as the last argument as origin. So origin refers to the location of remote server where repository is to be pushed. Though we can replace origin by any other name you feel comfortable, it is a standard to name it origin.
git remote add origin <location of remote server>
- After this you simply need to enter the following command to push the repository to a remote server.
You will also need to enter the branches you need to push to remote server. Enter master if you want to push the master branch only, or other branches you want to push to your remote server. If you want to push all the branches you can enter –all or -A in place of branches
git push origin <branches you want to push>
Hurray! You just pushed your repository to github.
6) Pulling Repository
Pulling a repository, as it may sound, means the opposite of pushing which means that we want the changes in our remote repository (say Github Repository) to reflect in our local repository.
To pull a repository-
- We pull repository when we want to update our repository with the latest changes in the remote repository.
- To pull a repository, just open up your command prompt or terminal and write
git pull origin
this will update you local repository with the changes in the remote repository.
And you just completed pulling a repository from a remote server.
7) Fork Operation
Forking operation allows you make a copy of someone else’s repository and associate it with your own account. Then you can make changes to this copy of repository, add or remove code from it and do all things with it you can do a repository you own (Laughs in Silence!!).
To fork a Repository on Github-
- Go to the repository you want to fork to your account. In this example we will fork this repository https://github.com/ethereumbook/ethereumbook.
- Click on the “Fork” button on top right.
- And you have a copy of that repository in your account which you can check by going to the “Repositories” section of your Account.
That’s it Guys in this Tutorial,
So in this Github Tutorial we covered basics of Github, how to create a repository on github, how to create and edit files in a repository, how to creating branches on github and how to switch between those branches, how to clone a github repository into your computer, how to push a repository to github, how to pull a repository from Github and how to fork a repository on Github. In next tutorial we will be working on how to make a Pull Request or PR on Github which by the way is a very important topic and we would not like anyone of you to miss that topic. It is a very crucial part of how many organizations and open source organizations work.
Further, you can also read about how these operations are used in professional world over here.
Though this Github tutorial was designed make basic operations of Github as simple as possible, to get a deeper understanding of working of Github, you must also know working of Git which you can find over here.
Of course we would love to hear from you, you can leave a comment in the comment section below or contact us regarding a suggestion, an improvement, a doubt or just a random “hi”.