Git – Tag Operation

Git – Tag Operation ”; Previous Next Tag operation allows giving meaningful names to a specific version in the repository. Suppose Tom and Jerry decide to tag their project code so that they can later access it easily. Create Tags Let us tag the current HEAD by using the git tag command. Tom provides a tag name with -a option and provides a tag message with –m option. tom@CentOS project]$ pwd /home/tom/top_repo/project [tom@CentOS project]$ git tag -a ”Release_1_0” -m ”Tagged basic string operation code” HEAD If you want to tag a particular commit, then use the appropriate COMMIT ID instead of the HEAD pointer. Tom uses the following command to push the tag into the remote repository. [tom@CentOS project]$ git push origin tag Release_1_0 The above command will produce the following result − Counting objects: 1, done. Writing objects: 100% (1/1), 183 bytes, done. Total 1 (delta 0), reused 0 (delta 0) To [email protected]:project.git * [new tag] Release_1_0 −> Release_1_0 View Tags Tom created tags. Now, Jerry can view all the available tags by using the Git tag command with –l option. [jerry@CentOS src]$ pwd /home/jerry/jerry_repo/project/src [jerry@CentOS src]$ git pull remote: Counting objects: 1, done. remote: Total 1 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (1/1), done. From git.server.com:project * [new tag] Release_1_0 −> Release_1_0 Current branch master is up to date. [jerry@CentOS src]$ git tag -l Release_1_0 Jerry uses the Git show command followed by its tag name to view more details about tag. [jerry@CentOS src]$ git show Release_1_0 The above command will produce the following result − tag Release_1_0 Tagger: Tom Cat <[email protected]> Date: Wed Sep 11 13:45:54 2013 +0530 Tagged basic string operation code commit 577647211ed44fe2ae479427a0668a4f12ed71a1 Author: Tom Cat <[email protected]> Date: Wed Sep 11 10:21:20 2013 +0530 Removed executable binary diff –git a/src/string_operations b/src/string_operations deleted file mode 100755 index 654004b..0000000 Binary files a/src/string_operations and /dev/null differ Delete Tags Tom uses the following command to delete tags from the local as well as the remote repository. [tom@CentOS project]$ git tag Release_1_0 [tom@CentOS project]$ git tag -d Release_1_0 Deleted tag ”Release_1_0” (was 0f81ff4) # Remove tag from remote repository. [tom@CentOS project]$ git push origin :Release_1_0 To [email protected]:project.git – [deleted] Release_1_0 Print Page Previous Next Advertisements ”;

Git – Patch Operation

Git – Patch Operation ”; Previous Next Patch is a text file, whose contents are similar to Git diff, but along with code, it also has metadata about commits; e.g., commit ID, date, commit message, etc. We can create a patch from commits and other people can apply them to their repository. Jerry implements the strcat function for his project. Jerry can create a path of his code and send it to Tom. Then, he can apply the received patch to his code. Jerry uses the Git format-patch command to create a patch for the latest commit. If you want to create a patch for a specific commit, then use COMMIT_ID with the format-patch command. [jerry@CentOS project]$ pwd /home/jerry/jerry_repo/project/src [jerry@CentOS src]$ git status -s M string_operations.c ?? string_operations [jerry@CentOS src]$ git add string_operations.c [jerry@CentOS src]$ git commit -m “Added my_strcat function” [master b4c7f09] Added my_strcat function 1 files changed, 13 insertions(+), 0 deletions(-) [jerry@CentOS src]$ git format-patch -1 0001-Added-my_strcat-function.patch The above command creates .patch files inside the current working directory. Tom can use this patch to modify his files. Git provides two commands to apply patches git amand git apply, respectively. Git apply modifies the local files without creating commit, while git am modifies the file and creates commit as well. To apply patch and create commit, use the following command − [tom@CentOS src]$ pwd /home/tom/top_repo/project/src [tom@CentOS src]$ git diff [tom@CentOS src]$ git status –s [tom@CentOS src]$ git apply 0001-Added-my_strcat-function.patch [tom@CentOS src]$ git status -s M string_operations.c ?? 0001-Added-my_strcat-function.patch The patch gets applied successfully, now we can view the modifications by using the git diff command. [tom@CentOS src]$ git diff The above command will produce the following result − diff –git a/src/string_operations.c b/src/string_operations.c index 8ab7f42..f282fcf 100644 — a/src/string_operations.c +++ b/src/string_operations.c @@ -1,5 +1,16 @@ #include <stdio.h> +char *my_strcat(char *t, char *s) diff –git a/src/string_operations.c b/src/string_operations.c index 8ab7f42..f282fcf 100644 — a/src/string_operations.c +++ b/src/string_operations.c @@ -1,5 +1,16 @@ #include <stdio.h> +char *my_strcat(char *t, char *s) + { + char *p = t; + + + while (*p) ++p; + while (*p++ = *s++) + ; + return t; + } + size_t my_strlen(const char *s) { const char *p = s; @@ -23,6 +34,7 @@ int main(void) { Print Page Previous Next Advertisements ”;

