10

I use the following Git command

git diff-tree --no-commit-id --name-only -r <SHA>

to get a list of changed files.

Unfortunately, the list doesn't specify the type of change for each file: added, modified or deleted ... etc

How may I display a list of changes [type of change, file name] in a given SHA of a specific commit.

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
  • If you want to get the file changes between any two arbitrary commits instead of a specific commit, then see [Show all changed files between two Git commits](http://stackoverflow.com/q/1552340/456814). *(Sorry for the re-comment, the previous one had too much bold and made it look awful, so I got rid of it)* –  Oct 13 '15 at 15:19
  • A developer used this very command in a gitlab pre-receive hook to compare the code on our gitlab server (the commit passed in) with the code being pushed/received. Just want to confirm we're doing the right thing. Seems to work. – Gobi Dasu Nov 25 '16 at 08:39

5 Answers5

21

Use --name-status instead of --name-only

git diff-tree --no-commit-id --name-status -r <SHA>

This will show the filename with a status letter of (extracted from man): 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).

Joe
  • 25,000
  • 3
  • 22
  • 44
8

While Joe's answer points out that you can use the --name-status flag with git diff-tree, you can also use the same flag with git diff instead.

To get the changed files with their status for just a specific commit, you can just use the sha id of that commit with a commit-parent specifier, like so

git diff --name-status <sha>^ <sha>

The revision specifier <sha>^ means the first parent of the commit <sha>, so using that with git diff effectively gives you all the changes that were made between a commit and its parent.

Alternative

You can also use

git diff --name-status <sha>~ <sha>

where <sha>~ also means the first parent of commit <sha>.

Documentation

Community
  • 1
  • 1
  • That also works great, unfortunately cannot mark multiple answers as correct answers. marked up and it's a correct solution, Thanks. – Ashraf Bashir Jul 23 '14 at 11:28
4

Use

git whatchanged 

to see the last commit

stdcall
  • 27,613
  • 18
  • 81
  • 125
1
git checkout <commit>
git whatchanged -1
Martin G
  • 17,357
  • 9
  • 82
  • 98
1

Thanks to hvd's comment on stdcall's answer,

Your original answer, which included the git whatchanged SHA-1 form, was almost right: add the -1 option to get only that specific commit.

here is the solution for those who are interested:

git whatchanged <SHA> -1

Another solution is:

git diff-tree --no-commit-id -r <SHA>
Community
  • 1
  • 1
Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82