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.