Git – Delete Operation

Git – Delete Operation ”; Previous Next Tom updates his local repository and finds the compiled binary in the src directory. After viewing the commit message, he realizes that the compiled binary was added by Jerry. [tom@CentOS src]$ pwd /home/tom/project/src [tom@CentOS src]$ ls Makefile string_operations string_operations.c [tom@CentOS src]$ file string_operations string_operations: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped [tom@CentOS src]$ git log commit 29af9d45947dc044e33d69b9141d8d2dad37cc62 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 10:16:25 2013 +0530 Added compiled binary VCS is used to store the source code only and not executable binaries. So, Tom decides to remove this file from the repository. For further operation, he uses the git rm command. [tom@CentOS src]$ ls Makefile string_operations string_operations.c [tom@CentOS src]$ git rm string_operations rm ”src/string_operations” [tom@CentOS src]$ git commit -a -m “Removed executable binary” [master 5776472] Removed executable binary 1 files changed, 0 insertions(+), 0 deletions(-) delete mode 100755 src/string_operations After commit, he pushes his changes to the repository. [tom@CentOS src]$ git push origin master The above command will produce the following result. Counting objects: 5, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 310 bytes, done. Total 3 (delta 1), reused 0 (delta 0) To [email protected]:project.git 29af9d4..5776472 master −> master Print Page Previous Next Advertisements ”;

Git – Review Changes

Git – Review Changes ”; Previous Next After viewing the commit details, Jerry realizes that the string length cannot be negative, that’s why he decides to change the return type of my_strlen function. Jerry uses the git log command to view log details. [jerry@CentOS project]$ git log The above command will produce the following result. commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530 Implemented my_strlen function Jerry uses the git show command to view the commit details. The git show command takes SHA-1 commit ID as a parameter. [jerry@CentOS project]$ git show cbe1249b140dad24b2c35b15cc7e26a6f02d2277 The above command will produce the following result − commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530 Implemented my_strlen function diff –git a/string.c b/string.c new file mode 100644 index 0000000..187afb9 — /dev/null +++ b/string.c @@ -0,0 +1,24 @@ +#include <stdio.h> + +int my_strlen(char *s) +{ + char *p = s; + + while (*p) + ++p; + return (p -s ); + } + He changes the return type of the function from int to size_t. After testing the code, he reviews his changes by running the git diff command. [jerry@CentOS project]$ git diff The above command will produce the following result − diff –git a/string.c b/string.c index 187afb9..7da2992 100644 — a/string.c +++ b/string.c @@ -1,6 +1,6 @@ #include <stdio.h> -int my_strlen(char *s) +size_t my_strlen(char *s) { char *p = s; @@ -18,7 +18,7 @@ int main(void) }; for (i = 0; i < 2; ++i) { – printf(“string lenght of %s = %dn”, s[i], my_strlen(s[i])); + printf(“string lenght of %s = %lun”, s[i], my_strlen(s[i])); return 0; } Git diff shows ”+” sign before lines, which are newly added and ”−” for deleted lines. Print Page Previous Next Advertisements ”;

