How to get ONLY filename with path using git log?

2020-04-07 05:28发布

问题:

I used almost all git log commands yet i haven't found a best way to do this. I need only this - get only file name with path nothing else

/path/filename.txt
/path/anotherfile.ext
...
...

My input is date FROM and TO to the git log command. Either git log gives developer-name or date or commit-number or something that i don't want with file names. How to get ONLY the file name with full path?

回答1:

Use the --since and --until options to select the time range and then you can use UNIX pipes to grep, sort and collect the uniqe paths:

git log --name-status --since='..' --until='..' | grep -E '^[A-Z]\b' | sort | uniq | sed -e 's/^\w\t*\ *//'

Example:

git log --name-status --since='1 January 2015' --until='2 January 2015' | grep -E '^[A-Z]\b' | sort | uniq | sed -e 's/^\w\t*\ *//'

For more detailed git log outputs see How to have git log show filenames like svn log -v


If you have to commit hashes, you can also use git diff and --name-only instead, as the other answer mentions:

git diff hash1..hash2 --name-only


回答2:

Use --name-only and remove the message with an empty format

git log --name-only --format=""

Just use all other git log options as usual. E.g.

git log --name-only --format="" -n 2 master

Optionally sort and remove dupplicates

git log --name-only --format="" | sort | uniq


回答3:

This does not use git log, but if you're willing to use commit objects (hashes, branch references or other commitish identifiers), git diff makes it simple. Example for getting the changed files for the last three commits.

$ git diff HEAD~3 --name-only
> some/path/with/file.php
> another/path/and/file.html
> yet/another.json

You can replace HEAD~3 with either a single commitish so you're comparing to the current HEAD (the commit you're "on"), or then define a commit range with <commitish>..<commitish>:

$ git diff HEAD..abcd1234 --name-only
> file/name/here.cpp

$ git diff 1234abcd..abcd1234 --name-only
> some/file/name-here.txt

If you need to filter the files according to the modification type (e.g. added, modified, deleted...), you can use the --diff-filter option. man git diff:

--diff-filter=[(A|C|D|M|R|T|U|X|B)...[*]]
       Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (i.e. regular file, symlink, submodule, ...) changed (T), are
       Unmerged (U), are Unknown (X), or have had their pairing Broken (B). Any combination of the filter characters (including none) can be used. When * (All-or-none) is added
       to the combination, all paths are selected if there is any file that matches other criteria in the comparison; if there is no file that matches other criteria, nothing
       is selected.

If you need to use dates for filtering, then this may not be the best option. Perhaps use git log --since=... to get the first hash for the date and pass it to git diff?