maxisme
maxisme

Reputation: 4245

How to identify if a recent pull had changes inside a directory

I currently have a git project with the structure:

z.txt
foo/a.txt
foo/b.txt

using bash how can I identify after running $ git pull that either a.txt and/or b.txt (i.e anything under the foo directory) have been altered?

Upvotes: 1

Views: 51

Answers (2)

tmaj
tmaj

Reputation: 34947

A. If you already pulled

git diff

You can use git diff and specifically:

  • git diff commit1..commit2 --name-only; or
  • git diff commit1..commit2 --name-status

The following descriptions are from the doco.

--name-only

Show only names of changed files.

--name-status

Show only names and status of changed files. See the description of the --diff-filter option on what the status letters mean.


git pull tells you the commit ids it merges/fast-forwards:

/mnt/c/git/repo666 (develop)>git pull
Updating f86907f7a..a708dcfe8

In this case the command would be:

git diff f86907f7a..a708dcfe8 --name-status

git log

To see differences per commit you could use git log with --name-only or --name-status.

B. Before a pull

If you haven't pulled and you want a peek at the potential changes you can git fetch the branch (not pull) and compare the local copy of the remote branch your current branch.

/mnt/c/git/repo666(develop)>git fetch // not git pull
(...)
/mnt/c/git/repo666(develop)>git status
On branch develop
Your branch is behind 'origin/develop' by 3 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working tree clean
/mnt/c/git/Platform (develop)>git diff develop origin/develop --name-status

(Please note I used git diff branch origin/branch and not git diff origin/branch so that is shown in the desired order i.e. if the file was added in origin/develop we want to see it as added not deleted.)


Note on git pull output

Please note that the output of git pull contains added and renamed files twice

Fast-forward
 ...
 src/Folder1/Services/File1.cs                                      |   30 +
 src/Folder1/Services/File2.cs                                      |    7 +
 ...
 src/Folder1/ViewModels/XViewModel.cs                               |    8 +-
 ...
 src/{Abc.Common/Services => Abc/Contracts/Area1}/Area1File1.cs     |    7 +-
 ...
 89 files changed, 7254 insertions(+), 4897 deletions(-)
 create mode 100644 src/Folder1/Services/File1.cs
 create mode 100644 src/Folder1/Services/File2.cs
 ...
 rename src/{Abc.Common/Services => Abc/Contracts/Area1}/Area1File1.cs (83%)
 ...

Upvotes: 2

jeremysprofile
jeremysprofile

Reputation: 11425

We can get closer than what @tymtam suggests, if you're willing to do things before you run git pull.

Get the SHA for your current head and save it:

sha="$(git rev-parse HEAD)"

Next, run the pull:

git pull

Finally, compare foo on your new HEAD to what you had before:

git diff --stat $sha -- foo

Note, if you have uncommitted changes, those will be included in what git diff reports, so you can always run git stash before your diff command to hide them, and then git stash pop to get them back.

Upvotes: 0

Related Questions