Git is a distributed version control system. It is primarily used for software development. So, here we will discuss how to create a new git repository and push it to GitHub. Before that, I would suggest you to go through the following article to know more about version control systems.
What is Git?
Git is an example of a Distributed Version Control System. It is the most widely used modern Version Control System in the world developed by Linus Torvalds in 2005 for the development of the Linux kernel.
In addition to being distributed, Git has been designed with performance, security and flexibility in mind.
So, with Git, you can easily follow your source code’s revision history and track changes. It is possible to find out how the version has changed and who has made the changes. Also, when the latest version of a file is on a shared repository or central repository, Git will prevent unintentional overwrites by anyone on your team who has an older version of the file.
The core of Git was originally written in the programming language C, but Git has also been re-implemented in other languages, e.g., Java, Ruby, and Python.
What is GitHub?
GitHub is a web-based Git repository hosting service, founded in 2008. It has been recently acquired by Microsoft. GitHub offers all functionality of Git as well as adding its own features. It hosts remote repositories and allows code collaboration with anyone who has an access to the GitHub. It’s free to use to host public repositories and has an Enterprise version like most open source applications.
There are around 85 million repositories hosted on GitHub, and 28 million developers contribute to them.
This post discusses how to create a new git repository and push it to GitHub.
- Git installation
- Creating GitHub Account
Debian and its derivatives
sudo apt-get install git
Fedora, RedHat and its derivatives –
yum install git
Setting up GIT
[email protected]:~> git config --global user.name "admin" [email protected]:~> git config --global user.email "***@thegeeksalive.com" [email protected]:~> git config --global core.editor vi [email protected]:~> git config --list user.name=TheGeeksAlive firstname.lastname@example.org core.editor=vi [email protected]:~>
The above details are stored in a file called “.gitconfig” which you can edit manually to add different configurations. The exact path to this file is “~/.gitconfig” i.e, under the user home directory. Please make sure that you’re following the syntax and formatting properly while adding the contents.
Creating GitHub Account:
Now, go to the GitHub site and signup. GitHub accounts are free for public repositories but there will be a nominal charge for setting up private repositories.
A repository is just a directory in your project’s root directory. A Git repository contains the history of a collection of files. We can create a new repository or copy an existing repository. The process of copying an existing Git repository via the Git tool is called cloning. When we clone a repository (stored in a remote location), the complete repository with its history will be copied to our local machine. This is usually known as a local repository.
To remove/delete a Git repository, you can simply delete the folder which contains the repository.
Creating a local repository:
We have to create a directory on your local machine and then convert it as the Git local repository.
[email protected]:~> mkdir myrepo [email protected]TheGeeksAlive:~> cd myrepo/ [email protected]:~/myrepo>
Next, we will initialize the above directory as a repository. We will execute the “git init” command in the root of the new directory to do this.
[email protected]:~/myrepo> git init Initialized empty Git repository in /home/admin/myrepo/.git/
Now, we will create a file in the directory with touch command or with any of your favorite editors.
[email protected]:~/myrepo> vi testfile [email protected]:~/myrepo>
Git will be aware of the changes happening in this repository. You can do a “git status” to know the status of your repository.
[email protected]:~/myrepo> git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) testfile nothing added to commit but untracked files present (use "git add" to track)
This means that git has noticed the creation of a new file called testfile but unless you execute “git add” command git is not going to do anything with it.
[email protected]:~/myrepo> git add testfile [email protected]:~/myrepo> git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: testfile
So, by doing “git add” we are actually marking that file to go to the staging area (Index). All files that are moved to this staging environment are then committed into the Git repository using the “git commit” command.
syntax: git commit -m "<commit message>"
[email protected]:~/myrepo> git commit -m "first commit" [master (root-commit) b190ab6] first commit 1 file changed, 1 insertion(+) create mode 100644 testfile
Committed means that the data or that particular version is safely stored in the database.
Pushing the local repository to the GitHub:
The next step is to push this newly created local repository to the Github so that it will be available for the public. By doing this, they can see our code, pull it to their system modify it and push it back. The GitHub push is recommended only if we need to work collaboratively on the code.
So, go to GitHub and create a new public repository there.
On clicking the “Create repository” button, the GitHub will ask if you want to create a new repo from scratch or if you want to push an existing repo you have created locally.
Since we have already created a new repo locally, we want to push that onto GitHub. So we will follow the “….or push an existing repository from the command line” section.
In order to push the local repository to our GitHub repository, we need to set the remote URL of the newly created GitHub repository to our local repository. The command “git remote -v” will show whether our local repo is linked to any remote URL.
[email protected]:~/myrepo> git remote -v [email protected]:~/myrepo>
This means, by default the remote URL is not setup for this local repository. So, we will setup the remote URL using the “git remote add origin” command.
[email protected]:~/myrepo> git remote add origin https://github.com/thegeeksalive/myrepo.git [email protected]:~/myrepo> git remote -v origin https://github.com/thegeeksalive/myrepo.git (fetch) origin https://github.com/thegeeksalive/myrepo.git (push) [email protected]:~/myrepo>
Now, we can try pushing the contents of our local repository “myrepo” to the remote GitHub repository.
[email protected]:~/myrepo> git push -u origin master Username for 'https://github.com': <username> Password for 'https://<username>@github.com': Counting objects: 3, done. Writing objects: 100% (3/3), 224 bytes | 224.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/thegeeksalive/myrepo.git * [new branch] master -> master Branch master set up to track remote branch master from origin. [email protected]:~/myrepo>
The username and the password is the one we created during the GitHub signup process.
That’s it!!. All the contents on your local repository are now pushed into your GitHub repository. We will discuss more git operations in the future posts. If you have any questions, feel free to leave a comment below.