Git – Push Operation

Git – Push Operation ”; Previous Next Jerry modified his last commit by using the amend operation and he is ready to push the changes. The Push operation stores data permanently to the Git repository. After a successful push operation, other developers can see Jerry’s changes. He executes the git log command to view the commit details. [jerry@CentOS project]$ git log The above command will produce the following result: commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t Before push operation, he wants to review his changes, so he uses the git show command to review his changes. [jerry@CentOS project]$ git show d1e19d316224cddc437e3ed34ec3c931ad803958 The above command will produce the following result: commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t diff –git a/string.c b/string.c new file mode 100644 index 0000000..7da2992 — /dev/null +++ b/string.c @@ -0,0 +1,24 @@ +#include <stdio.h> + +size_t my_strlen(char *s) + { + char *p = s; + + while (*p) + ++p; + return (p -s ); + } + +int main(void) + { + int i; + char *s[] = { + “Git tutorials”, + “Tutorials Point” + }; + + + for (i = 0; i < 2; ++i) printf(“string lenght of %s = %lun”, s[i], my_strlen(s[i])); + + return 0; + } Jerry is happy with his changes and he is ready to push his changes. [jerry@CentOS project]$ git push origin master The above command will produce the following result: Counting objects: 4, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 517 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:project.git 19ae206..d1e19d3 master −> master Jerry’s changes have been successfully pushed to the repository; now other developers can view his changes by performing clone or update operation. Print Page Previous Next Advertisements ”;

Git – Discussion

Discuss Git ”; Previous Next Git is a distributed revision control and source code management system with an emphasis on speed. Git was initially designed and developed by Linus Torvalds for Linux kernel development. Git is a free software distributed under the terms of the GNU General Public License version 2. This tutorial explains how to use Git for project version control in a distributed environment while working on web-based and non web-based applications development. Print Page Previous Next Advertisements ”;

Git – Handling Conflicts

