Tuesday 22 December 2015

Git Tutorial for Beginners Part-2


We have already seen the configuration of the Git, adding files to repository and pushing all the files to Git server. If you want to review those steps you can always go through my previous tutorial Git Tutorial for Beginners Part-1.




In this tutorial we will see how to view different commits and switching between those snapshots by retrieving old version of the program.

Let's Start...

First go to directory where we created our original code with first version.


$ cd git-repos/first-repo

If you don't know, how to create repository using Git, you can always read Git Tutorial for Beginners Part 1.

Type command "git log" to see different commits that we have done up till now. As we have committed only once, there will be only one commit entry  with its comment.


/git-repos/first-repo$ git log
commit 028b485742ee1893b234d22c0373578d67b6b98d
Author: vish2207 <vish2207@gmail.com>
Date:   Mon Nov 30 12:25:22 2015 +0530

    Initial release version with hello world string

The output of the "git log" command shows commit details with commit ID, author, time stamp and comment of the commit.

The line "commit 028b485742ee1893b234d22c0373578d67b6b98d" shows the check-sum which is unique for each commit.

Author and Time stamp of commit with Day of week, Date, Time, Year and GMT settings.

Comment for particular commit which shows brief explanation of code changes that we have entered while committing.

There are other options which can be passed to git log command, details of which you can get by typing git log --help.

Now we will change the C file which we had initially created using our user friendly text editor gedit.

Open file hello.c using command gedit hello.c .

Change the original file to match the new file as shown below.


1
2
3
4
5
6
7
// Old file
#include <stdio.h>

int main(){
        printf("Hello World...!");
        return 0;
}


1
2
3
4
5
6
7
8
// New file
#include <stdio.h>

int main(){
        printf("This is new line.");
        printf("Updated to make changes in file");
        return 0;
}

You can see that we have changed line no 5 and added one more line right below it. Save the file hello.c and exit.

As you know that files hello.c is already being tracked by git, it should show us respective changes.

We will first see which files have changed using command "git status". This file will show below output with changed files in red color.


$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

 modified:   hello.c

no changes added to commit (use "git add" and/or "git commit -a")

The output of git status shows us the files that have been changed under "Changes not staged for commit:".

In our case hello.c has been shown as modified. Now, we will see what has been changed in the file named hello.c using command "git diff". The output will be as below.


$ git diff
diff --git a/hello.c b/hello.c
index 24a107a..1cdb29b 100644
--- a/hello.c
+++ b/hello.c
@@ -1,6 +1,7 @@
 #include <stdio.h>

 int main(){
- printf("Hello World...!");
- return 0;
+       printf("This is new line.");
+       printf("Updated to make changes in file");
+       return 0;
 }

The output is a summery of changes in the file which has a line @@ -1,6 +1,7 @@. Let's interpret this line.

The -(ve) sign indicates deletion. In the above line(enclosed by @@) "-1,6" indicates that starting from line no 1 till next 6 lines has been deleted. The +(ve) sign in the above line(enclosed by @@) "+1,7" indicate that starting from line no 1 till next 7 lines has been added.

Now below @@ line you can see that there are lines starting with - sign which are actually deleted and inplace of them lines starting with + signs are added.

Now we will add second version of our modified file hello.c to our git repository for permanent backup using "git add" and "git commit -m" commands.


$ git status
$ git add hello.c
$ git status
$ git commit -m "Second version commit"
$ git push origin master

Now you can see the same file updated on the https://github.com/<your username> with your comment "Second version commit".

We would review the different versions of the commits that we have performed till now using "git log" command.


$ git log
commit 16a52b541a8cdb06fdf4b41fe2ba2129d8df5a35
Author: vish2207 <vish2207@gmail.com>
Date:   Fri Dec 11 18:32:37 2015 +0530

    Second version commit

commit 028b485742ee1893b234d22c0373578d67b6b98d
Author: vish2207 <vish2207@gmail.com>
Date:   Mon Nov 30 12:25:22 2015 +0530

    Initial release version with hello world string

The output above shows two different versions with respective comments and their timestamp.

If you want short and sweet summery of for each version we have committed, use command "git log --oneline".


$ git log --oneline
16a52b5 Second version commit
028b485 Initial release version with hello world string

The output above line shows first 7 characters of the check-sum of particular version and its comment only.

Try to change files and add more files and commit changes and check the reflection in the https://github.com/<your username>.

If you want to your original version which we commented as "Initial release version with hello world string" then you can get it simply by command "git checkout <checksum>".

In our case the checksum for first version is 028b485. So, our command will be "git checkout 028b485". Now check your file hello.c using gedit. It would show you the original content which we initially uploaded.

Again if you want to go to latest version you can fire "git pull" command. This command will pull all the updated files from github till date. When you check your file hello.c, you will find the latest content in the file.

If you like the blog like my Facebook Blog page to keep updated.

Tuesday 24 November 2015

Compiling and Running C programs in Ubuntu command line

 


Many of you might be knowing the C language thoroughly, but have you ever tried to run the very basic programs in the Linux through command line? You may not have done that, like me.

I had learned C programming using Borland C in the windows about 11 years ago, and then used various IDEs to compile and run the programs  for different platforms.

Monday 23 November 2015

Qemu setup for ARM-Cortex-A9 Part-1




Many of you people would have used simulators for Microcontrollers like, Proteus and other platform dependent simulators integrated in IDEs.

But for large Embedded systems there is an open source option available to us. QEMU.


One of the most versatile tool for emulating number of microprocessors and other peripherals around it. It gives near native performance by emulating processors on your host system without actually having a hardware board.


Friday 20 November 2015

Git tutorial in linux for beginners Part 1





Introduction

Git is one of the modern version control systems, which has multiple advantages over CVS, Mercurial, etc.

It is distributed version control system, employing totally new concept for version control. It has very good features when multiple developers are working on the same project.

Git uses file system like structure, where it takes snapshot of file system of each version. It only tracks the changes in the files between each versions. Every thing is being stored in the simple text files. This new concept is space efficient compared to older version control systems.

Git is faster as it stores every thing on the local computer and then it pushes the changes to the server at the end. The change files are protected by check-sums, so, you don't need to worry about data integrity.

Thursday 29 October 2015

Structure Pointers Simplified


I was running away when it came to structure pointers. It might be my mind block, that I never tried to properly learn structure pointers.

Once I did, it was all easy. You need a good approach for learning such seemingly complex but simple things.

Simple Definition

Structure Pointers are the pointers which points structure. Ha Ha Ha...

This might look funny but it is as simple as that. They are like any other char pointer and int pointers, but they points to a structure.

Wednesday 28 October 2015

Difference between uint8_t and unsigned char, uint16_t or uint32_t and unsigned int

Have you ever wondered why people uses different keywords to define the same variable in C or C++ program?

Sometimes you would have noticed that 8 bit variable has been defined as

unsigned char a;
uint8_t       b;

Also, many a times you would notice integer has been defined as

unsigned int c;
uint16_t     d;
uint32_t     e;

Initially when I was in the learning phase of C programming with microcontrollers, I used to get confused, what to use when.