Mandragor
Mandragor

Reputation: 5584

Check if a file was changed in during commit in git

Is there any git command which allows to check if a file was changed during last commit?

I'd like to check if a file which contains a version number was updated, otherwise the dev shall receive a warning not to release the software to an integration environment before a the version was bumped up.

Upvotes: 8

Views: 7809

Answers (3)

uasi
uasi

Reputation: 499

git diff --quiet HEAD\^ HEAD -- path/to/file

exits with status code 1 if path/to/file is changed or removed in the last commit. Otherwise status code is 0.

Upvotes: 0

jthill
jthill

Reputation: 60527

[[ `git rev-parse @:path/to/file` = `git rev-parse @~:path/to/file` ]]

or as a function

head-changed-file () {
        set -- $(git rev-parse "@:$1" "@~:$1")
        [[ $1 != $2 ]]
}

and then you can head-changed-file somefile.txt && echo okay it changed

Upvotes: 5

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522596

Try this:

git diff --name-only HEAD~1 HEAD | grep somefile.txt

This command assumes that you want to check whether the file somefile.txt changed in the last commit. If you want to check whether somefile.txt changed between any two commits, then use

git diff --name-only SHA1 SHA2 | grep somefile.txt

where SHA1 and SHA2 are the hashes of the two commits bounding the diff.

Upvotes: 20

Related Questions