Git – Handling Conflicts ”; Previous Next Perform Changes in wchar_support Branch Jerry is working on the wchar_support branch. He changes the name of the functions and after testing, he commits his changes. [jerry@CentOS src]$ git branch master * wchar_support [jerry@CentOS src]$ git diff The above command produces the following result − diff –git a/src/string_operations.c b/src/string_operations.c index 8fb4b00..01ff4e0 100644 — a/src/string_operations.c +++ b/src/string_operations.c @@ -1,7 +1,7 @@ #include <stdio.h> #include <wchar.h> -size_t w_strlen(const wchar_t *s) +size_t my_wstrlen(const wchar_t *s) { const wchar_t *p = s; After verifying the code he commits his changes. [jerry@CentOS src]$ git status -s M string_operations.c [jerry@CentOS src]$ git add string_operations.c [jerry@CentOS src]$ git commit -m ”Changed function name” [wchar_support 3789fe8] Changed function name 1 files changed, 1 insertions(+), 1 deletions(-) [jerry@CentOS src]$ git push origin wchar_support The above command will produce the following result − Counting objects: 7, done. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 409 bytes, done. Total 4 (delta 1), reused 0 (delta 0) To [email protected]:project.git 64192f9..3789fe8 wchar_support -> wchar_support Perform Changes in Master Branch Meanwhile in the master branch, Tom also changes the name of the same function and pushes his changes to the master branch. [tom@CentOS src]$ git branch * master [tom@CentOS src]$ git diff The above command produces the following result − diff –git a/src/string_operations.c b/src/string_operations.c index 8fb4b00..52bec84 100644 — a/src/string_operations.c +++ b/src/string_operations.c @@ -1,7 +1,8 @@ #include <stdio.h> #include <wchar.h> -size_t w_strlen(const wchar_t *s) +/* wide character strlen fucntion */ +size_t my_wc_strlen(const wchar_t *s) { const wchar_t *p = s; After verifying diff, he commits his changes. [tom@CentOS src]$ git status -s M string_operations.c [tom@CentOS src]$ git add string_operations.c [tom@CentOS src]$ git commit -m ”Changed function name from w_strlen to my_wc_strlen” [master ad4b530] Changed function name from w_strlen to my_wc_strlen 1 files changed, 2 insertions(+), 1 deletions(-) [tom@CentOS src]$ git push origin master The above command will produce the following result − Counting objects: 7, done. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 470 bytes, done. Total 4 (delta 1), reused 0 (delta 0) To [email protected]:project.git 64192f9..ad4b530 master -> master On the wchar_support branch, Jerry implements strchr function for wide character string. After testing, he commits and pushes his changes to the wchar_support branch. [jerry@CentOS src]$ git branch master * wchar_support [jerry@CentOS src]$ git diff The above command produces the following result − diff –git a/src/string_operations.c b/src/string_operations.c index 01ff4e0..163a779 100644 — a/src/string_operations.c +++ b/src/string_operations.c @@ -1,6 +1,16 @@ #include <stdio.h> #include <wchar.h> +wchar_t *my_wstrchr(wchar_t *ws, wchar_t wc) + { + while (*ws) { + if (*ws == wc) + return ws; + ++ws; + } + return NULL; + } + size_t my_wstrlen(const wchar_t *s) { const wchar_t *p = s; After verifying, he commits his changes. [jerry@CentOS src]$ git status -s M string_operations.c [jerry@CentOS src]$ git add string_operations.c [jerry@CentOS src]$ git commit -m ”Addded strchr function for wide character string” [wchar_support 9d201a9] Addded strchr function for wide character string 1 files changed, 10 insertions(+), 0 deletions(-) [jerry@CentOS src]$ git push origin wchar_support The above command will produce the following result − Counting objects: 7, done. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 516 bytes, done. Total 4 (delta 1), reused 0 (delta 0) To [email protected]:project.git 3789fe8..9d201a9 wchar_support -> wchar_support Tackle Conflicts Tom wants to see what Jerry is doing on his private branch so, he tries to pull the latest changes from the wchar_support branch, but Git aborts the operation with the following error message. [tom@CentOS src]$ git pull origin wchar_support The above command produces the following result − remote: Counting objects: 11, done. 63Git Tutorials remote: Compressing objects: 100% (8/8), done. remote: Total 8 (delta 2), reused 0 (delta 0) Unpacking objects: 100% (8/8), done. From git.server.com:project * branch wchar_support -> FETCH_HEAD Auto-merging src/string_operations.c CONFLICT (content): Merge conflict in src/string_operations.c Automatic merge failed; fix conflicts and then commit the result. Resolve Conflicts From the error message, it is clear that there is a conflict in src/string_operations.c . He runs the git diff command to view further details. [tom@CentOS src]$ git diff The above command produces the following result − diff –cc src/string_operations.c index 52bec84,163a779..0000000 — a/src/string_operations.c +++ b/src/string_operations.c @@@ -1,8 -1,17 +1,22 @@@ #include <stdio.h> #include <wchar.h> ++<<<<<<< HEAD +/* wide character strlen fucntion */ +size_t my_wc_strlen(const wchar_t *s) ++======= + wchar_t *my_wstrchr(wchar_t *ws, wchar_t wc) + { + + while (*ws) { if (*ws == wc) + return ws; + ++ws; + } + return NULL; + } + + size_t my_wstrlen(const wchar_t *s) ++>>>>>>>9d201a9c61bc4713f4095175f8954b642dae8f86 { const wchar_t *p = s; As both Tom and Jerry changed the name of the same function, Git is in a state of confusion and it asks the user to resolve the conflict manually. Tom decides to keep the function name suggested by Jerry, but he keeps the comment added by him, as it is. After removing the conflict markers, git diff will look like this. [tom@CentOS src]$ git diff The above command produces the following result. diff –cc src/string_operations.c diff –cc src/string_operations.c index 52bec84,163a779..0000000 — a/src/string_operations.c +++ b/src/string_operations.c @@@ -1,8 -1,17 +1,18 @@@ #include <stdio.h> #include <wchar.h> + wchar_t *my_wstrchr(wchar_t *ws, wchar_t wc) + { + while (*ws) { + if (*ws == wc) + return ws; + ++ws; + } + return NULL; + } + +/* wide character strlen fucntion */ – size_t my_wc_strlen(const wchar_t *s) + size_t my_wstrlen(const wchar_t *s) { const wchar_t *p = s; As Tom has modified the files, he has to commit these changes first and thereafter, he can pull the changes. [tom@CentOS src]$ git commit -a -m ”Resolved conflict” [master 6b1ac36] Resolved conflict [tom@CentOS src]$ git pull origin wchar_support. Tom has resolved the conflict, now the pull operation will succeed. Print Page Previous Next Advertisements ”;

