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?

Murali Uppangala
  • 884
  • 6
  • 22
  • 49

3 Answers3

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
René Link
  • 48,224
  • 13
  • 108
  • 140
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
Sam Mackrill
  • 4,004
  • 8
  • 35
  • 55
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 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..?? – Murali Uppangala Oct 12 '15 at 12:24
  • 1
    @flute Got it! You only need some `sed` magic at the end (see the edit): `... | sed -e 's/^\w\t*\ *//'` – Ionică Bizău Oct 12 '15 at 12:33
  • Are you a magician by any chance? you 'sed'uced git log to do this!! it works! – Murali Uppangala Oct 12 '15 at 12:39
  • @flute Yeah, Node.js & UNIX black magician. :D UNIX pipes are great, aren't they? Glad I helped! – Ionică Bizău Oct 12 '15 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. – Murali Uppangala Oct 12 '15 at 13:07
  • @flute Oh, if it works out of the code, it should work in your code too, but make sure `git` is accessible as executable. – Ionică Bizău Oct 12 '15 at 13:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92054/discussion-between-flute-and-ionic-bizu). – Murali Uppangala Oct 12 '15 at 13:41
  • I think you need to pipe through `sort` and then `uniq` at the end, otherwise you are sorting and filtering while the lines still start with the diff-filter prefix (A, D, M, etc.) which `sed` later removes. – dumbledad Jul 26 '20 at 08:31
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?

ojrask
  • 2,799
  • 1
  • 23
  • 23