22

I need to write a script that incrementally keeps track of files and directories added and removed from a git repo.

I have tried to use:

git log -n1 --pretty="format:" --name-only

But that only tells me which files were committed. It does not specify if it was added or removed.

Any ideas?

smarber
  • 4,829
  • 7
  • 37
  • 78
pocketfullofcheese
  • 8,427
  • 9
  • 41
  • 57

2 Answers2

38

The option you're looking for is --name-status. Like --name-only it's actually a git-diff option; git-log accepts those to determine how it'll display patches.

git log -n 1 --pretty=oneline --name-status

Or equivalently (minus the log header):

git diff --name-status HEAD^ HEAD

As isbadawi points out, you can also use git-whatchanged. This is pretty much git-log with a specific diff output:

git whatchanged -n 1

You might like the --name-status version better, though, since it doesn't show all the blob hashes, just the human-readable statuses.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 1
    That does it! Now I just have to write a script to read that line and pick out the A's and D's and I'm done. Thanks. – pocketfullofcheese Apr 02 '10 at 14:56
  • 2
    Example of outputting the specific commit which contains the **addition** of a file named **.gitignore** in the format of _commit message, names and status of changed files_: `git log --name-status --diff-filter=A --follow .gitignore` – Eido95 May 23 '17 at 19:26
7

git whatchanged

Ismail Badawi
  • 36,054
  • 7
  • 85
  • 97
  • Why was this not voted higher? Plain English, easy to remember, does the trick. – elPastor Mar 21 '19 at 14:30
  • @elPastor It wasn't voted higher because it's intended to be deprecated and replaced by `git log`. It is still useful now, but might not be for future readers. – John Pavek Jul 10 '19 at 15:47
  • Good to know, thanks. Although `git log` doesn't include the files added, at least not in its plain form. – elPastor Jul 10 '19 at 15:49