Starting with GIT using GitHub | Create a new repository.| What is GitHub?
Already decided that you want to use the git, a new or an existing project. Where (on 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 and others can access using VPN.
- Use a server in any data center with public IP, the 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 provides other useful features that make git easier and richer. Few are like, WiKIs, Use 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 bigger you may opt for the professional versions.
OpenSource Project and GitHub?
For open-source, it is the best option as no setup cost and is already tested with millions of users.
There are other companies also, that 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.
GitHub is 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 there will be a huge cost to maintain the servers. With GitHub, you have full individual user-level access management to protect the source code.
Github vs own hosting server
|Features||GitHub server||Own Server|
|Cost||Free For Basic usage||Will be some cost|
|GUI||Inbuild||You need to develop|
|Redundancy||Yes||You need more serves|
|Backup||Yes||You need to implement a|
|Use management||Yes||Need 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:
Create a 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:
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 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:
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 done with the git server, we will now care about a project using the git init command. Below we will show how we can add a local project folder in a repository created on Github.
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 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://email@example.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. While the remote head has moved to one (we commit README). To push our changes, we need to synch the local work with the remote. For that, there is a pull command.
If there was no README commit, there was not an 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://firstname.lastname@example.org': 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?
Again login and check the repository
projectsource. You can see that the new file
hello.c is added.
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.