Git – Quick Guide

Git – Quick Guide ”; Previous Next Git – Basic Concepts Version Control System Version Control System (VCS) is a software that helps software developers to work together and maintain a complete history of their work. Listed below are the functions of a VCS − Allows developers to work simultaneously. Does not allow overwriting each other’s changes. Maintains a history of every version. Following are the types of VCS − Centralized version control system (CVCS). Distributed/Decentralized version control system (DVCS). In this chapter, we will concentrate only on distributed version control system and especially on Git. Git falls under distributed version control system. Distributed Version Control System Centralized version control system (CVCS) uses a central server to store all files and enables team collaboration. But the major drawback of CVCS is its single point of failure, i.e., failure of the central server. Unfortunately, if the central server goes down for an hour, then during that hour, no one can collaborate at all. And even in a worst case, if the disk of the central server gets corrupted and proper backup has not been taken, then you will lose the entire history of the project. Here, distributed version control system (DVCS) comes into picture. DVCS clients not only check out the latest snapshot of the directory but they also fully mirror the repository. If the server goes down, then the repository from any client can be copied back to the server to restore it. Every checkout is a full backup of the repository. Git does not rely on the central server and that is why you can perform many operations when you are offline. You can commit changes, create branches, view logs, and perform other operations when you are offline. You require network connection only to publish your changes and take the latest changes. Advantages of Git Free and open source Git is released under GPL’s open source license. It is available freely over the internet. You can use Git to manage property projects without paying a single penny. As it is an open source, you can download its source code and also perform changes according to your requirements. Fast and small As most of the operations are performed locally, it gives a huge benefit in terms of speed. Git does not rely on the central server; that is why, there is no need to interact with the remote server for every operation. The core part of Git is written in C, which avoids runtime overheads associated with other high-level languages. Though Git mirrors entire repository, the size of the data on the client side is small. This illustrates the efficiency of Git at compressing and storing data on the client side. Implicit backup The chances of losing data are very rare when there are multiple copies of it. Data present on any client side mirrors the repository, hence it can be used in the event of a crash or disk corruption. Security Git uses a common cryptographic hash function called secure hash function (SHA1), to name and identify objects within its database. Every file and commit is check-summed and retrieved by its checksum at the time of checkout. It implies that, it is impossible to change file, date, and commit message and any other data from the Git database without knowing Git. No need of powerful hardware In case of CVCS, the central server needs to be powerful enough to serve requests of the entire team. For smaller teams, it is not an issue, but as the team size grows, the hardware limitations of the server can be a performance bottleneck. In case of DVCS, developers don’t interact with the server unless they need to push or pull changes. All the heavy lifting happens on the client side, so the server hardware can be very simple indeed. Easier branching CVCS uses cheap copy mechanism, If we create a new branch, it will copy all the codes to the new branch, so it is time-consuming and not efficient. Also, deletion and merging of branches in CVCS is complicated and time-consuming. But branch management with Git is very simple. It takes only a few seconds to create, delete, and merge branches. DVCS Terminologies Local Repository Every VCS tool provides a private workplace as a working copy. Developers make changes in their private workplace and after commit, these changes become a part of the repository. Git takes it one step further by providing them a private copy of the whole repository. Users can perform many operations with this repository such as add file, remove file, rename file, move file, commit changes, and many more. Working Directory and Staging Area or Index The working directory is the place where files are checked out. In other CVCS, developers generally make modifications and commit their changes directly to the repository. But Git uses a different strategy. Git doesn’t track each and every modified file. Whenever you do commit an operation, Git looks for the files present in the staging area. Only those files present in the staging area are considered for commit and not all the modified files. Let us see the basic workflow of Git. Step 1 − You modify a file from the working directory. Step 2 − You add these files to the staging area. Step 3 − You perform commit operation that moves the files from the staging area. After push operation, it stores the changes permanently to the Git repository. Suppose you modified two files, namely “sort.c” and “search.c” and you want two different commits for each operation. You can add one file in the staging area and do commit. After the first commit, repeat the same procedure for another file. # First commit [bash]$ git add sort.c # adds file to the staging area [bash]$ git commit –m “Added sort operation” # Second commit [bash]$ git add search.c # adds file to the staging area [bash]$ git commit –m “Added search operation” Blobs Blob stands for

