Do you have a clean way to list all the files that ever existed in specified branch?
Asked
Active
Viewed 2.9k times
4 Answers
171
This is a simplified variation of Strager's solution:
git log --pretty=format: --name-status | cut -f2- | sort -u
Edit: Thanks to Jakub for teaching me a bit more in the comments, this version has a shorter pipeline and gives git more opportunity to get things right.
git log --pretty=format: --name-only --diff-filter=A | sort -u
-
9@Dustlin: Add --diff-filter=A option (list only added files). Current version (without sed filtering only added files) would fail if you have enabled rename detection and have renames in history. I think you can then use --name-only instead of --name-status and remove 'cut -f2-' from pipeline. – Jakub Narębski Feb 13 '09 at 12:39
-
In one of my repos, I get quite a few duplicate lines (including a number of blank lines at the beginning of the output) with the second command that aren't dupes with the first. – Slipp D. Thompson May 08 '12 at 16:19
-
If you need a bit more info than the file name: $ git log --pretty=format:"%h %an [%cd]: %s" --name-only | cut -f2- | sort -u | grep Filename.ext – Nitay Apr 06 '14 at 11:13
-
4*Note: `--all` is something you will need if you have more than a single orphaned tip. Eg, multiple separate histories in one repo.* – ThorSummoner Apr 27 '15 at 05:50
-
4`--diff-filter=A` ignores files that were created by copying an already existing file, so adding it may not _always_ be what you want. – cmbuckley May 31 '18 at 14:21
-
@Dustin "**Ever** exist", does it include the files that exist in a commit\version several months ago and they **may be deleted** at current commit\version? – John Mar 10 '22 at 03:22
17
This does the right thing for checking if a filename was ever present in the repo not just on the current branch.
git log --all --pretty=format: --name-only --diff-filter=A | sort - | grep fubar

dch
- 1,502
- 9
- 9
4
Here is two useful alias: FindFile ff
and FindFilewithCopies ffc
:
# Find if one file ever had into repository
ff = "!git log --pretty=format: --name-status --all -M -B | sort -u | grep $1 #"
# The same as above but showing copied files
ffc = "!git log --pretty=format: --name-status --all -C -M -B | sort -u | grep $1 #"
You get information about file names and operations with them.
Sample use:
$ git ff create
A database/migrations/2014_10_12_000000_create_users_table.php
A database/migrations/2014_10_12_100000_create_password_resets_table.php
A database/migrations/2015_05_11_200932_create_boletin_table.php
A database/migrations/2015_05_15_133500_create_usuarios_table.php
D database/migrations/2015_05_12_000000_create_users_table.php
M database/migrations/2015_05_11_200932_create_boletin_table.php
R051 database/migrations/2014_10_12_000000_create_users_table.php database/migrations/2015_05_12_000000_create_users_table.php
$ git ffc create
A database/migrations/2014_10_12_000000_create_users_table.php
A database/migrations/2014_10_12_100000_create_password_resets_table.php
A database/migrations/2015_05_11_200932_create_boletin_table.php
A database/migrations/2015_05_15_133500_create_usuarios_table.php
C052 database/migrations/2014_10_12_000000_create_users_table.php database/migrations/2015_05_11_210246_create_boletin_nosend_table.php
D database/migrations/2015_05_12_000000_create_users_table.php
M database/migrations/2015_05_11_200932_create_boletin_table.php
R051 database/migrations/2014_10_12_000000_create_users_table.php database/migrations/2015_05_12_000000_create_users_table.php

Benjamin Loison
- 3,782
- 4
- 16
- 33

Juan Antonio Tubío
- 1,172
- 14
- 28
3
You can run git-log --name-status
, which echoes something like:
commit afdbbaf52ab24ef7ce1daaf75f3aaf18c4d2fee0
Author: Your Name <your@email.com>
Date: Tue Aug 12 13:28:34 2008 -0700
Added test file.
A test
Then extract files added:
git-log --name-status | sed -ne 's/^A[^u]//p' | sort -u

strager
- 88,763
- 26
- 134
- 176