mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Check If A File Has Changed In A Script as a Git TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1671 TILs and counting..._
|
||||
_1672 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -321,6 +321,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Caching Credentials](git/caching-credentials.md)
|
||||
- [Change The Start Point Of A Branch](git/change-the-start-point-of-a-branch.md)
|
||||
- [Check How A File Is Being Ignored](git/check-how-a-file-is-being-ignored.md)
|
||||
- [Check If A File Has Changed In A Script](git/check-if-a-file-has-changed-in-a-script.md)
|
||||
- [Checking Commit Ancestry](git/checking-commit-ancestry.md)
|
||||
- [Checkout Old Version Of A File](git/checkout-old-version-of-a-file.md)
|
||||
- [Checkout Previous Branch](git/checkout-previous-branch.md)
|
||||
|
||||
38
git/check-if-a-file-has-changed-in-a-script.md
Normal file
38
git/check-if-a-file-has-changed-in-a-script.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# Check If A File Has Changed In A Script
|
||||
|
||||
If I'm at the command line and I want to check if a file has changed, I can run
|
||||
`git diff` and see what has changed. If I want to be more specific, I can run
|
||||
`git diff README.md` to see if there are changes to that specific file.
|
||||
|
||||
If I'm trying to do this check in a script though, I want the command to clearly
|
||||
tell the script _Yes_ or _No_. Usually a script looks for an exit code to
|
||||
determine what path to take. But as long as `git diff` runs successfully,
|
||||
regardless of whether or not their are changes, it is going to have an
|
||||
affirmative exit code of `0`.
|
||||
|
||||
This is why `git diff` offers the `--exit-code` flag.
|
||||
|
||||
> Make the program exit with codes similar to diff(1). That is, it exits with 1
|
||||
> if there were differences and 0 means no differences.
|
||||
|
||||
With that in mind, we can wire up a script with `git diff` that takes different
|
||||
paths depending on whether or not there are changes.
|
||||
|
||||
```bash
|
||||
if ! git diff --exit-code README.md; then
|
||||
echo "README.md has changes"
|
||||
else
|
||||
echo "README.md is clean"
|
||||
fi
|
||||
```
|
||||
|
||||
We can take this a step further and instead use the `--quiet` flag.
|
||||
|
||||
> Disable all output of the program. Implies --exit-code. Disables execution of
|
||||
> external diff helpers whose exit code is not trusted
|
||||
|
||||
This exhibits the same behavior as `--exit-code` and goes the additional step of
|
||||
silencing diff output and disabling execution of external diff helpers like
|
||||
`delta`.
|
||||
|
||||
See `man git-diff` for more details.
|
||||
Reference in New Issue
Block a user