5

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?

3 Answers 3

11

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
1
  • I highlighted the phrase "ONLY file-name with path". Oct 13, 2015 at 6:04
4

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 two commit hashes, you can also use git diff and --name-only instead, as the other answer mentions:

git diff hash1..hash2 --name-only
8
  • i did what you showed above in simpler terms like "git log --pretty=format: --name-status" . Yes This gives only filenames but it adds the modification status A,M,D etc.. at the starting of every line before file name. This is why i mentioned ONLY filename with path!! Any workaround..?? Oct 12, 2015 at 12:24
  • 1
    @flute Got it! You only need some sed magic at the end (see the edit): ... | sed -e 's/^\w\t*\ *//' Oct 12, 2015 at 12:33
  • Are you a magician by any chance? you 'sed'uced git log to do this!! it works! Oct 12, 2015 at 12:39
  • @flute Yeah, Node.js & UNIX black magician. :D UNIX pipes are great, aren't they? Glad I helped! Oct 12, 2015 at 12:52
  • I have a Python, to work with! These magics doesn't work with it. cmd = ["git", "log --name-status | grep -E '^[A-Z]\b' | sort | uniq | sed -e 's/^\w\t*\ *//'"] subprocess.call(cmd). This in python script give error - saying "command invalid or something". May be because am using windows machine. Oct 12, 2015 at 13:07
2

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.