I get asked this a lot. Luckily, Git makes it pretty easy to get this information, but sometimes you need to string together multiple commands to do this gracefully.
Here’s the meat and potatoes:
git diff --name-only REVISION1..REVISION2 desired/sub/directory
What if you don’t have the revision/commit but instead a branch name? And what if that branch name has a / in it? Well, you’ll find that this command fails. It throws a generic ambiguous argument error. How would you get around this? Easy! Find the revision/commit of that branch. How? Check out the branch, make sure it’s the latest revision, and determine the revision/commit ID.
How this can be accomplished in Bash:
git checkout BRANCH/NAME && git pull origin BRANCH/NAME REV_ID=$(git rev-parse HEAD)
But who added the file and what is the associated work item? STOP MOVING THE GOAL POST! Still, that’s fairly easy, too. Just pull the last commit to each file that’s returned in the diff command. Below is a simple Bash script that will generate this information:
BRANCH_A=release/9.2.0.0 BRANCH_B=release/9.3.0.0 REPO_DIR=/path/to/cloned/repo CHECK_DIR=sub/path/to/desired/dir cd $REPO_DIR git fetch git checkout $BRANCH_A && git pull origin $BRANCH_A COMMIT_A=$(git rev-parse HEAD) git checkout $BRANCH_B && git pull origin $BRANCH_B COMMIT_B=$(git rev-parse HEAD) FILE_LIST=$(git diff --name-only $COMMIT_A..$COMMIT_B $CHECK_DIR) echo "FROM - $BRANCH_A" echo "TO - $BRANCH_B" for FILENAME in $FILE_LIST; do echo $FILENAME git log -n 1 --pretty=short -- $FILENAME done