top
April flash sale

Search

GIT Tutorial

1. Finding contenta) git describeThis command gives a commit object a human readable name based on an available reference.The command finds the most recent tag that is reachable from a commit. If the tag points to the commit, then only the tag is shown; otherwise it suffixes the tag name with the number of additional commits on top of the tagged object and the abbreviated object name of the most recent commitClone and get the working directory for the project: myProjcd /Users/Divya1/Documents/GIT/gitRepos/myProj git clone http://github.com/greets/myProj.gitTags: are the save-points or stable commit points in the commit history of your source code.In other words:Tags are a friendly name to commits SHA-11) List the tags:git tag –list newFeature_add t_featureB v0.1 v1.0 v1.82) Create an annotated tag from the recent commit:git tag -a release_r1.5 -m 'Tag the release version 1.5'git tag --list newFeature_add release_r1.5 t_featureB v0.1 v1.0 v1.83) To checkout the project snapshot of a commit that is taggedgit checkout v1.8 Note: checking out 'v1.8'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:  git checkout -b <new-branch-name> HEAD is now at 018d46a changes in the code NOTE: It is similar to working on an old commit from the project history, you could edit the project files and create a new branch out of here with your changed commit objects over it.Now these changes in the history of the project can be shared with the peers by merging in to the main branch (‘master’ in this case).4) To create a tag from an old commit idLet’s create another tag from tag: v1.8; so that both point to the same object id.#Find out tag: v18 points to which commit object  git show v1.8 --no-patch tag v1.8 Tagger: divya <divya_bhushan@hotmail.com> Date:   Wed Aug 15 17:31:21 2018 +0530 tag: 018d46a - changes in the code commit 018d46aaf4177a0964e78e80c8e711771afff7c2 (tag: v1.8, tag: show, origin/branch_v1.8) Author: divya <divya_bhushan@hotmail.com> Date:   Thu Mar 5 19:46:12 2015 +0530 changes in the codeV1.8 points to 018d46a commit object#List all the tags pointing to the same commit object id: 018d46a Divya1@Divya:myProj [master] $git tag --points-at=018d46a v1.8#Create v1.3 from v1.8 Divya1@Divya:myProj [master] $git tag -a v1.3 018d46 -m '018d46: tagging'#List all the tags pointing to the same commit object id: 018d46a git tag --points-at=018d46a v1.8 v1.3NOTE: Now tag: v1.3 has been created; that also points to the same object id as tag: v1.8Get the latest tag reachable from the latest commit object:Git describe latest tag:Divya1@Divya:myProj [master] $git describe t_featureB-13-g9ecf5bdTag: t_featureB has 12 commits over it, fb5d4ee is the latest commit.“g” stands for git (SCM), others like: CVS, Perforce, ClearCase etc…To apply a tag into your current branch run the ‘git merge’ command giving the tag name as the commit id.Example:[dev] $ git merge v1.3b) git grep: git grep command print lines matching a patternProject: /Users/Divya1/Documents/GIT/gitRepos/myProj-- Find all files whose content contains the string “bug”myProj [master]$git grep -i "bug" code/welcome.sh:  echo "Bug fixed." hotfix/hotfix.html:Fixing the bug: 101--Count the number of times the pattern occurred in each file--show the count of pattern “fix” that occurred in each file. git grep -ic "fix" code/editInfo.info:1 code/welcome.sh:4 hotfix/hotfix.html:2 iss54/iss54.log:1 temp/temp.tmp:1--Print the line number at which the search string occursmyProj [master] $git grep -in "fix" code/editInfo.info:5:hotfix code/welcome.sh:20:echo "HOTFIX ADDED !!!" code/welcome.sh:21:echo "quickfix branch" code/welcome.sh:22:quickfix_func(){ code/welcome.sh:23:  echo "Bug fixed." hotfix/hotfix.html:3:<title>Hotfix page</title> hotfix/hotfix.html:6:Fixing the bug: 101 iss54/iss54.log:3:fix. temp/temp.tmp:10:hotfix branchi = case-insensitiven = show line numbersc = count the occurrences-- List all the filenames whose content contains the pattern--List all the filenames that contains the string “feature” myProj [master] $git grep -il "feature" featureB/README.txt temp/temp.tmp2. Debugging – git debuga) File annotationThis tool tells you which commit was responsible for that bug, when, why and by whom was it introduced.git blameproject: myProjgit checkout master Git blame example: --blame all the linesgit blame code/welcome.sh--Print only certain line numbers in the debug mode[master] $git blame -L 11,20 code/welcome.sh 9ecf5bd7 (administrator 2018-09-07 13:40:18 +0530 11) adminTasks 9ecf5bd7 (administrator 2018-09-07 13:40:18 +0530 12) echo "TimeStamp:"`date "+%Y_%m_%d_%H:%M:%S"` e69eee03 (greets        2014-11-14 22:50:35 +0530 13) #today ^86792c6 (greets        2014-11-14 22:37:07 +0530 14) echo "Hello $LOGNAME !" 36b2f386 (divya         2015-03-05 12:05:37 +0530 15) echo "Changes committed" 018d46aa (divya         2015-03-05 19:46:12 +0530 16) echo "changes staged" 018d46aa (divya         2015-03-05 19:46:12 +0530 17) echo "changes only in working dir" 5bdd026e (divya         2015-03-05 19:46:12 +0530 18) echo "changes staged" 5bdd026e (divya         2015-03-05 19:46:12 +0530 19) echo "changes only in working dir" 904ce2b9 (greets        2017-12-15 19:55:35 +0530 20) echo "HOTFIX ADDED !!!"field 1: SHA-1field 2: author name and the authored datefield 3: content of the fileNOTE:Lines beginning with ^ are the lines in the file which were introduced in the repositories initial commit and have remained unchanged.You could use “-w” flag to ignore the white space changes.b) Bisect: Binary searchUseful when you don’t know what code is breaking, you are not sure of when was the last time the code was working.The bisect command does a binary search through your commit history to help you identify as quickly as possible which commit introduced an issue.Lab Exercises 1) Identify the cause of a bug. Project: ~/Onedrive/gitRepos/bisectTest Run the ./code/runTest.sh script, that fails with the error message(un-expected):bisectTest [master] $./code/runTest.sh [OK]: /Users/Divya1/OneDrive/gitRepos/bisectTest/log/err.out file exists ./code/runTest.sh: line 21: no_function: command not found Process failed !!! returning to the main program...2) Debug the code --start the bisect mode bisectTest [master] $git bisect start* 035f294 2019-01-26 | Add date in the commonLibrary.sh script (HEAD -> master) [divya bhushan] * c220d66 2019-01-26 | Add data/data.log file [divya bhushan] * eee1437 2019-01-26 | Introduce a bug in the script [divya bhushan] * 6a9fb7a 2019-01-26 | Add the .gitignore rules file [divya bhushan] * 654e9b2 2019-01-26 | testing the script fine. ready for deployment [divya bhushan] * bba6094 2018-09-27 | Add exit_status lib function [divya] * b4161e6 2018-09-27 | Adding log directory and err.out file [divya] * e4d3ed3 2018-09-27 | Adding library functions [divya] * ff019ad 2018-09-27 | Adding the initial project structure [divya]Commit id: 654e9b2 was the last commit where the deployment happened.BONUS: You could also checkout to the commit id: 654e9b2 and test the code/runTest.sh script ran fine.--Pass on the last known good commit bisectTest [master] $git bisect good 654e9b2 --Also pass the latest bad commit id bisectTest [master] $git bisect bad 035f294 Bisecting: 1 revision left to test after this (roughly 1 step) [eee1437b9ce95b6ac3690cd0f541786b09f468a0] Introducing a bug in the script bisectTest [(no branch, bisect started on master)]NOTE:HEAD pointer of current ‘master’ branch points jumps back to the eee1437 commit id in the middle of the range of good and bad commit history.Bisect command checks out ‘eee1437’.Compile and test the checked-out version in this snapshot, if the code here works fine type ‘git bisect good’ if not type ‘git bisect bad’Keep repeating the process until you find the commit snapshot with the good code.After a bisect session, to clean up the bisection state and return to the original HEAD, issue the following command:bisectTest [(no branch, bisect started on master)] $./code/runTest.sh [OK]: /Users/Divya1/OneDrive/gitRepos/bisectTest/log/err.out file exists ./code/runTest.sh: line 21: no_function: command not found Process failed !!! returning to the main program... bisectTest [(no branch, bisect started on master)] $git bisect bad Bisecting: 0 revisions left to test after this (roughly 0 steps) [6a9fb7a01f0cf4afa5c739024b33d3083e31fff9] Add the .gitignore rules file bisectTest [(no branch, bisect started on master)] $./code/runTest.sh [OK]: /Users/Divya1/OneDrive/gitRepos/bisectTest/log/err.out file existsbisectTest [(no branch, bisect started on master)] $git bisect good eee1437b9ce95b6ac3690cd0f541786b09f468a0 is the first bad commit commit eee1437b9ce95b6ac3690cd0f541786b09f468a0 Author: divya bhushan <divya_bhushan@hotmail.com> Date:   Sat Jan 26 10:34:26 2019 +0100    Introduce a bug in the script :040000 040000 319a812a5c080f70a74fb8b3de93b1fcaced875f 611b3b9eac89e8228c6a1567a4142d002185b0a8 M codebisectTest [(no branch, bisect started on master)] $git bisect reset Previous HEAD position was 6a9fb7a Add the .gitignore rules file Switched to branch 'master'CONCLUSTION:‘git bisect’ command checked the good version of the code in the previous snapshots between the good and bad commits. It checked-out the earlier snapshot and allowed you to compile and run the code.eee1437 is the first bad commit: Introduce a bug in the scriptLogically 6a9fb7a becomes the last known good project snapshot where the code was fine.To fix the broken code get the good code from this old commit snapshot and test it:bisectTest [master] $git checkout 6a9fb7a code/runTest.sh Divya1@Divya:bisectTest [master] $./code/runTest.sh [OK]: /Users/Divya1/OneDrive/gitRepos/bisectTest/log/err.out file existsRun a ‘git diff’ command to see the code of broken lines removed:bisectTest [master] $git diff --staged code/runTest.sh diff --git a/code/runTest.sh b/code/runTest.sh index 03a1c5d..a023b60 100755 --- a/code/runTest.sh +++ b/code/runTest.sh @@ -17,10 +17,6 @@ else        echo "\n[NOK]: $FILENAME file does not exists\n" fi -#introduce a bug: Calling a function that does not exist in the library file. -no_function -exit_status - exit 0 }‘git status’ shows the modified file.bisectTest [master] $git status On branch master Changes to be committed:  (use "git reset HEAD <file>..." to unstage) modified:   code/runTest.shSummary: Fixed the broken code. 
logo

