muntakim360
muntakim360

Reputation: 97

Get number of changed files in Git between two branches from bash

I'd like to know how can I get a number of changed files between two branches in a bash script i.e.

git diff v1.1.123..master --stat

and this gives a list of files and detailed changed/added/deletion, but I'd need the number of changed files in a bash variable.

OR, anyway to find out if there has been any change between the branches in a bash variable, so that I can do some operations if there's any change or not.

Upvotes: 5

Views: 5652

Answers (3)

thierno
thierno

Reputation: 934

if A and B are different branches, type

git diff A..B --name-status

Upvotes: 3

torek
torek

Reputation: 489628

(Answer copied and expanded from comment, though this is now largely a duplicate of several "name or name-status only" questions as well.)

You are already most of the way there. Simply replace --stat with --name-status and then write code to count based on the statuses. (Note also that git diff X..Y means precisely the same thing as git diff X Y: the usual special meaning of .., two dots, goes away in git diff.)

--name-only would work as well if you don't care to aggregate stats for Add, Delete, Modify, and potentially Copy, Rename, Type-change (e.g., from regular file to symlink), etc. Whether your diff will detect renames depends on (a) file similarity and (b) options and/or your setting for diff.renameLimit. Copy detection is similarly under the control of options. Note that you can also use --diff-filter to select for (or, since git 1.8.5, exclude1) particular changes.

To just count files, piping the diff output through wc -l will suffice (though note that its output has leading white space).


1For instance, git diff A B --name-status --diff-filter=AD will produce just a list of files Added or Deleted. To exclude Type-change items, git diff A B --name-status --diff-filter=t does the trick in git 1.8.5 or newer; with older versions, you would have to list everything but T, e.g., --diff-filter=ABCDMRUX, depending on whether you asked for pairing breaks. I'll also note that Unmerged and X(unknown) should never occur here; U status should only occur when invoking a diff against the index.

Upvotes: 7

CodeWizard
CodeWizard

Reputation: 142532

Execute this command and execute a count on the files

 git diff --name-only SHA-1 SHA-2

If you just want to count files:

git-whatchanged - Show logs with difference each commit introduces

 # replace the -1 with the desired number of commits you need
 # the output will be a number for the modified files,
 git whatchanged -1 --format=oneline | wc -l

To get a full list of files (names and more, simply remove the wc )

git whatchanged -1 --format=oneline

Upvotes: 1

Related Questions