Creating a git repository for the first time and share the code in GitHub

I guess all companies in the software industries maintain one or more configuration management tool to manage the ongoing development and testing process. It is important to track changes so that development is consistent among people involved in a development and testing project. GitHub is one platform where developers share their code as a means of open sourcing their code for the world to use and improvise. Some recruiters even even check the developers github profile in order to check their working progress and coding experiences and how developers are involved in the open source community. In this blog, I will show how to create your own repository for the first time and upload the repository to github. So let’s start.

Before moving on, you need to make sure that git is installed, which you can do this by downloading the installation package from the following site for your respective platforms:
https://git-scm.com/downloads

Mac and Linux users can directly use git commands from the terminal while it is advisable for Windows users to use git bash program, which is similar to the Windows Command Prompt but mimics the same environment as that of Linux and Mac terminal. Once we are inside the terminal we will navigate to the folder where our code is located. In my case my work is in the Downloads folder and I will navigate there. For you all you can do the same with your desired location. Before navigating here are three terminal codes that would be useful:

ls                 #used for listing files and folder of that location

cd folder_name     #go to that folder_name

cd ..              #go back

mkdir folder_name  #create a new folder named folder_name

touch file_name    #create a new file named file_name

So I want to upload my ColorGuessingGame code to my own github. So I will navigate over to that folder containing ColorGuessingGame codes. Before we continue, whenever you start working on a new code, always initialize git before you work on creating or modifying codes. This will enable you to track down your own codes and changes and make necessary arrangement if you end up doing nay mistake to your code. In my usual projects, I always start making a new folder (using mkdir foldername) and go to that folder (cd foldername) and initialize git before writing and creating any code file. In this tutorial, I would assume you already have a stable code running but the concept is same all throughout, whether you start your project or upload anytime during the project. Before moving on in the terminal we have to set up our git email and user name by entering the following (use your email and your name instead):

git config --global user.email "syed.r.akbar@gmail.com"


git config --global user.name "Syed Akbar"

So to initialize git, I assume you are in the parent folder containing the codes and so you type the following to initialize git:

git init

You will see a message, which confirms your git has initialized on your parent folder location:

Initialized empty Git repository in C:/Users/junction/downloads/ColorGuessingGame/.git/

You will also see master label in brackets, indicating you are in a master branch. Since I dont have anything to edit and add any code, I will add the codes in the repository by typing:

git add .
You will notice that I have added a dot after a space. This indicates that I am adding all files. You can replace . with a file name that you want to upload. I will come to that later. Next we type the following with a message:

git commit -m "Created the color matching game"

Here the developer is confirming the change and using -m to write additional message in quotes to inform other developers that he made the changes and what changes he made. My terminal showed the following, your one could be have more messages depending on the number of files:

[master (root-commit) 4af77f6] Created the color matching game
3 files changed, 204 insertions(+)
create mode 100644 colorGame.css
create mode 100644 colorGame.js
create mode 100644 index.html

Before heading to github, we will create another file and write something and add and commit the file again. As a heads up we can check the status of our git by typing the following:

git status

It will show the following, which states that we are in the master branch and we have nothing to commit (we committed all files):

On branch master
nothing to commit, working tree clean

Lets create another file called README.md and write something over there using a text editor:

touch README.md

In my README.md file I wrote a title and description of the game. Now you can see the change made in the git by typing the following again:

git status

And the result shows:

On branch master
Untracked files:
    (use "git add <file>..." to include in what will be committed)

        README.md

The above message says that README.md is not being tracked mainly because we haven’t added the file yet and finally commit the file. We can do this by adding that single file or just type git add .. I will just add the single file:

git add README.md

Now if we again type git status, we will see the following:

On branch master
Changes to be committed:
    (use "git reset HEAD <file>..." to unstage)

        new file: README.md

This indicates a new file is added. Now we can commit the file to inform the changes:

git commit -m "Created a readme file"

We have everything set up, ready to be uploaded to GitHub. Now lets head to our github account. For the first time github users, you need to create the account with your desired user name and password. After that we will click the plus sign beside our picture at top right corner and click on New Repository.

In the Create a new repository window, you provide the name of the repository (in this case try to keep the name same according to the folder where your codes are located). I typed ColorMatchingGame in my case. You can write a short description and leaving everything else, click Create Repository.

Github will give the procedure to add the repository to the github. I have covered half of the part. The next we need to type is assign the github repository to our git folder containing the code:

git remote add origin https://github.com/Koshai/colorguessinggame.git

Finally you push the code to the github repository for view and share to the public and you will be asked to type username and password before the files get uploaded:

git push origin master

You will see the messages confirming the upload:

Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 2.06 KiB | 704.00 KiB/s, done.
Total 8 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/Koshai/colorguessinggame.git
* [new branch] master -> master

You can also verify by refreshing the github repository (in my case: https://github.com/Koshai/colorguessinggame). Congratulations, you have your first GitHub repository.

 

Leave a Reply

Your email address will not be published. Required fields are marked *