GIT Tutorial

Identifying content

1. Finding content

a) git describe

This command gives a commit object a human readable name based on an available reference.

The command finds the most recent tag that is reachable from a commit. If the tag points to the commit, then only the tag is shown; otherwise it suffixes the tag name with the number of additional commits on top of the tagged object and the abbreviated object name of the most recent commit

Clone and get the working directory for the project: myProj

cd /Users/Divya1/Documents/GIT/gitRepos/myProj
git clone http://github.com/greets/myProj.git

Tags: are the save-points or stable commit points in the commit history of your source code.

In other words:

Tags are a friendly name to commits SHA-1

1) List the tags:

git tag –list

newFeature_add
t_featureB
v0.1
v1.0
v1.8

2) Create an annotated tag from the recent commit:

git tag -a release_r1.5 -m 'Tag the release version 1.5'

git tag --list

newFeature_add
release_r1.5
t_featureB
v0.1
v1.0
v1.8

3) To checkout the project snapshot of a commit that is tagged

git checkout v1.8
Note: checking out 'v1.8'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

 git checkout -b <new-branch-name>
HEAD is now at 018d46a changes in the code

NOTE: It is similar to working on an old commit from the project history, you could edit the project files and create a new branch out of here with your changed commit objects over it.

Now these changes in the history of the project can be shared with the peers by merging in to the main branch (‘master’ in this case).

