Search code examples
gitgit-diffgit-log

GIT - Get commit messages and diff from last pull


I have a project that contains GIT submodules. I'd like to gather the changes in that submodule into a text file and send that text file to a Jenkins email template.

Right now, I have this command, which gets me that changed files in that submodule:

git diff origin/master --stat >> fullLog.txt

And the generated file looks like this

 test.cs             | 8 +++++++-
 test1.cs            | 3 ++-
 2 files changed, 9 insertions(+), 2 deletions(-)

Ideally I would like the commit messages that we're just pulled but looking at the git-log documentation I would have to know which commit to limit from. Right now when I do git log -p I get every message since the start of the project.

My desired end result would look something like this:

Commit a2a4cd6a72892d7a6f0f6e2097d8bf13826a56c3 by jsmith
test-111: clears cache

Commit c58a86c8ebc7153d65c0bfdf0d8e5f92be571b9f by psmith
test-112: Changed setting

 test.cs             | 8 +++++++-
 test1.cs            | 3 ++-
 2 files changed, 9 insertions(+), 2 deletions(-)

Solution

  • If you know you just pulled AND the pull actually had new commits, then you can make use of the reflog to find out what the state of your HEAD was right before the pull, and use that as an argument to git log. The state of HEAD before the last update to HEAD is HEAD@{1}, so this might work:

    git log HEAD@{1}.. --stat
    

    You can run git reflog to see the reflog itself. Note that the reflog is not updated if a git pull did not pull in any new commits for the checked-out branch.