Home > OS >  How to detect if a certain file has been changed on the current git branch
How to detect if a certain file has been changed on the current git branch

Time:12-20

I'm trying to detect if a certain file has been changed on the current feature branch of my repo from within my CI.

My current approach is to use git --no-pager diff @~..@ -- PATH_TO_FILE, but this apparently only outputs changes for the last commit, not if the change occurred in a previous commit on this branch.

Is it possible to specify such a range?

CodePudding user response:

You haven't named any specific service, but I will assume that you are using github or gitlab or Azure devops or ... and that you work with Pull Requestst (or Merge Requests), and that the events that trigger your CI are based on this.


The PR that triggered your CI job indicates that the <feature> branch should be merged to a specific <master branch> (can be master, main, develop, ci ...).

You can see the diff of your feature branch using the 3 dots notation for git diff :

git diff <master branch>...<feature>

this will show you the changes that landed on feature since it forked from <master branch>.
You can add any other regular git diff options:

git diff --name-only <master branch>...<feature>
git diff --name-status  <master branch>...<feature>
git diff <master branch>...<feature> -- path/to/file
# etc ...

Check the doc of your CI tool to see how you can get the target branch of your PR.

  • Related