4) To create a tag from an old commit id

Let’s create another tag from tag: v1.8; so that both point to the same object id.

#Find out tag: v18 points to which commit object 
git show v1.8 --no-patch

tag v1.8
Tagger: divya <divya_bhushan@hotmail.com>
Date:   Wed Aug 15 17:31:21 2018 +0530

tag: 018d46a - changes in the code

commit 018d46aaf4177a0964e78e80c8e711771afff7c2 (tag: v1.8, tag: show, origin/branch_v1.8)
Author: divya <divya_bhushan@hotmail.com>
Date:   Thu Mar 5 19:46:12 2015 +0530

changes in the code

V1.8 points to 018d46a commit object

#List all the tags pointing to the same commit object id: 018d46a
Divya1@Divya:myProj [master] $git tag --points-at=018d46a

v1.8

#Create v1.3 from v1.8

Divya1@Divya:myProj [master] $git tag -a v1.3 018d46 -m '018d46: tagging'

#List all the tags pointing to the same commit object id: 018d46a
git tag --points-at=018d46a

v1.8
v1.3

NOTE: Now tag: v1.3 has been created; that also points to the same object id as tag: v1.8

Get the latest tag reachable from the latest commit object:

Git describe latest tag:

Divya1@Divya:myProj [master] $git describe

