How to list all the files in a commit?

2018-12-31 06:50发布

I am looking for a simple git command that provides a nicely formatted list of all files that were part of the commit given by a hash (SHA1), with no extraneous information.

I have tried:

git show a303aa90779efdd2f6b9d90693e2cbbbe4613c1d

Although it lists the files, it also includes unwanted diff information for each.

Is there another git command that will provide just the list I want, so that I can avoid parsing it from the git show output?

标签: git git-show
27条回答
何处买醉
2楼-- · 2018-12-31 07:12

I use this to get list of modified files between two changesets:

git diff --name-status <SHA1> <SHA2> | cut -f2
查看更多
其实,你不懂
3楼-- · 2018-12-31 07:13

OK, there are couple of ways to show all files in a particular commit...

To reduce the info and show only names of the files which committed, you simply can add --name-only or --name-status flag..., these flags just show you the file names which are different from previous commits as you want...

So you can do git diff followed by --name-only, with two commit hashes after <sha0> <sha1>, something like below:

git diff --name-only 5f12f15 kag9f02 

I also create the below image to show all steps to go through in these situation:

git diff --name-only 5f12f15 kag9f02

查看更多
柔情千种
4楼-- · 2018-12-31 07:17

Using standard git diff command (also good for scripting):

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

If you want also the status of the changed files:

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

This works well with merge commits.

查看更多
低头抚发
5楼-- · 2018-12-31 07:19

There is a simple trick to view as a file listing, just add : after the hash.

git show 9d3a52c474:

You can then drill in,

git show 9d3a52c474:someDir/someOtherDir

If you hit a file you'll get the raw version of the file; which sometimes is what you want if you're only looking for a nice reference or key pieces of code (diffs can make everything a mess),

git show 9d3a52c474:someDir/someOtherDir/somefile

Only drawback of this method is that it doesn't easily show a tree of files.

查看更多
有味是清欢
6楼-- · 2018-12-31 07:21

There's also git whatchanged, which is more low level than git log

NAME
       git-whatchanged - Show logs with difference each commit introduces

It outputs the commit summary with a list of files beneath it with their modes and if there added(A), deleted(D) or modified(M);

$ git whatchanged f31a441398fb7834fde24c5b0c2974182a431363

Would give something like:

commit f31a441398fb7834fde24c5b0c2974182a431363
Author: xx <xx@xx.nl>
Date:   Tue Sep 29 17:23:22 2015 +0200

    added fb skd and XLForm

:000000 100644 0000000... 90a20d7... A  Pods/Bolts/Bolts/Common/BFCancellationToken.h
:000000 100644 0000000... b5006d0... A  Pods/Bolts/Bolts/Common/BFCancellationToken.m
:000000 100644 0000000... 3e7b711... A  Pods/Bolts/Bolts/Common/BFCancellationTokenRegistration.h
:000000 100644 0000000... 9c8a7ae... A  Pods/Bolts/Bolts/Common/BFCancellationTokenRegistration.m
:000000 100644 0000000... bd6e7a1... A  Pods/Bolts/Bolts/Common/BFCancellationTokenSource.h
:000000 100644 0000000... 947f725... A  Pods/Bolts/Bolts/Common/BFCancellationTokenSource.m
:000000 100644 0000000... cf7dcdf... A  Pods/Bolts/Bolts/Common/BFDefines.h
:000000 100644 0000000... 02af9ba... A  Pods/Bolts/Bolts/Common/BFExecutor.h
:000000 100644 0000000... 292e27c... A  Pods/Bolts/Bolts/Common/BFExecutor.m
:000000 100644 0000000... 827071d... A  Pods/Bolts/Bolts/Common/BFTask.h
...

I know this answer doesn't really match "with no extraneous information.", but I still think this list is more useful then just the filenames.

查看更多
其实,你不懂
7楼-- · 2018-12-31 07:22

Display the log.

COMMIT can be blank ("") or the sha-1 or the sha-1 shortened.

git log COMMIT -1 --name-only

This will list just the files, very useful for further processing.

git log COMMIT -1 --name-only --pretty=format:"" | grep "[^\s]"
查看更多
登录 后发表回答