Git Log Advanced Usage

Source: Internet
Author: User
Tags git commands

Format Log Output

First, this article will show several git log examples of formatted output. Most examples are simply tagged to git log request more or less information.

If you don't like the default git log format, you can use git config the alias feature to create a shortcut to the format you want.

Oneline

--onelinetag to compress each commit into a single row. It displays only the first line of the submission ID and submission information by default. git log --onelinethe output is generally like this:

0e25143 Merge branch ‘feature‘ad8621a Fix a bug in the feature16b36c6 Add a new feature23ad9ad Add the initial code base

It is useful for getting the overall picture of the project.

Decorate

Many times, it is useful to know the branch or tag that each commit is associated with. The --decorate tag lets you git log show all references to the commit (such as branches, tags, and so on).

This can be used with another configuration item. For example, execution will git log --oneline --decorate format the commit history as follows:

0e25143 (HEAD, master) Merge branch ‘feature‘ad8621a (feature) Fix a bug in the feature16b36c6 Add a new feature23ad9ad (tag: v0.9) Add the initial code base

In this example, you can see that the topmost commit has been checkout, and that it is the end of the master branch (through the head tag). The second commit has another feature branch pointing to it, and the last commit with a v0.9 tag.

Branches, tags, head, and commit history are all the information contained in your Git repository. Therefore, this command allows you to see the structure of the project more fully.

Diff

git logA number of options are available to show the difference between two commits. Two of the most commonly used are --stat and -p .

--statoption displays the number of additions and deletions per commit (note: Modifying a row to add a row and deleting a row) is useful when you want to see the changes introduced by the commit. For example, the following submission adds 67 lines to the hello.py file, deleting 38 rows.

commit f2a238924e89ca1d4947662928218a06d39068c3Author: John <[email protected]>Date:   Fri Jun 25 17:30:28 2014 -0500    Add a new feature hello.py | 105 ++++++++++++++++++++++++----------------- 1 file changed, 67 insertion(+), 38 deletions(-)

The number of + and-after the file name is the relative ratio of additions and deletions in the change caused by this commit. It gives you an intuitive feeling about how many changes have been made to this submission. If you want to know the absolute number of deletions per commit, you can pass in the -p option git log . This will be output if all the deletions are submitted:

commit 16b36c697eb2d24302f89aa22d9170dfe609855bAuthor: Mary <[email protected]>Date:   Fri Jun 25 17:31:57 2014 -0500    Fix a bug in the featurediff --git a/hello.py b/hello.pyindex 18ca709..c673b40 100644--- a/hello.py+++ b/hello.py@@ -13,14 +13,14 @@ B-print("Hello, World!")+print("Hello, Git!")

For a lot of changes in the submission, this output will become long and large. In general, when you export all the edits, you want to find a specific change, then you need to use the pickaxe option.

Shortlog

git shortlogis special git log , it is designed to create a release statement. It categorizes each submission by author and displays the first line of the submission information. This makes it easy to see who did what.

For example, two developers contributed 5 commits to a project, so git shortlog the output would be:

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

By default, the git shortlog output is sorted by author name, but you can pass in the -n option to sort by each author's submitted quantity.

Graph

--graphOption to draw an ASCII image to show the branching structure of the commit history. It is often --oneline used with two options, which makes it easier --decorate to see which commits belong to which branch:

git log --graph --oneline --decorateFor a simple repository with just 2 branches, this will produce the following:*   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 indicates the branch in which this commit is committed, so the meaning is 23ad9ad and 16b36c6 the two commits on the topic branch, the rest on the master branch.

While this is a good choice for simple projects, you might prefer to GITK or sourcetree these more powerful visualization tools to analyze large projects.

Custom formats

For other git log formatting needs, you can use the --pretty=format:"<string>" options. It allows you to output commits using placeholders like printf.

For example, the following commands, and the three placeholders, are replaced with the %cn author's %h %cd first name, the thumbnail ID, and the submission date, respectively.

git log --pretty=format:"%cn committed %h on %cd"This results in the following format for each commit:John committed 400e4b7 on Fri Jun 24 12:30:04 2014 -0500John committed 89ab2cf on Thu Jun 23 17:09:42 2014 -0500Mary committed 180e223 on Wed Jun 22 17:21:19 2014 -0500John committed f12ca28 on Wed Jun 22 13:50:31 2014 -0500