t_featureB-13-g9ecf5bd

Tag: t_featureB has 12 commits over it, fb5d4ee is the latest commit.

“g” stands for git (SCM), others like: CVS, Perforce, ClearCase etc…

To apply a tag into your current branch run the ‘git merge’ command giving the tag name as the commit id.

Example:

[dev] $ git merge v1.3

b) git grep: 

git grep command print lines matching a pattern

Project: /Users/Divya1/Documents/GIT/gitRepos/myProj

-- Find all files whose content contains the string “bug”

myProj [master]$git grep -i "bug"

code/welcome.sh:  echo "Bug fixed."
hotfix/hotfix.html:Fixing the bug: 101

--Count the number of times the pattern occurred in each file

--show the count of pattern “fix” that occurred in each file.
git grep -ic "fix"

code/editInfo.info:1
code/welcome.sh:4
hotfix/hotfix.html:2
iss54/iss54.log:1
temp/temp.tmp:1

--Print the line number at which the search string occurs

myProj [master] $git grep -in "fix"

code/editInfo.info:5:hotfix
code/welcome.sh:20:echo "HOTFIX ADDED !!!"
code/welcome.sh:21:echo "quickfix branch"
code/welcome.sh:22:quickfix_func(){
code/welcome.sh:23:  echo "Bug fixed."
hotfix/hotfix.html:3:<title>Hotfix page</title>
hotfix/hotfix.html:6:Fixing the bug: 101
iss54/iss54.log:3:fix.
temp/temp.tmp:10:hotfix branch

i = case-insensitive

n = show line numbers

c = count the occurrences

-- List all the filenames whose content contains the pattern

--List all the filenames that contains the string “feature”
myProj [master] $git grep -il "feature"

featureB/README.txt
temp/temp.tmp

2. Debugging – git debug

a) File annotation

This tool tells you which commit was responsible for that bug, when, why and by whom was it introduced.

  1. git blame

project: myProj

git checkout master

Git blame example:
--blame all the lines
git blame code/welcome.sh

--Print only certain line numbers in the debug mode

[master] $git blame -L 11,20 code/welcome.sh

9ecf5bd7 (administrator 2018-09-07 13:40:18 +0530 11) adminTasks
9ecf5bd7 (administrator 2018-09-07 13:40:18 +0530 12) echo "TimeStamp:"`date "+%Y_%m_%d_%H:%M:%S"`
e69eee03 (greets        2014-11-14 22:50:35 +0530 13) #today
^86792c6 (greets        2014-11-14 22:37:07 +0530 14) echo "Hello $LOGNAME !"
36b2f386 (divya         2015-03-05 12:05:37 +0530 15) echo "Changes committed"
018d46aa (divya         2015-03-05 19:46:12 +0530 16) echo "changes staged"
018d46aa (divya         2015-03-05 19:46:12 +0530 17) echo "changes only in working dir"
5bdd026e (divya         2015-03-05 19:46:12 +0530 18) echo "changes staged"
5bdd026e (divya         2015-03-05 19:46:12 +0530 19) echo "changes only in working dir"
904ce2b9 (greets        2017-12-15 19:55:35 +0530 20) echo "HOTFIX ADDED !!!"

field 1: SHA-1

field 2: author name and the authored date

field 3: content of the file

NOTE:

Lines beginning with ^ are the lines in the file which were introduced in the repositories initial commit and have remained unchanged.

You could use “-w” flag to ignore the white space changes.

b) Bisect: Binary search

Useful when you don’t know what code is breaking, you are not sure of when was the last time the code was working.
The bisect command does a binary search through your commit history to help you identify as quickly as possible which commit introduced an issue.

Lab Exercises 

1) Identify the cause of a bug. 


Project: ~/Onedrive/gitRepos/bisectTest
Run the ./code/runTest.sh script, that fails with the error message(un-expected):

bisectTest [master] $./code/runTest.sh

[OK]: /Users/Divya1/OneDrive/gitRepos/bisectTest/log/err.out file exists

./code/runTest.sh: line 21: no_function: command not found

Process failed !!! returning to the main program...

2) Debug the code 


--start the bisect mode
bisectTest [master] $git bisect start

