Home > Blockchain >  How to find what folders changed from last git pull?
How to find what folders changed from last git pull?

Time:10-19

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?

CodePudding user response:

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.

CodePudding user response:

If the automated system is plugged into a known hosting solution (e.g : a CI job for github, gitlab, azure devops ...), you should be able to get the information of "what was the previous commit for that branch ?" from the data in the trigger.

Check the documentation for your hosting service.


A simple solution using git data only :

  • write down the current commit on the branch about to be pulled
  • pull the changes
  • check the differences between the new commit and the previous commit

assuming you work on master :

# current commit on your branch:
old=$(git rev-parse HEAD)

# pull the changes:
git pull origin master

# check the different files between the two commits:
git diff --name-only $old HEAD
git diff --name-status $old HEAD

From your question, it looked that you were interested only in the differences on the toplevel directories, you may have a result that more directly describes that using git diff-tree :

git diff-tree --name-only $old HEAD
  • Related