A complete placeholder list can be found in the document.

This --pretty=format:"<string>" option is especially useful when you want to use the log content in another command, except to let you see only the information that you are interested in.

Filter Submission History

Formatting the commit output is just one git log of the uses. The other half is to understand how to navigate through the commit history. The next article describes how to git log select specific commits in the project history. All usages can be combined with the formatting options discussed above.

By quantity

git logThe most basic filtering option is to limit the number of submissions that are displayed. When you are only interested in the last few commits, it can save you a page of time to view.

You can add the -<n> options later. For example, the following command will show the latest 3 commits:

git log -3
By date

If you want to see a commit during a specific time period, you can use --after or --before tag to filter by date. They all accept several date formats as parameters. For example, the following command shows the submission after July 1, 2014 (inclusive):

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

You can also pass in a relative date, such as a week ago (" 1 week ago ") or Yesterday (" yesterday "):

get log --after="yesterday"

You can also provide --before and --after to retrieve submissions between two dates. For example, to show the submission from July 1, 2014 to July 4, 2014, you can write:

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

Note --since , --until mark and --after , and --before tag are equivalent respectively.

by author

You can use tags when you only want to see the submission of a particular author --author . It accepts regular expressions and returns all authors whose names satisfy the submission of this rule. If you know the exact name of the author, you can just pass in the text string:

git log --author="John"

It will show all submissions by the author called John. The author name is not necessarily a full match, as long as the string containing that substring matches.

You can also use regular expressions to create more complex searches. For example, the following command retrieves the submission of the author named Mary or John.

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

Note that the author's email address is also counted as the author's name, so you can also use this option to search by mailbox.

If your workflow distinguishes between the submitter and the author, it --committer can be used in the same way.

By submitting information

By submitting information to filter submissions, you can use --grep tags. It --author is similar to the one above, except that it searches for submission information rather than the author.

For example, your team specification requires that the relevant issue number be included in the submission, and you can use the following command to display all commits related to this issue:

git log --grep="JRA-224:"

You can also pass in -i parameters to ignore case matching.

by file

Many times, you are only interested in changes to a particular file. In order to display the history of a particular file, you only need to pass in the file path. For example, the following command returns all foo.py and bar.py file-related commits:

git log -- foo.py bar.py

--Tells git log the next parameter is the file path, not the branch name. If the branch name and file name cannot be conflicting, you can omit it -- .

by content

We can also search for commits based on the addition and deletion of a line in the source code. This is known as Pickaxe, which accepts -S"<string>" parameters like the shape. For example, when you want to know when a Hello, World! string is added to a file in a project, you can use the following command:

git log -S "Hello, World!"

If you want to search using regular expressions instead of strings, you can use -G"<regex>" tags.

This is a very powerful debugging tool that lets you navigate to all commits that affect a particular line in your code. It even lets you see when a line is copied or moved to another file.

By range

You can pass in scopes to filter commits. This range is specified in the following format, where <since> and <until> is the commit reference:

git log <since>..<until>

This command is especially useful when you use a branch reference as a parameter. This is the simplest way to show the difference between two branches. Take a look at the following command:

git log master..feature

One of the master: The feature scope contains all commits in the feature branch and not in the Master branch. In other words, this command shows what has changed since the Master Branch Fork to the feature branch. It can be visualized like this:

Note If you change the front and back order of the range (feature: Master), you get all commits to the Master branch, not the feature branch. If git log you output all two branches of the submission, this means that your commit history has been forked.

Filtering Merge Submissions

git logBy default, merge commits are included in the output. However, if your team adopts a force-merging strategy (meaning that you modify the upstream branch of the merge instead of rebase your branch to the upstream branch), there will be a lot of foreign submissions in your project history.

You can --no-merges exclude these submissions by tagging:

git log --no-merges

On the other hand, if you are interested only in merge submissions, you can use --merges tags:

git log --merges

It returns all commits that contain two parent nodes.

Summarize

You should now be familiar with using git log to format the output and choosing the usage of the submission you want to display. It allows you to view any content you need in your project history.

These tips are an important part of your GIT toolkit, but note that they are git log often used with other git commands. When you find the submission you want, you pass it on git checkout , git revert or other tools that control the submission of history. So, go ahead and stick to Git's advanced usage learning.

Git Log Advanced Usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.