Issue
I have a repository with several folders and many subfolders and files inside:
/folder1/....
/folder2/....
/folder3/....
And whenever I change one (or more), an automated system should git pull the changes and recompile the code. Each folder has its own compilation script and it takes a long time to compile each of them. Is there a way to know, for the changes I'm about to pull, what were the modified folders, so I can run only those compilation scripts that are needed and not all of them?
Solution
Fetch first:
git fetch
OPTION 1
So, let’s say you want to check if your current branch has changes in the folder when compared to origin:
$ git diff --quiet HEAD origin/<branch> -- <path/to/dir> || echo changed
For example:
$ git diff --quiet HEAD origin/master -- folder1 || echo changed
If there are any changes in folder1 since the most recent fetch, you will see the word "changed" in the console:
changed
OPTION 2
Let's see what has changed:
git diff <branch>...origin/<branch> <path/to/dir>
For example:
git diff master...origin/master folder1
If there were changes in the specified path, Git will output those changes. Then you can merge.
git merge origin/<branch>
After that, you can compile the project (if necessary).
In addition
You can also find out if there have been changes in the last n commits in a given folder. To do this, you can run the following command:
git log --name-status -n <path/to/dir>
For example:
git log --name-status -2 folder1
This will allow you to see the changes for the last 2 commits in the folder1 folder.
Answered By - Ivan Yuzafatau Answer Checked By - Senaida (WPSolving Volunteer)