Get a list of changed files and their status for a specific Git commit
Asked Answered
W

5

12

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.

Weakminded answered 17/7, 2014 at 12:29 Comment(2)
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. (Sorry for the re-comment, the previous one had too much bold and made it look awful, so I got rid of it)Alben
A developer used this very command in a gitlab pre-receive hook to compare the code on our gitlab server (the <SHA> commit passed in) with the code being pushed/received. Just want to confirm we're doing the right thing. Seems to work.Vaginal
S
24

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).

Shy answered 18/7, 2014 at 7:36 Comment(0)
A
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

Alben answered 22/7, 2014 at 4:32 Comment(1)
That also works great, unfortunately cannot mark multiple answers as correct answers. marked up and it's a correct solution, Thanks.Weakminded
S
4

Use

git whatchanged 

to see the last commit

Statesman answered 17/7, 2014 at 12:31 Comment(4)
This outputs all changes for all commits, not for the passed commit's SHA only.Weakminded
The problem which I'm trying to solve is for a specific commit.Weakminded
Your original answer, which included the git whatchanged SHA-1 form, was almost right: add the -1 option to get only that specific commit.Wimsatt
This is what I was looking for. But a quick man-page check shows that a new equal command is git log --raw.Biddie
W
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>
Weakminded answered 17/7, 2014 at 13:14 Comment(0)
M
0
git checkout <commit>
git whatchanged -1
Micropathology answered 17/7, 2014 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.