Home » Source Code Management ( SCM ) » Git » How to Create Your First Git Repository ?

How to Create Your First Git Repository ?

As we seen in our previous post, “Install Git On Ubuntu” we are now ready with basic command required to start developing using git. In this post, we will create our first git repository without going into much details and with using very few simple commands which you can remember easily.

$ mkdir git-helloworld
$ cd git-helloworld
$ git init
Initialized empty Git repository in /home/devlab/git-helloworld/.git/

“git init” command creates an empty Git repository [Read more about git init at “Understanding ” git init ” command] – basically a .git directory with sub-directories for objects/database will be created. You can check the created directory as,

$ ls -al
drwxr-xr-x 7 devlab devlab 4096 Jun 18 00:11 .git

Now, we will create a simple helloworld.c C program which we will add to this git repository for the tracking of development of source code.

$ vim helloworld.c
#include <stdio.h>

int main(int argc, char **argv) {
        printf("This is C helloworld from Git Repository");
        return 0;
}

In above command, we just created source code files. Git automatically doesn’t comes to know what changes you have made inside git repository, and we have to specifically inform git to add those source code to its database for tracking.

Now, lets see what Git shows us, after we created C code. This can be seen by using “git status” command. ( Git Status shows the working tree status )

$ git status
On branch master

No commits yet

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

	helloworld.c

nothing added to commit but untracked files present (use "git add" to track)

As, we can see above, git shown us that file we created is still “Untracked Files”, so now we need to inform git to start tracking that file. This can be done by using “git add” command. ( git-add – Add file contents to the index )

$ git add helloworld.c

If you have lot of files or sub directories inside your git, you can add all files & directories by using “dot” instead of individual files which inform git to add all files and directories inside current directory. For example: ” git add .

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   helloworld.c

Using above command, we just inform git that there is some files which you need to track, but have not informed git that what is the purpose of this file, when we have created this file, Who is the author of this file ? For this follow our another post from “4. Configuring User Name, E-mail and Default Editor in git for first time use”

As you may have seen how to inform git about Author, we will just use those commands here as,

$ git config user.name "Developer Name"
$ git config user.email "developer@yourcompany.com"
$ git config core.editor vim

The above details should get reflected in “git config –list” command as,

$ git config --list
color.ui=auto
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.editor=vim
user.name=Developer Name
user.email=developer@yourcompany.com

Now, we are ready to inform git, everything is done and save this code to your database. This procedure is call “commit” . Since we have not committed to git anything yet, lets verify what is the database state of the git as,

$ git log
fatal: your current branch 'master' does not have any commits yet

The “git log” command informs us what are the previous commits if we have saved anything previously. Since this is fresh git, it showed there is nothing. Now, we will save our changes. This can be done using “git commit” command, and argument “-m” informs the message we want to display.

$ git commit -m "This is first commit with helloworld.c"
[master (root-commit) 76eb2de] This is first commit with helloworld.c
 1 file changed, 6 insertions(+)
 create mode 100644 helloworld.c

Now, we have saved our file, lets see what is the git status.

$ git status
On branch master
nothing to commit, working tree clean

As we can see above, there is nothing to commit and current directory is clean with no changes, but it also showed us that, it in on “Master” branch. You can visualise this git “branch” which branches of tree, means you can have multiple different copies with different changes on different branches.

The current working branch can be seen as,

$ git branch
* master

Now, as we have committed our first code into git, we can see the history which git will save permanently as,

$ git log
commit 76eb2dec9f4749f550ebca7302d12a84f5b972db (HEAD -> master)
Author: Developer Name <developer@yourcompany.com>
Date:   Tue Jun 18 09:22:49 2019 +0530

    This is first commit with helloworld.c

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

1 thought on “How to Create Your First Git Repository ?”

Leave a Comment