* 035f294 2019-01-26 | Add date in the commonLibrary.sh script (HEAD -> master) [divya bhushan]
* c220d66 2019-01-26 | Add data/data.log file [divya bhushan]
* eee1437 2019-01-26 | Introduce a bug in the script [divya bhushan]
* 6a9fb7a 2019-01-26 | Add the .gitignore rules file [divya bhushan]
* 654e9b2 2019-01-26 | testing the script fine. ready for deployment [divya bhushan]
* bba6094 2018-09-27 | Add exit_status lib function [divya]
* b4161e6 2018-09-27 | Adding log directory and err.out file [divya]
* e4d3ed3 2018-09-27 | Adding library functions [divya]
* ff019ad 2018-09-27 | Adding the initial project structure [divya]

Commit id: 654e9b2 was the last commit where the deployment happened.

BONUS: You could also checkout to the commit id: 654e9b2 and test the code/runTest.sh script ran fine.

--Pass on the last known good commit
bisectTest [master] $git bisect good 654e9b2

--Also pass the latest bad commit id
bisectTest [master] $git bisect bad 035f294
Bisecting: 1 revision left to test after this (roughly 1 step)
[eee1437b9ce95b6ac3690cd0f541786b09f468a0] Introducing a bug in the script
bisectTest [(no branch, bisect started on master)]

NOTE:

HEAD pointer of current ‘master’ branch points jumps back to the eee1437 commit id in the middle of the range of good and bad commit history.
Bisect command checks out ‘eee1437’.
Compile and test the checked-out version in this snapshot, if the code here works fine type ‘git bisect good’ if not type ‘git bisect bad’
Keep repeating the process until you find the commit snapshot with the good code.

After a bisect session, to clean up the bisection state and return to the original HEAD, issue the following command:

bisectTest [(no branch, bisect started on master)] $./code/runTest.sh

[OK]: /Users/Divya1/OneDrive/gitRepos/bisectTest/log/err.out file exists

./code/runTest.sh: line 21: no_function: command not found

Process failed !!! returning to the main program...

bisectTest [(no branch, bisect started on master)] $git bisect bad
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[6a9fb7a01f0cf4afa5c739024b33d3083e31fff9] Add the .gitignore rules file

bisectTest [(no branch, bisect started on master)] $./code/runTest.sh

[OK]: /Users/Divya1/OneDrive/gitRepos/bisectTest/log/err.out file exists

bisectTest [(no branch, bisect started on master)] $git bisect good
eee1437b9ce95b6ac3690cd0f541786b09f468a0 is the first bad commit
commit eee1437b9ce95b6ac3690cd0f541786b09f468a0
Author: divya bhushan <divya_bhushan@hotmail.com>
Date:   Sat Jan 26 10:34:26 2019 +0100

   Introduce a bug in the script

:040000 040000 319a812a5c080f70a74fb8b3de93b1fcaced875f 611b3b9eac89e8228c6a1567a4142d002185b0a8 M code
bisectTest [(no branch, bisect started on master)] $git bisect reset
Previous HEAD position was 6a9fb7a Add the .gitignore rules file
Switched to branch 'master'

CONCLUSTION:

‘git bisect’ command checked the good version of the code in the previous snapshots between the good and bad commits. It checked-out the earlier snapshot and allowed you to compile and run the code.

eee1437 is the first bad commit: Introduce a bug in the script

Logically 6a9fb7a becomes the last known good project snapshot where the code was fine.

To fix the broken code get the good code from this old commit snapshot and test it:

bisectTest [master] $git checkout 6a9fb7a code/runTest.sh
Divya1@Divya:bisectTest [master] $./code/runTest.sh

[OK]: /Users/Divya1/OneDrive/gitRepos/bisectTest/log/err.out file exists

Run a ‘git diff’ command to see the code of broken lines removed:

bisectTest [master] $git diff --staged code/runTest.sh
diff --git a/code/runTest.sh b/code/runTest.sh
index 03a1c5d..a023b60 100755
--- a/code/runTest.sh
+++ b/code/runTest.sh
@@ -17,10 +17,6 @@ else
       echo "\n[NOK]: $FILENAME file does not exists\n"
fi
-#introduce a bug: Calling a function that does not exist in the library file.
-no_function
-exit_status
-
exit 0
}

‘git status’ shows the modified file.

bisectTest [master] $git status
On branch master
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)

           modified:   code/runTest.sh

Summary: Fixed the broken code. 

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Ezeelogin

Thanks for the post. I liked the way all the details have been provided!

stephen

Firstly thanks for providing this tutorial from this article I have gathered lots of information. I can say that whatever the information provided by knowledgeHut is more useful to know more about the git.

Saurabh

Written nicely