Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git log - How to filter (exclude) files from appearing in `git log`? (git pathspec magic)

I need to use the git-log command to create a report of the number of added and removed lines by commit, on average between two dates.

Currently the command I use is:

git log --since="2015-12-01" --until="2015-12-31" --shortstat

But I need to filter some files in the process. Thoses files are autogenerated, and we don't want to see their influence. They are easily recognizes by their name *.generated.*

I choose to use the git-log command, I am able to get the report I need, except I don't see how to filter those unwanted files.

The doc is big, I already read it several times, but I don't see anything about filtering files based on their names. Is this possible, or do I have to find another command to find the number of lines added/deleted by commit?

like image 690
Cyril Gandon Avatar asked Mar 03 '16 13:03

Cyril Gandon


People also ask

How do I remove files from git log?

Note that by using the “git rm” command, the file will also be deleted from the filesystem. Also, you will have to commit your changes, “git rm” does not remove the file from the Git index unless you commit it.

What is pathspec in git?

The pathspec is the mechanism that git uses for limiting the scope of a git command to a subset of the repository. If you have used much git, you have likely used a pathspec whether you know it or not. For example, in the command git add README.md , the pathspec is README.md .

How do you exclude a file from a commit?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.

How does git log follow work?

With git log --follow , Git runs an after-diff step (called from diff_tree_sha1 ; see footnotes) that trims everything down to one file. The diff is done with R=C and L=P. The general case is actually easier to describe, though, so we'll start with that.


1 Answers

The "secret" on how to do it is called:


pathspec magic


You Can simply use this format (introduced in git version >1.9):

# Use this syntax, pay attention to all the parameters and the syntax

# Unix: 
git log <any required flags> -p  -- . ':(exclude)*.generated.*'

# Windows (double quote) [ Thank to @Cyril Gandon for noticing it]:
# (double quote) should work on all OS as well
git log <any required flags> -p  -- . ":(exclude)*.generated.*"

What is this weird syntax?

This syntax is called pathspec magic.
Using this syntax you can "tell" git which file extensions to exclude. In your case it's the *.generated.*


From the doc:

http://git-scm.com/docs/gitglossary.html:

A pathspec that begins with a colon : has special meaning.

In the short form, the leading colon : is followed by zero or more magic signature letters (which optionally is terminated by another colon :), and the remainder is the pattern to match against the path.

The magic signature consists of ASCII symbols that are neither alphanumeric, glob, regex special characters nor colon. The optional colon that terminates the magic signature can be omitted if the pattern begins with a character that does not belong to "magic signature" symbol set and is not a colon.

In the long form, the leading colon : is followed by a open parenthesis (, a comma-separated list of zero or more magic words, and a close parentheses ), and the remainder is the pattern to match against the path.


Note

In older versions (the feature was introduced in git v1.9 and the bug was fixed in git 1.9.5) there was a bug which was fixed.

https://github.com/git/git/commit/ed22b4173bd8d6dbce6236480bd30a63dd54834e


Demo:

git log --stat

(check the last commit)
enter image description here

And the same runt with the filer - you can see that there is only one file in the results instead of 2

git log --stat -p -- . ':(exclude)*dal.js*'

enter image description here

like image 176
CodeWizard Avatar answered Oct 17 '22 05:10

CodeWizard