| by Arround The Web | No comments

Advanced Git Log: Filtering and Custom Formatting

The “git log” is the most popular command in Git Bash that is considered for checking commit histories in the projects. It is beneficial when users want to see who has contributed to the projects, find bugs in the projects, and resolve them. Interestingly, “git log” offers various options for filtering and custom formatting to display required logs only rather than the whole history.

The write-up will discuss:

How to Filter and Custom Format Git Log in Git?

Various flags/options can be used for filtering and custom formatting of displaying the Git log history. We have implemented several examples below to demonstrate them practically. Let’s dive into it!

Example 1: Display Commits in Custom Formatting

For displaying the commits in custom formatting with name, hash, and data, you can use the “–pretty=format” flag with the command. Here, “%cn” is the commit name, “%h” is the commit hash, and “%cd” is the commit date:

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

 

The above output displays the commit history with the name “added”, and hash “on” along with the commit date.

Example 2: Display Commits Using –decorate Flag

The “–decorate” flag categorizes the Git log history with all references like Git branches and tags pointing to each commit. For this purpose, execute the following command:

git log --decorate

 

Example 3: Display Commits Using -p Flag

The “-p” flag is used to display the difference between the previous and new commit changes. In simple words, it displays the actual changes. For that purpose, see the below-provided command:

git log -p

 

As you can see the difference between previous and new commits has been listed.

Example 4: Display Commits Using “shortlog”

The “shortlog” is an option that gives a quick look at the log commits message along with the author’s name. To obtain this type of result, run the below-given command:

git shortlog

 

Only commit messages with the author’s name have been listed.

Example 5: Display Commits Using –graph Flag

To display the commit history in an ASCII graph, use the “–graph” flag. The graph shows the branch structure of the commit’s history, making it easier to understand for users which commit belongs to the particular branch. Let’s execute the provided command for better understanding:

git log --graph

 

Example 6: Display Commits Using –oneline Flag

The “–oneline” flag displays each Git log in one line. To get its results, run the below-mentioned command:

git log --oneline

 

The Git log has been listed in one line.

Example 7: Display Amount of Most Recent Commits

Likewise, if the users want to display the most recent logs, they can simply use the hyphen “” with the number of logs. For instance, the provided command displays the most recent logs:

git log -3

 

Example 8: Display Commits By Date

In order to display the commits from a particular date to onward, use the “–after=” tag and specify the desired date:

git log --after="2023-23-8"

 

Only the log history after the date “2023-23-8” has been listed.

Example 9: Display Commits By Author Name

For displaying the applied commits by the particular author, use the “–author=” and give the author’s name. The below command will display the commits having author name “Mateen”:

git log --author="Mateen"

 

Example 10: Display Commits By Message

The user can also match the commits message with the support of the “–grep” flag. Suppose the message you want to search is “file added”. For that purpose, see the below command:

git log --grep="file added"

 

The commits that match with the given message “file added” are listed.

Conclusion

The “git log” is the command that is used for displaying the commit histories that happened in the projects. To filter and custom format these log histories, multiple flags/options are available for the “git log” command. By following this guide, you can check Git log filtering and custom formatting options implemented with practical examples.

Share Button

Source: linuxhint.com

Leave a Reply