Git – Move Operation

Git – Move Operation ”; Previous Next As the name suggests, the move operation moves a directory or a file from one location to another. Tom decides to move the source code into src directory. The modified directory structure will appear as follows − [tom@CentOS project]$ pwd /home/tom/project [tom@CentOS project]$ ls README string string.c [tom@CentOS project]$ mkdir src [tom@CentOS project]$ git mv string.c src/ [tom@CentOS project]$ git status -s R string.c −> src/string.c ?? string To make these changes permanent, we have to push the modified directory structure to the remote repository so that other developers can see this. [tom@CentOS project]$ git commit -m “Modified directory structure” [master 7d9ea97] Modified directory structure 1 files changed, 0 insertions(+), 0 deletions(-) rename string.c => src/string.c (100%) [tom@CentOS project]$ git push origin master Counting objects: 4, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 320 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:project.git e86f062..7d9ea97 master −> master In Jerry’s local repository, before the pull operation, it will show the old directory structure. [jerry@CentOS project]$ pwd /home/jerry/jerry_repo/project [jerry@CentOS project]$ ls README string string.c But after the pull operation, the directory structure will get updated. Now, Jerry can see the src directory and the file present inside that directory. [jerry@CentOS project]$ git pull remote: Counting objects: 4, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From git.server.com:project e86f062..7d9ea97 master −> origin/master First, rewinding head to replay your work on top of it… Fast-forwarded master to 7d9ea97683da90bcdb87c28ec9b4f64160673c8a. [jerry@CentOS project]$ ls README src string [jerry@CentOS project]$ ls src/ string.c Print Page Previous Next Advertisements ”;

Git – Update Operation

