Starting with GIT using GitHub. How to create a new repository, and What is GitHub?


If you already decided that you want to use Git for a new or an existing project? Where (on the git server) to put the project repository? So that there can be secure access to the developers or contributors. There are various options for a git server.

  • Install a git server on a local machine in the office LAN so employees can access it using a VPN.
  • Use a server in any data center with public IP. Then no need to have a VPN.
  • Use a cloud server, e.g., AWS.
  • You can use a service from another company that provides only servers for git repository hosting. E.g GitHub and BitBucket.  In this tutorial, we will describe GitHub only.

What is GitHub?

As per the source, GitHub is a company in the United States that provides hosting servers for Git, a version control system. Now it is part of Microsoft after 2018. Along with the server, GitHub offers other valuable features that make it easier and richer. Few are like, WiKIs, User level access control, defect tracking, etc.

For the cost, the company offers free and paid plans. For starters, free plans are suitable. When work gets more extensive, you may opt for a professional version.

OpenSource Project and GitHub?

Open-source is the best option as there is no setup cost, and millions of users have already tested it.

There are other companies also those are providing repository servers for Git. Bitbucket and Github are famous ones. In this tutorial, we will describe GitHub only. We will use the basic, which is free.

Is GitHub a git server only or much more?

Other than the git server, a  company does many other things. E.g., failovers and regular backups. So that you do not need to worry about disaster; if you have it all on your own, maintaining the servers will be a considerable cost. With GitHub, you have full individual user-level access management to protect the source code.

GitHub vs. own hosting server

Features       GitHub serverOwn Server
CostFree For Basic usageWill be some cost
GUIInbuildYou need to develop
RedundancyYesYou need more serves
BackupYesYou need to implement a
Backup Procedure.
Use managementYesNeed to add

Let’s start creating a repository:

First, create an account with the email address of GitHub. It is a simple process, as we have with any other web service. After login, you will see your dashboard as follows.

Login to Github:

Login to github

Create a New Repository Step One :

Create New Repository step one

Click on the + sign in the top right corner. There will be a list from the popup menu. Click the New Repository.

Create a New Repository Step Two:

Create New Repository step 2

While creating a new repository, there is multiple information we need to provide. The first is the name of the repository. We have given the name project source to our new repository.

For access to use level access management, we can choose the public or private. If it’s private, you can choose who can see the repository on his dashboard after login and also need to grant permission to commit or modify.

With the Public, all can see, but you can choose who can commit.

We have added a README file at the time of creation.

Create a New Repository Step Three:

Create New Repository step 3

After clicking on the create repository, we can see that repository has been created. It is with a single commit for the README file.

How to add a project to GitHub using the Linux command line?

After being done with the git server, we will now care about a project using the git init command. Below we will show how to add a local project folder to a GitHub repository.

Initialized a local project folder as git repository:

[root@CentOs7 cspsprotocol]# mkdir myFirstProject
[root@CentOs7 cspsprotocol]# cd myFirstProject/
[root@CentOs7 myFirstProject]# git init
Initialized empty Git repository in /home/cspsprotocol/myFirstProject/.git/

Create a source file hello.c and add to the repository. 

  • vim hello.c – Create a hello.c source file
  • git add hello.c – add the file to the empty repository. Optionally we may also use Git add all command if there are multiple files and we want to add all of them for the initial commit.
  • Git commit -m “Commit for first source file – Do the initial commit. With -m, we can give the message to our first commit.
[root@CentOs7-181 myFirstProject]# vim hello.c
[root@CentOs7-181 myFirstProject]# git add hello.c
[root@CentOs7-181 myFirstProject]# git commit -m "Commit for first source file"
[master (root-commit) 26592d3] Commit for first source file
1 file changed, 1 insertion(+)
create mode 100644 hello.c

Add the local git project into GitHub repository projectsource.

[root@CentOs7-181 myFirstProject]# git remote add origin https://github.com/cspsprotocol/projectsource.git
[root@CentOs7-181 myFirstProject]# git push -u origin master
Username for 'https://github.com': cspsprotocol
Password for 'https://cspsprotocol@github.com':
To https://github.com/cspsprotocol/projectsource.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/cspsprotocol/projectsource.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

We can see in the above output that while adding the project, there was an error. This is because the local repository head is at zero. At the same time, the remote head has moved to one (we commit README). We need to synch the local work with the remote to push our changes. For that, there is a pull command.

If there was no README commit, there was no error.

Bring changes to the local project and make the repository update.

[root@CentOs7-181 myFirstProject]# git pull https://github.com/cspsprotocol/projectsource.git
From https://github.com/cspsprotocol/projectsource
* branch HEAD -> FETCH_HEAD
Merge made by the 'recursive' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 README.md

Send the local changes to the GitHub.

[root@CentOs7-181 myFirstProject]# git push -u origin master
Username for 'https://github.com': cspsprotocol
Password for 'https://cspsprotocol@github.com':
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 562 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/cspsprotocol/projectsource.git
bccf0d7..cf3ea37 master -> master
Branch master set up to track remote branch master from origin.
[root@CentOs7-181 myFirstProject]#

No errors and changes were sent to the remote master branch.

How to Check and verify for updates on GitHub?

Check and verify changes on github

Again login and check the repository project source. You can see that the new file hello.c has been added.

Conclusion: 
You have learned how to create a project repository on GitHub and initialize it with a local project. Now you can invite other developers or contributors to work on the project.