Format log output

Posted by pietbez on Tue, 09 Nov 2021 21:49:05 +0100

Transferred from: https://www.cnblogs.com/irocker/p/advanced-git-log.html

 

Format log output

oneline

--The oneline tag compresses each commit into one line. By default, a commit ID and the first line of the commit description are displayed. The output is as follows:

0e25143 Merge branch 'feature' ad8621a Fix a bug in the feature 16b36c6 Add a new feature 23ad9ad Add the initial code base

 

decorate

Many times, it is very useful to know which branch or tag the commit is associated with. The -- modify tag will make git log display the reference of each commit (such as branch, tag, etc.)

 

It can be used in combination with other config options. For example, GIT log -- oneline -- modify output is as follows:

0e25143 (HEAD, master) Merge branch 'feature' ad8621a (feature) Fix a bug in the feature 16b36c6 Add a new feature 23ad9ad (tag: v0.9) Add the initial code base

It can be seen that the first commit is in the master branch, the second commit is in the feature branch, and the fourth commit is tag ged as v0.9

 

diffs

git log has many options to show the differences of each commit. The two most commonly used are -- stat and - p

 

--stat displays the number of added lines and deleted lines of each file in each commit. This is very useful for us to understand the general changes of a commit. The following commit hello.py adds 67 lines and deletes 38 lines:

1 2 3 4 5 6 7 8 commit f2a238924e89ca1d4947662928218a06d39068c3 Author: John <john@example.com> Date:   Fri Jun 25 17:30:28 2014 -0500       Add a new feature    hello.py | 105 ++++++++++++++++++++++++-----------------  1 file changed, 67 insertion(+), 38 deletions(-)

The + - symbol after the file name is the relative number of lines added and deleted from the file

 

If you want to see what specific changes have been made to each commit, you can use git log -p, and the results are as follows:

commit 16b36c697eb2d24302f89aa22d9170dfe609855b Author: Mary <mary@example.com> Date:   Fri Jun 25 17:31:57 2014 -0500       Fix a bug in the feature   diff --git a/hello.py b/hello.py index 18ca709..c673b40 100644 --- a/hello.py +++ b/hello.py @@ -13,14 +13,14 @@ B -print("Hello, World!") +print("Hello, Git!")

 

shortlog

git shortlog is a special version of git log. Its purpose is to create a published notification. Commit is grouped according to the author and the first line description of each commit is displayed. Through it, you can easily see who has done what

 

For example, two developers have contributed five commitments to a project. The output of GIT shortlog is as follows:

Mary (2):       Fix a bug in the feature       Fix a serious security hole in our framework   John (3):       Add the initial code base       Add a new feature       Merge branch 'feature'

By default, git shortlog is sorted by author's name. You can use - n to sort by the number of commit s of each author

 

graph

--The graph tag will draw an ASCII diagram to show the branch structure of the commit history. It is usually used in combination with -- oneline --decorate:

git log --graph --oneline --decorate

  

*   0e25143 (HEAD, master) Merge branch 'feature' |\  | * 16b36c6 Fix a bug in the new feature | * 23ad9ad Start a new feature * | ad8621a Fix a critical security issue |/  * 400e4b7 Fix typos in the documentation * 160e224 Add the initial code base

The asterisk tells you which branch the commit is in. The above output tells us that 23ad9ad and 16b36c6 are in one subject branch and the others are in the master branch

 

Custom output

You can use -- pretty = format: "< string >" to customize the output  

 

For example,% cn stands for commiter name and% h stands for the abbreviation of commit hash,  % cd stands for committer date

git log --pretty=format:"%cn committed %h on %cd"

The output is as follows:

John committed 400e4b7 on Fri Jun 24 12:30:04 2014 -0500 John committed 89ab2cf on Thu Jun 23 17:09:42 2014 -0500 Mary committed 180e223 on Wed Jun 22 17:21:19 2014 -0500 John committed f12ca28 on Wed Jun 22 13:50:31 2014 -0500

 

Filter commit history

By quantity

You can use - n to limit the number of outputs. The following example shows only the last three commit s

git log -3

 

By date

You can use -- after or -- before to filter by date. The following example only shows the commit after July 1, 2014 (including July 1)

git log --after="2014-7-1"

You can also use a relative time, such as "1 week ago" and "yesterday"“

git log --after="yesterday"

If you look at the commit of a certain period of time, you can use -- after and -- before at the same time. The following example shows the commit between July 1, 2014 and July 4, 2014:

git log --after="2014-7-1" --before="2014-7-4"

Note that -- since --until and -- after --before mean the same thing

 

By author

git log --author="John"

Show the commit ment of John's contribution. The author name doesn't need to match exactly -- just include it

 

You can also use regular expressions. The following command searches for the commit ment contributed by Mary and John

git log --author="John\|Mary"

Note that this -- author contains not only the name but also email, so you can use this to search email

  

Description by commit

For example, if your team adds the relevant issue number to each commit description, you can use the following command to find the commit related to an issue:

git log --grep="JRA-224"

You can also pass - i to ignore case

 

according to document

Sometimes you may only be interested in the modification of a file. You just want to view the historical information related to a file. You just need to insert the path of the file you are interested in. The following example only returns the commit related to foo.py or bar.py:

git log -- foo.py bar.py

Here -- is to tell Git that the following parameters are the file path rather than the name of the branch. If the following file path will not be confused with a branch, you can omit --

 

By content

Sometimes you want to search for a commit related to adding or deleting a line of code. You can use - s "< string >". The following example assumes that you want to know when Hello, World! Was added to the project, you can use the following command:

git log -S"Hello,World!"

If you want to use regular expressions instead of strings, you can use - G instead of - S

 

This is a very useful debug tool. With it, you can locate all commit s related to a line of code. You can even see when a line is copied and moved to another file

 

By range

You can view the commit of a range:

git log <since>..<until>

This command is very useful. When you use branch as the range parameter, it can easily display the differences between the two branches. Take a look at the following command:

git log master..feature

The range of master..feature contains all commit ments that exist in the feature but not in the master. As shown in the figure (the lowest master..feature in the figure should be wrong, and only the feature..master is right):

feature..master contains all commit s that the master has but the feature does not

 

Filter merge commit

By default, git log will output merge commit   You can filter out the merge commit through the -- no merges tag:

git log --no-merges

If you are only interested in merge commit, you can use -- merges:

git log --merges