0% found this document useful (0 votes)
29 views70 pages

GIT Material Titorial Point

Uploaded by

jkjlab01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
29 views70 pages

GIT Material Titorial Point

Uploaded by

jkjlab01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 70
(4°) tutorialspoint & Home Coding Ground (© Jobs [2] Whiteboard % Tools 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 othera€™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. 0° tutorialspoint 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, ie, 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 GPLG€™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 0° tutorialspoint security Git uses a common cryptographic hash function called secure hash function (SHAI), 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 dona€™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. 0° tutorialspoint Ine Working GWeCLory IS Ine place Wnere IES GFE CHECKED OUL IT ONE CVE, 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 modity 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. Working directory Git add operation Staging area Git commit operation Git repository Suppose you modified two files, namely G€cesort.cG€H and G€cesearch.ca€X 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. 0° tutorialspoint # adds file to the staging area [bash]$ git commit G€“m a€ceAdded sort operationaes # Second commit [bash]$ git add search.c # adds file to the staging area [bash]s git commit 4€"m a€ceAdded search operationa€s Blobs Blob stands for Binary Large Object. Each version of a file is represented by blob. A blob holds the file data but doesna@€™t contain any metadata about the file. It is a binary file, and in Git database, itis named as SHAl hash of that file. in Git, files are not addressed by names. Everything is content-addressed. Trees Tree is an object, which represents a directory. It holds blobs as well as other sub-directories. A tree is a binary file that stores references to blobs and trees which are also named as SHAI hash of the tree object. Commits Commit holds the current state of the repository. A commit is also named by SHA1 hash. You can consider a commit object as a node of the linked list. Every commit object has a pointer to the parent commit object. From a given commit, you can traverse back by looking at the parent pointer to view the history of the commit. if a commit has multiple parent commits, then that particular commit has been created by merging two branches. Branches Branches are used to create another line of development. By default, Git has a master branch, which is same as trunk in Subversion. Usually, a branch is created to work on a new feature. Once the feature is completed, it is merged back with the master branch and we delete the branch. Every branch is 0° tutorialspoint Tags Tag assigns a meaningful name with a specific version in the repository. Tags are very similar to branches, but the difference is that tags are immutable. It means, tag is a branch, which nobody intends to modify. Once a tag is created for a particular commit, even if you create a new commit, it will not be updated. Usually, developers create tags for product releases. Clone Clone operation creates the instance of the repository. Clone operation not only checks out the working copy, but it also mirrors the complete repository. Users can perform many operations with this local repository. The only time networking gets involved is when the repository instances are being synchronized. Pull Pull operation copies the changes from a remote repository instance to a local one. The pull operation is used for synchronization between two repository instances. This is same as the update operation in Subversion. Push Push operation copies changes from a local repository instance to a remote one. This is used to store the changes permanently into the Git repository. This is same as the commit operation in Subversion. HEAD HEAD is a pointer, which always points to the latest commit in the branch. Whenever you make a commit, HEAD is updated with the latest commit. The heads of the branches are stored in .git/rets/heads/ directory. [Centos]s Is -1 git/refs/heads/ master 0° tutorialspoint Revision Revision represents the version of the source code. Revisions in Git are represented by commits. These commits are identified by SHAT secure hashes. URL URL represents the location of the Git repository. Git URL is stored in config file. [tom@Centos tom_repo]$ pwd |home/tom/tom_repo [tom@Centos tom_repo]$ cat .git/config {core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote “orig url = gituser@gitserver.comiproject.git fetch = +refs/heads/*:refs/remotes/origin/* Git - Environment Setup Before you can use Git, you have to install and do some basic configuration changes. Below are the steps to install Git client on Ubuntu and Centos Linux. Installation of Git Client If you are using Debian base GNU/Linux distribution, then apt-get command will do the needful. [ubuntu ~]$ sudo apt-get install git-core [sudo] password for ubuntu: 0° tutorialspoint And if you are using RPM based GNU/Linux distribution, then use yam command as given. [centos -]$ su- Password: [Centos ~]# yum -y install git-core [centos ~]# git --version git version 1.7.1 Customize Git Environment Git provides the git config tool, which allows you to set configuration variables. Git stores all global configurations in .gitconfig file, which is located in your home directory. To set these configuration values as global, add the --global option, and if you omit --global option, then your configurations are specific for the current Git repository. You can also set up system wide configuration. Git stores these values in the [ete/gitconfig file, which contains the configuration for every user and repository on the system. To set these values, you must have the root rights and use the ~~ system option. When the above code is compiled and executed, it produces the following result Setting username This information is used by Git for each commit. [jerry@CentOs project]$ git config --global user.name "Jerry Mouse” Setting email id 0° tutorialspoint Yerry@centus proyectyy git conng --glopar usereman yerry@tutoriarspoInccom Avoid merge commits for pulling You pull the latest changes from a remote repository, and if these changes are divergent, then by default Git creates merge commits. We can avoid this via following settings. jerry@CentOs project]$ git config --global branch.autosetuprebase always Color highlighting The following commands enable color highlighting for Git in the console. [jerry@Centos project]$ git config --global color.ui true [jerry@Centos project]$ git config --global color.status auto [jerry@Centos project]$ git config --global color.branch auto Setting default editor By default, Git uses the system default editor, which is taken from the VISUAL or EDITOR environment variable. We can configure a different one by using git config, [jerry@CentOs project]$ git config --global core.editor vim Setting default merge tool Git does not provide a default merge tool for integrating conflicting changes into your working tree. We can set default merge tool by enabling following settings. [jerry@Centos project]$ git config --global merge.tool vimdiff 0° tutorialspoint to verily your Git SeLUNgs OF Ine 1OCa! reposuory, Use grt Conng ae-ust commana as given below. [ierry@Centos ~]$ git config --list The above command will produce the following result. user.name=Jerry Mouse user.email=jerry@tutorialspoint.com push.default=nothing branch.autosetuprebase=always colorui=true color.status=auto color.branch=auto core.editor=vim merge.tool=vimdiff Git - Life Cycle In this chapter, we will discuss the life cycle of Git. In later chapters, we will cover the Git commands for each operation. General workflow is as follows - You clone the Git repository as a working copy. You modify the working copy by adding/editing files. if necessary, you also update the working copy by taking other developer's changes You review the changes before commit. You commit changes. If everything is fine, then you push the changes to. the repository. After committing, if you realize something is wrong, then you correct the last commit and push the changes to the repository. Shown below is the pictorial representation of the work-flow. 0° tutorialspoint Repository | —————> working copy | Edit, Add, Move files Update operation > | Modify working copy | Status and Diff operation Review changes | Commit and Push operation Push operation Commit changes I ‘Amend and Push — =e Git - Create Operation In this chapter, we will see how to create a remote Git repository; from now on, we will refer to it as Git Server. We need a Git server to allow team collaboration. Create New User # add new group [root@Centos ~]# groupadd dev # add new user [root@Centos ~]# useradd -G devs -d /home/gituser -m -s /bin/bash gituser # change password [root@Centos ~]# passwd gituser 0° tutorialspoint Cnanging password tor user gituser. New password: Retype new password: passwd: all authentication token updated successfully. Create a Bare Repository Let us initialize a new repository by using init command followed by --bare option. It initializes the repository without a working directory. By convention, the bare repository must be named as .git. [gituser@centos ~]$ pwd /home/gituser [gituser@Centos ~]$ mkdir project.git [gituser@centos -]$ cd project.git/ [gituser@Centos projectgit]$ Is [gituser@Centos projectgit]$ git --bare init Initialized empty Git repository in /home/gituser-m/project.git/ [gituser@Centos projectgit]$ Is branches config description HEAD hooks info objects refs Generate Public/Private RSA Key Pair Let us walk through the process of configuring a Git server, ssh-keygen utility generates public/private RSA key pair, that we will use for user authentication, Open a terminal and enter the following command and just press enter for each input. After successful completion, it will create a .ssh directory inside the home directory. tom@Centos ~]$ pwd |home/tom 0° tutorialspoint The above command will produce the following result. Generating public/private rsa key pair. Enter file in which to save the key (/home/tom/.ssh/id_rsa): Press Enter Only Created directory '/home/tom/.ssh’. Enter passphrase (empty for no passphrase): =~ Enter same passphrase agai Your identification has been saved in /home/tom/.ssh/id_rsa. Your public key has been saved in /home/tom/.ssh/id_rsa.pub. The key fingerprint is: df:93:8c:al:b8:b7:67:69:3a:1f:65:e8:0¢:e9:25:al tom@CentOs The key's randomart image is +--[ RSA 2048]----+ I I -=- Press Enter Only ---> Press Enter Only ssh-keygen has generated two keys, first one is private (i. second one is public (i.e, id_rsa.pub). id_rsa) and the Note: Never share your PRIVATE KEY with others. Adding Keys to authorized_keys Suppose there are two developers working on a project, namely Tom and Jerry. Both users have generated public keys. Let us see how to use these keys for authentication. 0° tutorialspoint [tom@Centos ~]$ pwd [home/tom [tom@centos ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub gituser@git server.com The above command will produce the following result. gituser@gitserver.com’s password: Now try logging into the machine, with ‘ssh ‘gituser@gitserver.com”, and check in: ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. similarly, Jerry added his public key to the server by using ssh-copy-id command. ljerry@Centos ~]$ pwd /home/jerry ljerry@centos ~]$ ssh-copy-id -i ~/.ssh/id_rsa gituser@git server.com The above command will produce the following result. gituser@gitserver.com’s password: Now try logging into the machine, with ‘ssh ‘gituser@git.server.com”, and check in: -ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. Push Changes to the Repository We have created a bare repository on the server and allowed access for two users. From now on, Tom and Jerry can push their changes to the repository by adding it as a remote. Git init command creates .git directory to store metadata about the repository every time it reads the configuration from the .git/config file. 0° tutorialspoint [tom@centos ~]s pwd [nome/tom [tom@Centos ~]$ mkdir tom_repo [tom@centos ~]$ cd tom_repo/ [tom@CentoOs tom_repo]$ git init Initialized empty Git repository in /home/tom/tom_repo/.git/ [tom@Centos tom_repo]$ echo ‘TODO: Add contents for README’ > README [tom@CentOs tom_repo]$ git status -s 2? README [tom@Centos tom_repo]$ git add . [tom@CentOs tom_repo]$ git status -s A README [tom@CentOs tom_repo]$ git commit -m ‘initial commit The above command will produce the following result. [master (root-commit) 19ae206] Initial commit 1 files changed, | insertions(+), 0 deletions(-) create mode 100644 README Tom checks the log message by executing the git log command. [tom@centos tom_repo]$ git log The above command will produce the following result. commit 18ae20683fc460db7d1270f201a1429523b0e319 Author: Tom Cat 0° tutorialspoint Tom committed his changes to the local repository. Now, it#€™s time to push the changes to the remote repository. But before that, we have to add the repository as a remote, this is a one-time operation. After this, he can safely push the changes to the remote repository. Note - By default, Git pushes only to matching branches: For every branch that exists on the local side, the remote side is updated if a branch with the same name already exists there. In our tutorials, every time we push changes to the origin master branch, use appropriate branch name according to your requirement. [tom@Centos tom_repo]$ git remote add origin gituser@git.server.com:project.git [tom@Centos tom_repo]$ git push origin master The above command will produce the following result. Counting objects: 3, done. Writing objects: 100% (3/3), 242 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To gituser@gitserver.com:project.git * [new branch] master -> master Now, the changes are successfully committed to the remote repository. Git - Clone Operation We have a bare repository on the Git server and Tom also pushed his first version. Now, Jerry can view his changes. The Clone operation creates an instance of the remote repository. Jerry creates a new directory in his home directory and performs the clone operation. 0° tutorialspoint \jerry@Centos -]$ cd jerry_repo/ [jerry@Centos jerry_repo]$ git clone gituser@git server.com:project.git The above command will produce the following result. Initialized empty Git repository in /home/jerry/jerry_repo/project/.git/ remote: Counting objects: 3, done. Receiving objects: 100% (3/3), 241 bytes, done. remote: Total 3 (delta 0), reused 0 (delta 0) Jerry changes the directory to new local repository and lists its directory contents. [jerry@centos jerry_repo]$ cd project/ [jerry@Centos jerry_repo]$ Is README Git - Perform Changes Jerry clones the repository and decides to implement basic string operations. So he creates string. file. After adding the contents, string.c will look like as follows - #include int my_strlen(char *s) { char *p = s; while (*p) +p; return (p - s); } 0° tutorialspoint char *s[] = { “Git tutorials’, “Tutorials Point” i for (i = 0; i < 2; ++i) printt("string lenght of %s = %d\n’, s[i], my_strlen(s[i])); return 0; He compiled and tested his code and everything is working fine. Now, he can safely add these changes to the repository. Git add operation adds file to the staging area. [jerry@Centos project] $ git status -s 2? string 2? string.c [jerry@CentOs project]$ git add string.c Git is showing a question mark before file names. Obviously, these files are not a part of Git, and that is why Git does not know what to do with these files. That is why, Git is showing a question mark before file names. Jerry has added the file to the stash area, git status command will show files present in the staging area. [jerry@Centos project]$ git status -s Astring.c 2? string 0° tutorialspoint [jerry@Centos project]$ git commit -m ‘implemented my_strlen function’ The above command will produce the following result - [master cbe1249] implemented my_strien function 1 files changed, 24 insertions(+), 0 deletions(-) create mode 100644 string.c After commit to view log details, he runs the git log command. It will display the information of all the commits with their commit ID, commit author, commit date and SHA-1 hash of commit. [jerry@Centos project]$ git log The above command will produce the following result - commit cbel249b140dad24b2c35bl5cc7e26a6(02d2277 Author: Jerry Mouse Date: Wed Sep Il 08:05:26 2013 +0530 Implemented my_strien function commit 19ae20683f¢460db7d1270f201a1429523b0e319 Author: Tom Cat Date: Wed Sep 11 07:32:56 2013 +0530 Initial commit Git - Review Changes After viewing the commit details, Jerry realizes that the string length cannot be negative, thata€™s why he decides to change the return type of my_strlen function. 0° tutorialspoint Yerryercentus proectys git og The above command will produce the following result. commit cbel249b140dad24b2c35bl5cc7e26a6f02d2277 Author: Jerry Mouse 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 cbel249b140dad24b2c35bI5cc7e26a6t02d2277 The above command will produce the following result - commit cbel249b140dad24b2c35bI5cc7e26a6f02d2277 Author: Jerry Mouse Date: Wed Sep 11 08:05:26 2013 +0530 Implemented my_strien function diff --git a/string.c b/string.c new file mode 100644 index 0000000.187afb9 --- [dev/null +++ b/string.c @@ -0,0 +124 @@ +#include + +int my_strlen(char *s) 0° tutorialspoint + } + 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 @@-16 +16 @@ #include -int my_strlen(char *s) +size_t my_strlen(char *s) { char *p = s; @@ -18,7 +18,7 @@ int main(void) i for (i= { ~ printf(‘string lenght of %s = %d\n’, s[i], my_strien(s[i])); + printf(‘string lenght of %s = %lu\n’, s[i], my_strlen(s[i])); return 0; } yi < 2; +41) Git diff shows ‘+’ sign before lines, which are newly added and ‘~" for deleted lines. Git - Commit Changes Jerry has already committed the changes and he wants to correct his last commit. In this case, git amend operation will help. The amend operation 0° tutorialspoint Before amend operation, he checks the commit log. [jerry@centos project] $ git log The above command will produce the following result. commit cbel249bI40dad24b2c35bl5ce7e26a6f02d2277 Author: Jerry Mouse Date: Wed Sep I 08:05:26 2013 +0530 Implemented my_strlen function commit 194¢20683fc460db7d127¢f201a1429523b0e319 Author: Tom Cat Date: Wed Sep Il 07:32:56 2013 +0530 Initial commit Jerry commits the new changes with -- amend operation and views the commit log. [jerry@CentOs project]$ git status -s Mstring.c 2? string [jerry@CentOs project]$ git add string.c [jerry@CentOs project]$ git status -s Mstring.c 2? string [jerry@Centos project] $ git commit --amend -m ‘Changed return type of my_strier [master dlel9d3] Changed return type of my_strien to size_t files changed, 24 insertions(+), 0 deletions(-) create mode 100644 string. 0° tutorialspoint Yerryercentus proectys git og The above command will produce the following result. commit dlel9d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse Date: Wed Sep Il 08:05:26 2013 +0530 Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127¢f201a1429523b0e319 Author: Tom Cat Date: Wed Sep Il 07:32:56 2013 +0530 Initial commit Git - Push Operation 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 Jerrya€™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 diel9d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strlen to size_t 0° tutorialspoint [jerry@CentOs project]$ git show dle19d316224cdde437e3ed34ec3c931ad803958 The above command will produce the following result: commit dlel9d316224cdde437e3ed34ec3c931ad803958 Author: Jerry Mouse 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 +44 b/string.e @@ -0,0 +1,24 @@ +#include + +size_t my_strlen(char *s) + + char *p + + while (*p) + +4p; + return (p-s); + } + +int main(void) + { + inti; + char *s[] = { + Git tutorials’, + Tutorials Point" 0° tutorialspoint + + for (i = 0; i < 2; ++i) printf("string lenght of %s = %lu\n’, s[i], my_strien(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 gituser@gitserver.com:project.git 19ae206..dlel9d3 master -> master Jerrya€™s changes have been successfully pushed to the repository; now other developers can view his changes by performing clone or update operation. Git - Update Operation 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. 0° tutorialspoint 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 dlel8d316224cddc437e3ed34ec3c931ad803958 : Jerry Mouse Jed Sep Il 08:05:26 2013 +0530 ‘Changed return type of my_strlen to size_t commit 18ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat Date: Wed Sep II 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 Jerrya€™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 Jerrya€™s code. After modification, the code looks as follows - 0° tutorialspoint The above command will produce the following result - diff --git a/string.c b/string.c index 7da2992..32489eb 100644 --- a/string.e +++ b/string.c @@ -18 +18 @@ #include -size_t my_strlen(char *s) +size_t my_strlen(const char *s) { ~ char *p = 5; + const char *| while (*p) +P; } s; After testing, he commits his change. [tom@Centos project]$ git status -s Mstring.c 2? 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 files changed, 2 insertions(+), 2 deletions(-) [tom@Centos project]$ git log —_—_ SS > The above command will produce the following result - commit cea2c000f53ba99508c5959e3e1211f493b Author: Tom Cat Date: Wed Sep Il 08:32:07 2013 +0530 0° tutorialspoint commit dlel8d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse Date: Wed Sep I 08:05:26 2013 +0530 ‘Changed return type of my_strlen to size_t commit 194e20683fc460db7d127¢f201a1429523b0e319 Author: Tom Cat Date: Wed Sep Il 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 gituser@gitserver.com:project.git dlel8d3.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.be864ed 100644 --- a/string.c 0° tutorialspoint retum (p-s); } +char *my_strepy(char *t, char *s) + + char *p = t; + + while (*t++ = *s++) + + + return p; + } + int main(void) { inti; + 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 = %Iu\n", s[i], my_strlen(s[i])); + printf(‘%s\n", my_strcpy(pl, "Hello, World !!!")); + return 0; After testing, he is ready to push his change. [jerry@Centos project]$ git status -s Mstring.c 0° tutorialspoint [jerry@CentOs project]$ git commit -m "Added my_strcpy function” [master e944e5a] Added my_strepy function 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 e944e5aab74b26e7447d3281b225309e4e59efced Author: Jerry Mouse Date: Wed Sep 1 08:41:42 2013 +0530 Added my_strepy function commit dlel8d316224cddc437e3ed34ec3c931ad803958 Author: Jerry Mouse Changed return type of my_strlen to size_t commit 19ae20683fc460db7d127¢f201a1429523b0e319 Author: Tom Cat Date: Wed Sep Il 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 - 0° tutorialspoint master > master (non fast forward) error: failed to push some refs to ‘gituser@git server.com: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 Jerrya€™s local repository are not in syne. 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.comyproject dle19d3.cea2c00 master -> origin/master First, rewinding head to replay your work on top of it. Applying: Added my_strepy function After pull operation, Jerry checks the log messages and finds the details of Toma€™s commit with commit ID cea2c000fS3ba99508c5959e3el2t11493ba6I69 [jerry@CentOs project]$ git log 0° tutorialspoint Commi esorunczastosmunpaDsscuIENca /cMarsear Author: Jerry Mouse Date: Wed Sep Il 08:41:42 2013 +0530 Added my_strepy function commit cea2c000f53ba9950805959e3el2fff493basf6g Author: Tom Cat Date: Wed Sep Il 08:32:07 2013 +0530 ‘Changed char pointer to const char pointer commit die19d316224cdde437e3ed34ec3c931ad803958 Author: Jerry Mouse Date: Wed Sep 11 08:05:26 2013 +0530 Changed return type of my_strien to size_t commit 19ae20683fc460db7d1270f201a1429523b0e319 Author: Tom Cat Date: Wed Sep 11 07:32:56 2013 +0530 Initial commit Now, Jerry這s local repository is fully synchronized with the remote repository. So he can safely push his changes. [jerry@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), 455 bytes, done. Total 3 (delta 1), reused 0 (delta 0) 0° tutorialspoint Git - Stash Operation Suppose you are implementing a new feature for your product. Your code is in progress and suddenly a customer escalation comes. Because of this, you have to keep aside your new feature work for a few hours. You cannot commit your partial code and also cannot throw away your changes. So you need some temporary space, where you can store your partial changes and later on commit it. In Git, the stash operation takes your modified tracked files, stages changes, and saves them on a stack of unfinished changes that you can reapply at any time. [jerry@CentOs project]$ git status -s Mstring.c 22 string Now, you want to switch branches for customer escalation, but you dona€e™t want to commit what youd€™ve been working on yet; so youG€™ll stash the changes. To push a new stash onto your stack, run the git stash command. [jerry@Centos project]$ git stash Saved working directory and index state WIP on master: e86f062 Added my_strepy fi HEAD is now at e86f062 Added my_strepy function Now, your working directory is clean and all the changes are saved on a stack. Let us verify it with the git status command. [jerry@Centos project]$ git status -s 2? string Now you can safely switch the branch and work elsewhere. We can view a list of stashed changes by using the git stash list command. [jerry@CentOs project]$ git stash list 0° tutorialspoint Suppose you have resolved the customer escalation and you are back on your new feature looking for your half-done code, just execute the git stash pop command, to remove the changes from the stack and place them in the current working directory. [jerry@Centos project] $ git status -s 2? string [jerry@Centos project]$ git stash pop The above command will produce the following result: # On branch master # Changed but not updated: # (use “git add...” to update what will be committed) # (use “git checkout -- .." to discard changes in working directory) # # modified: string.c # # Untracked files: # (use “git add... to include in what will be committed) # # string no changes added to commit (use “git add" and/or “git commit -a") Dropped refs/stash@{0} (36f79dfedae4ac20e2e8558830154bd6315e72d4) [jerry@Centos project]$ git status -s Mstring.c 22 string Git - Move Operation 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 sre directory. The modified directory structure will appear as follows - 0° tutorialspoint [tom@Centos project]$ Is README string string.c [tom@Centos project]$ mkdir src [tom@Centos project]$ git mv string.c sre/ [tom@Centos project]$ git status -s R string.c -> sre/string.c 22 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 gituser@gitserver.com:project.git 86f062.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] Is README string string.c 0° tutorialspoint [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.comyproject 861062. 7d9ea97 master -> origin/master First, rewinding head to replay your work on top of it. Fast-forwarded master to 7d9ea97683da90bcdb87c28ec9b4164160673c8a. [jerry@CentOs project]$ Is README sre string [jerry@CentOs project]$ Is sro/ stringe Git - Rename Operation Till now, both Tom and Jerry were using manual commands to compile their project. Now, Jerry decides to create Makefile for their project and also give a proper name to the file @€cestring.ca€s. [jerry@Centos project]$ pwd /home/jerry/jerry_repo/project [jerry@centos project] $ Is README sre [jerry@Centos project]$ cd sre/ [jerry@Centos src]$ git add Makefile [jerry@Centos src]$ git mv string.c string_operations.c [jerry@Centos src]$ git status -s A Makefile Rstring.c -» string_operations.c 0° tutorialspoint detect the modified files. [jerry@Centos sre]$ git commit -a -m ‘Added Makefile and renamed strings.c to string_operations.c [master 94f7b26] Added Makefile and renamed strings.c to string_operations.c files changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/Makefile rename src/{string.c => string_operations.c} (100%) After commit, he pushes his changes to the repository. [jerry@Centos sre]$ git push origin master The above command will produce the following result - Counting objects: 6, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 396 bytes, done. Total 4 (delta 0), reused 0 (delta 0) To gituser@gitserver.com:project.git 7d9ea97.94f7b26 master -> master Now, other developers can view these modifications by updating their local repository. Git - Delete Operation Tom updates his local repository and finds the compiled binary in the sre directory. After viewing the commit message, he realizes that the compiled binary was added by Jerry. [tom@Centos src]$ pwd /home/tom/project/sre [tom@centos src]$ Is Makefile string_operations string_operations.c 0° tutorialspoint shared libs), for GNU/Linux 2.6.18, not stripped [tom@Centos src]$ git log commit 29af9d45947dc044e33d69b9141d8d2dad37cc682 : Jerry Mouse Jed 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]$ Is Makefile string_operations string_operations.c [tom@Centos src]$ git rm string_operations rm_'sre/string_operations’ [tom@Centos sre]$ git commit -a -m "Removed executable binary” [master 5776472] Removed executable binary 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) 0° tutorialspoint Git - Fix Mistakes To err is human. So every VCS provides a feature to fix mistakes until a certain point. Git provides a feature that we can use to undo the modifications that have been made to the local repository. Suppose the user accidentally does some changes to his local repository and then wants to undo these changes. In such cases, the revert operation plays an important role. Revert Uncommitted Changes Let us suppose Jerry accidentally modifies a file from his local repository. But he wants to undo his modification. To handle this situation, we can use the git checkout command. We can use this command to revert the contents of a file. [jerry@Centos src]$ pwd [home/jerry/jerry_repo/project/sre [jerry@Centos sre]$ git status -s Mstring_operations.c [jerry@CentOs src]$ git checkout string_operations.c [jerry@Centos src]$ git status 6€"s Further, we can use the git checkout command to obtain a deleted file from the local repository. Let us suppose Tom deletes a file from the local repository and we want this file back. We can achieve this by using the same command. [tom@Centos src]$ pwd |nome/tom/top_repo/project/src [tom@Centos src]$ Is -1 Makefile string_operations.c 0° tutorialspoint Makefile [tom@Centos src]$ git status -s D string_operations.c Git is showing the letter D before the filename. This indicates that the file has been deleted from the local repository. [tom@Centos sre]$ git checkout string_operations.c [tom@Ccentos sre]$ Is -1 Makefile string_operations.c [tom@Centos sre]$ git status -s Note - We can perform all these operations before commit. Remove Changes from Staging Area We have seen that when we perform an add operation, the files move from the local repository to the stating area. If a user accidently modifies a file and adds it into the staging area, he can revert his changes, by using the git checkout command. In Git, there is one HEAD pointer that always points to the latest commit. if you want to undo a change from the staged area, then you can use the git checkout command, but with the checkout command, you have to provide an additional parameter, ie, the HEAD pointer. The additional commit pointer parameter instructs the git checkout command to reset the working tree and also to remove the staged changes. Let us suppose Tom modifies a file from his local repository. if we view the status of this file, it will show that the file was modified but not added into the staging area, 0° tutorialspoint # Unmodified file [tom@Centos sre]$ git status -s # Modify file and view ita€™s status. [tom@Centos sre]$ git status -s Mstring_operations.c [tom@Centos src]$ git add string_operations.c Git status shows that the file is present in the staging area, now revert it by using the git checkout command and view the status of the reverted file. [tom@Centos src]$ git checkout HEAD -- string_operations.c [tom@Centos sre]$ git status -s Move HEAD Pointer with Git Reset After doing few changes, you may decide to remove these changes. The Git reset command is used to reset or revert changes. We can perform three different types of reset operations. Below diagram shows the pictorial representation of Git reset command. Commit 1 Commit 2 Before git reset command 0° tutorialspoint Commit 1 Commit 2 Commit 3 After git reset command Soft Each branch has a HEAD pointer, which points to the latest commit. If we use Git reset command with --soft option followed by commit ID, then it will reset the HEAD pointer only without destroying anything. .git/refs/neads/master file stores the commit ID of the HEAD pointer. We can verify it by using the git log -1 command. [jerry@Centos project]$ cat .git/refs/heads/master 5776472lled44fe2ae479427a0668a4fl2ed7Ial Now, view the latest commit ID, which will match with the above commit ID. [jerry@Centos project] $ git log -2 The above command will produce the following result. ‘commit 5776472lled44fe2ae479427a0668a4fl2ed71al Author: Tom Cat Date: Wed Sep 11 10:21:20 2013 +0530 Removed executable binary commit 29af9d45947dc044e33d69b9141d8d2dad37ec62 Author: Jerry Mouse Date: Wed Sep 11 10:16:25 2013 +0530 0° tutorialspoint Let us reset the HEAD pointer. [jerry@Centos project]$ git reset --soft HEAD~ Now, we just reset the HEAD pointer back by one position. Let us check the contents of .git/refs/heads/master file. [jerry@Centos project]$ cat git/refs/neads/master 29af9d45947dc044e33d69b9141d8d2dad37cc62 Commit ID from file is changed, now verify it by viewing commit messages. jerry@Centos project] $ git log -2 The above command will produce the following result. commit 29af9d45947dc044e33d69b9141d8d2dad37¢c62 Author: Jerry Mouse Date: Wed Sep 11 10:16:25 2013 +0530 Added compiled binary commit 94f726005f856flalb733ad438e9700cd509cla Author: Jerry Mouse Date: Wed Sep 11 10:08:01 2013 +0530 Added Makefile and renamed strings.c to string_operations.c mixed Git reset with --mixed option reverts those changes from the staging area that have not been committed yet. It reverts the changes from the staging area only. The actual changes made to the working copy of the file are unaffected. The default Git reset is equivalent to the git reset -- mixed 0° tutorialspoint you use nara Opto WILT Ine GI TeseL COMmMmana, 1 win Gear ine staging area; it will reset the HEAD pointer to the latest commit of the specific commit ID and delete the local file changes too. Let us check the commit ID. [jerry@Centos src]$ pwd Jnome|jerry/jerry_repo/project/sre [jerry@Centos sre]$ git log -1 The above command will produce the following result. commit 5776472Iled44fe2ae479427a0668a4fl2ed71al Author: Tom Cat Date: Wed Sep 1 10:21:20 2013 +0530 Removed executable binary Jerry modified a file by adding single-line comment at the start of file. ljerry@Centos src]$ head -2 string_operations.c |* This line be removed by git reset operation */ #include He verified it by using the git status command. [jerry@Centos sre]$ git status -s M string_operations.c Jerry adds the modified file to the staging area and verifies it with the git status command. [jerry@Centos src]$ git add string_operations.c [jerry@Centos sre]$ git status The above command will produce the following result. 0° tutorialspoint # (use "git reset HEAD . # # modified: string_operations.c # to unstage) Git status is showing that the file is present in the staging area. Now, reset HEAD with -- hard option. [jerry@centos src]$ git reset --hard 5776472lled44fe2ae479427a0668a4fl2ed7Ial HEAD is now at 5776472 Removed executable binary Git reset command succeeded, which will revert the file from the staging area as well as remove any local changes made to the file. [jerry@Centos src]$ git status -s Git status is showing that the file has been reverted from the staging area. [jerry@CentOs src]$ head -2 string_operations.c #include The head command also shows that the reset operation removed the local changes too. Git - Tag Operation 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 0° tutorialspoint tom@CentOs project]$ pwd |nome/tom/top_repo/project [tom@Centos project]$ git tag -a 'Release_1_0' -m ‘Tagged basic string operation ¢ TS > 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: I, done. writing objects: 100% (1/1), 183 bytes, done. Total | (delta 0), reused 0 (delta 0) To gituser@gitserver.com: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 a€"I option. [jerry@Centos src]$ pwd /home/jerry/jerry_repo/project/sre [jerry@Centos sre]$ git pull remote: Counting objects: 1, done. remote: Total | (delta 0), reused 0 (delta 0) Unpacking objects: 100% (1/1), done. From git.server.com:project * [new tag] Release _1_0 -> Release_l_0 0° tutorialspoint 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_o Tagger: Tom Cat Date: Wed Sep 11 18:45:54 2013 +0530 Tagged basic string operation code commit 5776472Iled44fe2ae479.427a0668a4f12ed71al Author: Tom Cat Date: Wed Sep 1 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 0° tutorialspoint [tom@CentOs project]$ git push origin :Release_1_0 To gituser@gitserver.com:project.git - [deleted] Release_1_0 Git - Patch Operation 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 |nome|jerry/jerry_repo/project/sre [jerry@Centos src]$ git status -s Mstring_operations.c 2? 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 0° tutorialspoint To apply patch and create commit, use the following command - [tom@Centos sre]$ pwd [home/tom/top_repo/project/sre [tom@centos src]$ git diff [tom@Centos sre]$ git status 4€"s [tom@centos src]$ git apply 0001-Added-my__strcat-function.patch [tom@Centos src]$ git status -s M string_operations.c 2? 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/sre/string_operations.c b/sre/string_operations.c index 8ab7t42.282!cf 100644 --- a/src/string_operations.c +++ b/src/string_operations.c @@ -15 +116 @@ #include +char *my_streat(char *t, char *s) diff --git a/src/string_operations.c b/src/string_operations.c index 8ab7f42..4282fcf 100644 --- a/sre/string_operations.c +++ b/src/string_operations.c @@ -15 +116 @@ #include +char *my_streat(char *t, char *s) + 0° tutorialspoint + + + while (*p) +t, + while (p++ = *s++) +i + return t; + } + size_t my_strlen(const char *s) { const char *p = s; @@ -23,6 +34,7 @@ int main(void) { Git - Managing Branches Branch operation allows creating another line of development. We can use this operation to fork off the development process into two different directions. For example, we released a product for 6.0 version and we might want to create a branch so that the development of 7.0 features can be kept separate from 6.0 bug fixes. Create Branch Tom creates a new branch using the git branch command. We can create a new branch from an existing one. We can use a specific commit or tag as the starting point. If any specific commit ID is not provided, then the branch will be created with HEAD as its starting point. [jerry@Centos src]$ git branch new_branch [jerry@Centos src]$ git branch 0° tutorialspoint Anew branch is created; Tom used the git branch command to list the available branches. Git shows an asterisk mark before currently checked out branch. The pictorial representation of create branch operation is shown below - | Commit 1 Commit 2 Commit 3 Before create branch command Master Commit 1 Commit 2 Commit 3 New branch Switch between Branches Jerry uses the git checkout command to switch between branches. After create branch operation [jerry@Centos sre]$ git checkout new_branch Switched to branch ‘new_branch’ 0° tutorialspoint Shortcut to Create and Switch Branch In the above example, we have used two commands to create and switch branches, respectively. Git provides @€“b option with the checkout command; this operation creates a new branch and immediately switches to the new branch. [jerry@Centos sre]$ git checkout -b test_branch Switched to a new branch ‘test_branch’ [jerry@Centos src]$ git branch master new. branch *test_branch Delete a Branch Abranch can be deleted by providing a€"D option with git branch command. But before deleting the existing branch, switch to the other branch. Jerry is currently on test_branch and he wants to remove that branch. So he switches branch and deletes branch as shown below. [jerry@Centos sre]$ git branch master new_branch * test_branch [jerry@Centos sre]$ git checkout master Switched to branch ‘master’ [jerry@Centos sre]$ git branch -D test_branch Deleted branch test_branch (was 5776472). Now, Git will show only two branches. 0° tutorialspoint new. branch Rename a Branch Jerry decides to add support for wide characters in his string operations project. He has already created a new branch, but the branch name is not appropriate. So he changes the branch name by using @€“m option followed by the old branch name and the new branch name. [jerry@Centos src]$ git branch * master new_branch [jerry@CentOs src]$ git branch -m new_branch wchar_support Now, the git branch command will show the new branch name. [jerry@Centos src]$ git branch * master wehar_support Merge Two Branches Jerry implements a function to return the string length of wide character string. New the code will appear as follows - [jerry@Centos src]$ git branch master * wchar_support [jerry@Centos src]$ pwd /home/jerry/jerry_repo/project/sre [ierry@Centos sre]$ git diff 0° tutorialspoint Taysrc/string_operanions.¢ py sreystring_operauons.¢ index 8ab7t42..8fb4b00 100644 --- a/sre/string_operations.c +++ b/sre/string_operations.c @@ -14 +114 @@ #include +#include + +size_t w_strlen(const wchar_t *s) + { + const wchar_t *p = s; + + while (*p) ++4p; + return (p- s); + } After testing, he commits and pushes his changes to the new branch. [jerry@Centos src]$ git status -s Mstring_operations.c 2? string_operations [jerry@CentOs src]$ git add string_operations.c [jerry@CentoOs src]$ git commit -m ‘Added w_strien function to return string lenght « string’ [wchar_support 64192f9] Added w_-strlen function to return string lenght of wchar_t files changed, 10 insertions(+), 0 deletions(-) Note that Jerry is pushing these changes to the new branch, which is why he used the branch name wehar_support instead of master branch. 0° tutorialspoint The above command will produce the following result. Counting objects: 7, done. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 507 bytes, done. Total 4 (delta 1), reused 0 (delta 0) To gituser@git server.com:project.git * [new branch] wchar_support -> wchar_support After committing the changes, the new branch will appear as follows - Commit 1 |<— Commit 2 Commit 3 After commit in new branch Tom is curious about what Jerry is doing in his private branch and he checks the log from the wehar_support branch. [tom@Centos src]$ pwd [home/tom/top _repo/project/sre [tom@Centos src]$ git log origin/wchar_support -2 The above command will produce the following result. commit 64192f91d7cc2bedf3bf946dd33ece63b74184a3 Author: Jerry Mouse 0° tutorialspoint commit 5776472Iled44fe2ae479427a0668a4fl2ed71al Author: Tom Cat Date: Wed Sep Il 10:21:20 2013 +0530 Removed executable binary By viewing commit messages, Tom realizes that Jerry implemented the strien function for wide character and he wants the same functionality in the master branch. instead of re-implementing, he decides to take Jerrya€™s code by merging his branch with the master branch. [tom@Centos project]$ git branch * master [tom@Centos project]$ pwd /home/tom/top_repo/project [tom@Centos project]$ git merge origin/wchar_support Updating 5776472..64192f9 Fast-forward src/string_operations.c | 10 ++++++++++ files changed, 10 insertions(+), 0 deletions(-) After the merge operation, the master branch will appear as follows - 0° tutorialspoint Commit 1 Commit 2 Commit 3 After branch merge Now, the branch wehar_support has been merged with the master branch. We can verify it by viewing the commit message or by viewing the modifications done into the string_operation.c file. [tom@CentOs project]$ cd sre/ [tom@Centos src]$ git log -1 commit 64192f91d7cc2bcdf3bf946dd33ece63b74184a3 Author: Jerry Mouse Date: Wed Sep 11 16:10:06 2013 +0530 Added w_strlen function to return string lenght of wchar_t string [tom@CentoOs src]$ head -12 string_operations.c The above command will produce the following result. #include #include size_t w_strlen(const wchar_t *s) { const wehar_t *p = while (*p) +p; 0° tutorialspoint After testing, he pushes his code changes to the master branch. [tom@CentoS src]$ git push origin master Total 0 (delta 0), reused 0 (delta 0) To gituser@git server. comproject git 5776472..6419219 master -> master Rebase Branches The Git rebase command is a branch merge command, but the difference is that it modifies the order of commits. The Git merge command tries to put the commits from other branches on top of the HEAD of the current local branch. For example, your local branch has commits A->B->C->D and the merge branch has commits A->B->X->Y, then git merge will convert the current local branch to something like A->B->C->D->X->Y The Git rebase command tries to find out the common ancestor between the current local branch and the merge branch. It then pushes the commits to the local branch by modifying the order of commits in the current local branch. For example, if your local branch has commits A->B->C->D and the merge branch has commits A->B->X->Y, then Git rebase will convert the current local branch to something like A->B->X->Y->C->D. When multiple developers work on a single remote repository, you cannot modify the order of the commits in the remote repository. in this situation, you can use rebase operation to put your local commits on top of the remote repository commits and you can push these changes. Git - Handling Conflicts Perform Changes in wchar_support Branch Jerry is working on the wehar_support branch. He changes the name of the functions and after testing, he commits his changes. 0° tutorialspoint * wehar_support lierry@Centos src]$ git diff The above command produces the following result - diff --git a/src/string_operations.c b/sre/string_operations.c index 8fb4b00.,01ff4e0 100644 --- a/sre/string_operations.c +++ b/sre/string_operations.c @@-17+17 @@ #include #include -size_t w_strlen(const wchar_t *s) +size_t my_wstrlen(const wehar_t *s) { const wehar_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 sre]$ git commit -m ‘Changed function name’ [wehar_support 3789fe8] Changed function name files changed, | insertions(+), | 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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy