I too have been having this problem, which is still not fixed in Mendeley. Based on que que's great suggestion, I solved my issue using emacs, but in batch mode, nicely allowing me to wrap the solution in a shell script.
First off, I created the following emacs lisp script (let's call it bibsort.el):
( defun bibsort()
( bibtex-sort-buffer )
( save-buffer )
)
This sorts any bibtex file by key name and then saves it. You can call the script in batch mode from the command line. I went further in writing a shell script to call it for each bibtex file which Mendeley had reordered (with or without fresh references), by checking the return code from "git diff filename" (this saves time by not running the lisp code unnecessarily on files which have not changed).
Here's the shell script (let's imaginitively call it bibsort.sh), which assumes that both the bibtex files and the lisp script are in the current directory:
#!/bin/bash
bibliobasher() {
IFS=$( echo -en "\n" )
BIBFILES=*.bib
for bibfile in $BIBFILES
do
git diff --quiet "$bibfile" 2> /dev/null
if [ $? = 1 ]; then
emacs --batch -l bibsort.el "$bibfile" -f bibsort
fi
done
}
bibliobasher
Just run it (after setting file permissions if necessary) before each git commit:
chmod a+x bibsort.sh
./bibsort.sh
git commit ...
It won't touch the extra non-bibtex lines added by Mendeley, and should work on any Unix/Linux installation with emacs. It also works on the GIT console in Windows (which I'm using), but you would have to install the MINGW build of emacs separately.
For me, git commits now work elegantly and economically. Hope this helps ...