Search code examples
gitversion-controlgit-log

How to get all commit messages which are affected in last push in git?


I am just trying to create a git hook which sends a notification about all the commits to our ms-teams channel on every "post-recieve".

The git hook itself works, but with the command git log -1 --pretty=format:'%s'
I only get the last commit-message, which is not very useful if you push multiple commits to master.

Is there a way to get the data (message, author, date of commit, commit number, usw.) of all affected commits on every push?

I have looked up a lot of questions here on Stack Overflow, but haven't found a proper solution for my problem.


Solution

  • Typically speaking, your origin/master will be the position of master on the remote origin after your last synchronisation. master will be your current branch. The commits between these will be the ones you're interested in and you can get them using git log origin/master..origin and you can do whatever you want with these.

    Now, in your case, you need to run this on the server after a receive. I'm not sure that post-receive will have this information (since the references will already have been updated). However pre-receive hook receives a list of all the commits that are being pushed. Perhaps hooking up this to your notification function would help better. The answer linked to from Lasse V. Karlsen's comment has details on how to do this.