How To Create a GitHub Repository

Introduction

Recently there emerged a necessity to use Git on a regular basis. First of all we migrated to Git at job. Second I had a git training. And third reason is that sometimes I do programming exercises and I have never uploaded them anywhere so I would like to straight things out.

I have already installed Git and have a registration on GitHub. In this article I would like to show how I first started to work with Git and which problems I came up against.

Git is a control system to manage computer file versions. Mostly it is used in the area of software development for tracking changes in a source code. It also helps to organize work on the code among multiple developers. It is important to mention that Git is a command-line tool.

GitHub is a web-service of projects hosting using Git version control. Developers can store and share their projects there. It is a kind of social network site for developers.

The most interesting thing to me for the beginning is how to create a GitHub repository.

Configure identity on server

First step is to configure my identity for a commit to GitHub repository. Open git bash and type:

$ git config --global user.name "GitHub user name"
$ git config --global user.email "GitHub user email"

First variant how to create a GitHub Repository

Create a new repository on GitHub

Copy the link of a new created project on GitHub

https://github.com/materiaBio/HelloWorld.git is the copied link from the GitHub.

Clone a GitHub repository

Using git bash I need to clone GitHub repository to a directory of my git projects for futher implementation work. For me it is a "D://projects/git".  From the folder run:

$ git clone https://github.com/materiaBio/HelloWorld.git

Now I have cloned a GitHub repository on my local computer in the folder "D://projects/git/HelloWorld"

Git status of the project

$ git status

This command control the status of my developed project which I will periodically commit to GitHub repository. It is seen from the comment that I have nothing to commit.

Create your project in a new cloned GitHub repository

Then I created an idea project in folder "D://projects/git/HelloWorld".
With git status I can see the status of files of my project.

Untracked files means that Git finds files which I haven`t commited yet. It means that these files are absent in previous snapshot. I haven`t done any snapshot yet (commit command) and it logically means that all my files are in the untracked status.

Add the files to index

$ git add

Git will not start including my files in my commit snapshots until I tell it to do so. Before I commit changes to local repository they need to be added to the place called the gitindex. It is a cache where a developer place files for further commit to git repository.

Now files are in staged status that is why index is also known as “staging area”.

Commit to a local repository

Now files are added to index and can be commited to a local repository.

Here I faced two challenges:

1. I want to ignore several files with “.idea” extension. To do this I added a pattern “**/.idea/” to the git exclude file “D:\projects\git\HelloWorld\.git\info\exclude” to exclude “.idea” folder. Don`t forget that “.git” is often invisible folder but it is there in the root of the working project.

2. LF” will be replaced with “CRLF”. It is a “line ending” problem. I don`t share my projects with other developers that is why I can ignore this problem for now.

  • LF (line feed) mostly used in Unix-like systems
  • CR (carriage return) Macintosh
  • CRLF (line feed + carriage return) Windows
Commit  and push the changes

Now I`m ready to commit the project to a local repository and then push the changes to the remote GitHub repository.

$ git commit -m “comment to commit”
$ git push

Let’s have a look at GitHub

Yippeeeeeee, it is there!

 

Second variant how to create a GitHub Repository

Let`s assume that I have already implemented somewhat project code and I want to add it to the GitHub.

First of all I created a repository on GitHub called “SecondProject” and then I needed to upload my existing local project with the same name “SecondProject” to the remote GitHub repository.

Initialize the project

Using Git Bash I run the command from my project directory ("D:\projects\git\SecondProject") :

$ git init

This command creates an empty Git repository or reinitialize an existing one.

Add all files from project to index

The next two commands were explained in the previous part “First variant how to create a GitHub Repository”.

$ git add .
$ git commit -m ‘comment’

Remote address

Add the remote address for a created project with:

$ git remote add origin "GitHub repository link"

The above command creates a new remote repository called origin which is located at https://github.com/materiaBio/SecondProject.git(was copied from GitHub repository). Now I can execute “push” command to origin instead of using the whole URL.

origin is a default name of how Git calls the server from where I can clone the GitHub project

Upload files to the GitHub

Now I can try to upload the files to GitHub with:

$ git push origin master

After this command is executed it is possible to push the commited files from the local branch called master to the remote repository called origin.

Rejected “push” and “pull”

It is seen from the above image that the “push” command was rejected. After reading the comment I decided to execute the “pull” command.

But it failed again with comment: “fatal: refusing to merge unrelated histories”.

Unrelated histories

This error occurred because the commit history of my local project (its files were commited) does not match with the remote commit history. Local and remote projects are disparate what means they “know nothing about each other” and their commit histories are different.

I allowed unrelated histories with the command:

$ git pull origin master --allow-unrelated-histories

By executing this command the remote history first followed by the commit were merged to a local commit history.

Push files to the GitHub

Now I can try to upload the files to GitHub once again with:

$ git push origin master

Yes, it is succesfully pushed to the GitHub repository. Now I can push all changes in SecondProject code to the GitHub whenever I need that.

Continue Reading

Stack Pseudocode

ISEMPTY()
1 if STACK: TOP == 0
2 return TRUE
3 else return FALSE

PUSH(ELEMENT)
1 STACK.TOP = STACK.TOP + 1
2 STACK[STACK.TOP] = ELEMENT

POP()
1 if ISEMPTY() == TRUE
2 ERROR “underflow”
3 else STACK.TOP = STACK.TOP - 1
4 return STACK[STACK.TOP + 1]

PEEK()
1 return STACK[STACK.TOP]

SIZE()
1 return STACK.TOP

Continue Reading