Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-commit hooks getting partial committed file list

I know running git-diff and other tools can identify the changes in the repository, but when I do a partial commit, I need to pass only the committed files (rather than the entire set of files that are touched) to the pre-commit hook.

For example, I modified three files and commit only two, and I want the pre-commit hook to know the name of the two files.

git status --porcelain

M A.c 
M B.c
M C.h

then I execute:

git commit -m "two files only" A.c B.c

How can the pre-commit hook get hold of the list of the two files only?

like image 684
Rex Xia Avatar asked Oct 15 '25 08:10

Rex Xia


1 Answers

The pre-commit is run at the beginning of the commit process... @Vince's link is the one to look at. Once the hook is running, your script can use git diff --cached --name-only to get the names of the staged files within the pre-commit script. The command works even if the user had staged more for commit, but only requested to commit a subset of the files on the command line.

like image 77
John Szakmeister Avatar answered Oct 17 '25 01:10

John Szakmeister



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!