I am using the following line to collect changed files in my branch
git diff --name-only "$CI_COMMIT_BRANCH"^! | grep ...
But getting the following error
fatal: ambiguous argument 'my/branch^!': unknown revision or path not in the working tree
Why and how to fix?
I don't think GitLab CI checks out the branch, but it instead runs the pipeline in detached HEAD mode. You can try "origin/$CI_COMMIT_BRANCH^!"
, maybe the remote tracking branch is set up and available.
But since the pipeline always checks out the branch, you can simply use HEAD
to reference the current commit.
Depending on what you want to achieve, the solution might be simple or impossible:
main
or master
): git diff --name-only "$CI_DEFAULT_BRANCH" HEAD
git diff --name-only "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"...HEAD
(...
here takes the merge base of the two commits, consequently showing you the changes since your branch was created, but ignoring any changes made on the target branch)