In the battle between the Jews and the Muslims at the end of time, the aggressors will be the Jews
Question

                It is narrated in the hadith that the Prophet (blessings and peace of Allah be upon him) said: “The Hour will not begin until you fight the Jews, until a Jew will hide behind a rock or a tree, and the rock or tree will say: ‘O Muslim, O slave of Allah, here is a Jew behind me; come and kill him – except the gharqad (a kind of thorny tree).’ At the same time – as far as I know – the Muslims throughout history have treated the Jews well and coexisted with them, for Islam is a religion of love and peace. So why does the hadith tell us here that we have to kill them, and why did the Muslims not do that previously?
Praise be to Allah.Firstly: 
Islam is the religion of mercy and peace, as the questioner describes it. Therefore, Islam allowed the Jews and Christians to remain in Muslim lands, and did not expel them or kill them, as they did and still do when they gain control over Muslim lands. That is in accordance with the treaty between them and the Islamic state, with both parties having rights and duties. So long as they continue to adhere to those conditions, they have the right to enjoy that right of residence and security. 
Hence Islam offers protection to ahl adh-dhimmah in Muslim lands, and defends their rights if they adhere to the conditions of residence, to the extent that the Prophet (blessings and peace of Allah be upon him) said: “Whoever kills a mu‘ahid (non-Muslim living under Muslim rule) will not smell the fragrance of Paradise, even though its fragrance may be detected from a distance of forty years.” Narrated by al-Bukhari (3166). 
And he (blessings and peace of Allah be upon him) said: “Whoever wrongs a mu‘ahid, detracts from his rights, burdens him with more work than he is able to do or takes something from him without his consent, I will plead for him (the mu‘ahid) – or I will be the opponent of (the Muslim who wronged him) – on the Day of Resurrection.”
Narrated by Abu Dawood (3052); classed as hasan by Ibn Hajar in Muwafaqat al-Khabr (2/184); classed as saheeh by al-Albani in Saheeh Abi Dawood. 
But if they break the conditions – such as if they commit acts of betrayal or treachery, or they prepare to fight us or help our enemies, and the like – then they have broken the treaty, and in that case there is no treaty between us and them, and they do not deserve to reside in security as they did not uphold the treaty.
Secondly: 
Yes, the Muslims used to coexist with the Jews and treat them well, so long as they adhere to the conditions of residence. But they may choose to be treacherous. It is sufficient for you to know that when the Messenger (blessings and peace of Allah be upon him) migrated to Madinah, there were three Jewish tribes in the city, namely Banu Qaynuqa‘, Banu an-Nadeer and Banu Qurayzah. He made treaties with all of them and agreed to live in peace with all of them, but they all committed acts of treachery and betrayal. 
Therefore the Messenger (blessings and peace of Allah be upon him) fought them, killing some of them and banishing others beyond Madinah, and he did not allow them to remain in the city. 
Thirdly: 
Al-Bukhari (3593) and Muslim (2921) narrated from the hadith of Ibn ‘Umar (may Allah be pleased with him) that he said: I heard the Messenger of Allah (blessings and peace of Allah be upon him) say: “The Jews will fight you and you will prevail over them, then a rock will say: ‘O Muslim, here is a Jew behind me; kill him.’” 
In Saheeh Muslim (2922), it is narrated from the hadith of Abu Hurayrah that the Messenger of Allah (blessings and peace of Allah be upon him) said: “The Hour will not begin until the Muslims fight the Jews and the Muslims will kill them, until a Jew hides behind a rock or a tree, and the rock or tree will say: O Muslim, O slave of Allah, there is a Jew behind me, come and kill him. Except the gharqad (a thorny tree), for it is one of the trees of the Jews.” 
In some reports there is an indication that the fighting against the Jews that is mentioned in this hadith will occur at the end of time, when the Dajjal emerges and the Messiah ‘Eesa ibn Maryam (peace be upon him) descends and fights him. 
‘Abdullah ibn ‘Umar (may Allah be pleased with him) narrated that the Messenger of Allah (blessings and peace of Allah be upon him) said: “The Dajjal will camp in this salty, barren ground in Marr Qanat [a valley in Madinah], and most of those who go out to him will be women, to the extent that a man will go back to his relative, to his mother, his daughter, his sister and his paternal aunt, and will tie her up for fear that she may go out to him. Then Allah will give the Muslims power over him, and they will kill him and his party, until a Jew will hide beneath a tree or a rock, and the rock or tree will say to the Muslim: ‘Here is a Jew beneath me; kill him.’” 
Narrated by Imam Ahmad in al-Musnad (9/255). But there is some weakness in its isnad because of the ‘an‘anah of Muhammad ibn Is-haq [i.e., narration by saying ‘an (from) rather than saying So and so said/told us].  There are other corroborating reports; see the commentary on al-Musnad in the Mu’sasat ar-Risalah edition. 
These corroborating reports were quoted by al-Hafiz Ibn Hajar (may Allah have mercy on him) in Fath al-Bari (6/610); he classed as hasan the hadith of Samurah ibn Jundub (may Allah be pleased with him), then he said: What is meant by fighting the Jews is that this will happen when the Dajjal emerges and ‘Eesa descends. End quote. 
Ibn al-Mulqin said: What is meant by the words “You will fight the Jews” is that this will happen when ‘Eesa descends; the Muslims will be with him and the Jews will be with the Dajjal.
End quote from at-Tawdeeh li Sharh al-Jami‘ as-Saheeh (17/663). 
This is what was stated definitively by a number of commentators on the hadith. In fact Imam at-Tirmidhi (2236) narrated the hadith of ‘Abdullah ibn ‘Umar, which is quoted above, in the chapter entitled The Signs of the Dajjal (‘Alamaat ad-Dajjal). 
Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) said:
The Jews are in fact awaiting the false messiah (al-maseeh ad-dajjal), for he is the one whom the Jews will follow. Seventy thousand of the Jews of Isfahan wearing tayalisah (a kind of head covering) will emerge with him. The Muslims will fight them with him, to the extent that trees and rocks will say: “O Muslim, here is a Jew behind me; come and kill him.”
End quote from al-Jawab as-Saheeh li man baddala Deen al-Maseeh (2/30). 
Ash-Shaykh Ibn Baz (may Allah have mercy on him) said: 
‘Eesa (blessings and peace of Allah be upon him) will fight him, supported by the Muslims, and he will kill him at the gate of al-Ludd (Lod), which is in Palestine, near al-Quds (Jerusalem). He will kill him with his spear, as it says in the saheeh hadith, and the Muslims who are with him will kill a great number of the Jews. It says in the hadith from the Prophet (blessings and peace of Allah be upon him) that the Muslims will fight the Jews, and will kill them and gain power over them, and trees and rocks will call out: O Muslim, O slave of Allah, here is a Jew; come and kill him. And ‘Eesa will kill the Dajjal and put an end to him. 
End quote from Fatawa Noor ‘ala ad-Darb by Ibn Baz, ed by ash-Shuway‘ir (4/290) 
When the Dajjal emerges, thousands of the Jews will follow him and rally around him, aiming to fight the Muslims. Then the Messiah ‘Eesa ibn Maryam (peace be upon him) will descend and the Muslims will rally around him to fight the Dajjal and his followers. ‘Eesa ibn Maryam will call the Jews to Islam, and he will not accept anything from them at that time except Islam. Some of them will become Muslim and some of them will remain followers of Judaism, then will come the battle that the Prophet (blessings and peace of Allah be upon him) mentioned. 
Thus it is clear that the battle, for the Muslims, will be a just and legitimate battle that Allah, may He be exalted, undoubtedly approves of. This is indicated by the following: 
·        It will be against the Dajjal and his supporters who gathered to fight the Muslims. “They (the disbelievers, the Jews and the Christians) want to extinguish Allah’s Light (with which Muhammad (blessings and peace of Allah be upon him) has been sent - Islamic Monotheism) with their mouths, but Allah will not allow except that His Light should be perfected even though the Kafiroon (disbelievers) hate (it)” [at-Tawbah 9:32].
·        It will be a battle fought by the Muslims under the leadership of ‘Eesa ibn Maryam (peace be upon him), one of the noble Messengers.  
·        Allah, may He be exalted, will honour the Muslims in this battle with this miracle, which is that rocks and trees will speak and call the Muslims to come and kill the Jews who will be hiding behind them. 
All of this indicates that it will be a just battle that Allah approves of, as was the case with all the Islamic battles in which the aim was to make the word of Allah supreme on earth. “Whoever fights so that the word of Allah will be supreme is fighting in the way of Allah.” Narrated by Muslim (1915). 
And Allah knows best.