Git – Update Operation ”; Previous Next Modify Existing Function Tom performs the clone operation and finds a new file string.c. He wants to know who added this file to the repository and for what purpose, so, he executes the git log command. [tom@CentOS ~]$ git clone [email protected]:project.git The above command will produce the following result − Initialized empty Git repository in /home/tom/project/.git/ remote: Counting objects: 6, done. remote: Compressing objects: 100% (4/4), done. Receiving objects: 100% (6/6), 726 bytes, done. remote: Total 6 (delta 0), reused 0 (delta 0) The Clone operation will create a new directory inside the current working directory. He changes the directory to newly created directory and executes the git log command. [tom@CentOS ~]$ cd project/ [tom@CentOS project]$ git log The above command will produce the following result − commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <[email protected]> Date: Wed Sep 11 07:32:56 2013 +0530 Initial commit After observing the log, he realizes that the file string.c was added by Jerry to implement basic string operations. He is curious about Jerry’s code. So he opens string.c in text editor and immediately finds a bug. In my_strlen function, Jerry is not using a constant pointer. So, he decides to modify Jerry’s code. After modification, the code looks as follows − [tom@CentOS project]$ git diff The above command will produce the following result − diff –git a/string.c b/string.c index 7da2992..32489eb 100644 — a/string.c +++ b/string.c @@ -1,8 +1,8 @@ #include <stdio.h> -size_t my_strlen(char *s) +size_t my_strlen(const char *s) { – char *p = s; + const char *p = s; while (*p) ++p; } After testing, he commits his change. [tom@CentOS project]$ git status -s M string.c ?? string [tom@CentOS project]$ git add string.c [tom@CentOS project]$ git commit -m ”Changed char pointer to const char pointer” [master cea2c00] Changed char pointer to const char pointer 1 files changed, 2 insertions(+), 2 deletions(-) [tom@CentOS project]$ git log The above command will produce the following result − commit cea2c000f53ba99508c5959e3e12fff493b Author: Tom Cat <[email protected]> Date: Wed Sep 11 08:32:07 2013 +0530 Changed char pointer to const char pointer commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <[email protected]> Date: Wed Sep 11 07:32:56 2013 +0530 Initial commit Tom uses git push command to push his changes. [tom@CentOS project]$ git push origin master The above command will produce the following result − Counting objects: 5, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 336 bytes, done. Total 3 (delta 1), reused 0 (delta 0) To [email protected]:project.git d1e19d3..cea2c00 master −> master Add New Function Meanwhile, Jerry decides to implement string compare functionality. So he modifies string.c. After modification, the file looks as follows − [jerry@CentOS project]$ git diff The above command will produce the following result − index 7da2992..bc864ed 100644 — a/string.c +++ b/string.c 30Git Tutorials @@ -9,9 +9,20 @@ size_t my_strlen(char *s) return (p -s ); } +char *my_strcpy(char *t, char *s) + { + char *p = t; + + while (*t++ = *s++) + ; + + return p; + } + int main(void) { int i; + char p1[32]; char *s[] = { “Git tutorials”, “Tutorials Point” @@ -20,5 +31,7 @@ int main(void) for (i = 0; i < 2; ++i) printf(“string lenght of %s = %lun”, s[i], my_strlen(s[i])); + printf(“%sn”, my_strcpy(p1, “Hello, World !!!”)); + return 0; } } After testing, he is ready to push his change. [jerry@CentOS project]$ git status -s M string.c ?? string [jerry@CentOS project]$ git add string.c [jerry@CentOS project]$ git commit -m “Added my_strcpy function” [master e944e5a] Added my_strcpy function 1 files changed, 13 insertions(+), 0 deletions(-) Before push operation, he verifies commit by viewing log messages. [jerry@CentOS project]$ git log The above command will produce the following result − commit e944e5aab74b26e7447d3281b225309e4e59efcd Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:41:42 2013 +0530 Added my_strcpy function commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <[email protected]> Date: Wed Sep 11 07:32:56 2013 +0530 Initial commit Jerry is happy with the changes and he wants to push his changes. [jerry@CentOS project]$ git push origin master The above command will produce the following result − To [email protected]:project.git ! [rejected] master −> master (non-fast-forward) error: failed to push some refs to ”[email protected]:project.git” To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again. See the ”Note about fast-forwards” section of ”git push –help” for details. But Git is not allowing Jerry to push his changes. Because Git identified that remote repository and Jerry’s local repository are not in sync. Because of this, he can lose the history of the project. To avoid this mess, Git failed this operation. Now, Jerry has to first update the local repository and only thereafter, he can push his own changes. Fetch Latest Changes Jerry executes the git pull command to synchronize his local repository with the remote one. [jerry@CentOS project]$ git pull The above command will produce the following result − remote: Counting objects: 5, done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From git.server.com:project d1e19d3..cea2c00 master −> origin/master First, rewinding head to replay your work on top of it… Applying: Added my_strcpy function After pull operation, Jerry checks the log messages and finds the details of Tom’s commit with commit ID cea2c000f53ba99508c5959e3e12fff493ba6f69 [jerry@CentOS project]$ git log The above command will produce the following result − commit e86f0621c2a3f68190bba633a9fe6c57c94f8e4f Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:41:42 2013 +0530 Added my_strcpy function commit cea2c000f53ba99508c5959e3e12fff493ba6f69 Author: Tom Cat <[email protected]> Date: Wed Sep 11 08:32:07 2013 +0530 Changed char pointer to const char pointer commit d1e19d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse <[email protected]> Date: Wed Sep 11 08:05:26 2013 +0530