"If ... I'll eat my hat" is a phrase that is often spoken by someone who is incredulous or skeptical, and does not believe that something in question is true or will proceed as suggested. In other words, if I say something like: "If he succeeds in doing that... I'll eat my hat", the implication is that I very much doubt that 'he' will be able to do 'it'. Moreso than just being skeptical about it, in making this type of statement I am, in a sense, making a wager that 'he' will fail. I am betting that if 'he' actually does succeed, then I am willing to destroy something of value to myself.
There are numerous proposals in regard to where the phrase originated, and any of them might be correct. I personally have always preferred the one that I am about to offer in this post.
In German-speaking regions, a cone of sugar was / is called a Zuckerhut ~ a Sugar Hat. The hut, translated as hat, or cone of sugar was a common way for either raw or refined sugar to be shaped and packaged before being wrapped in paper for sale and storage. The homeowner would unwrap a cone or hat when needed, and using a small wooden, round-headed hammer (called, appropriately enough, a zuckerhammer, or 'sugar hammer'), the cone would be smashed into lumps and smaller grains. The larger lumps might be set aside for use in cups of tea (such as in: "Do you want one lump or two?").
Sugar was an expensive commodity during the Colonial period ~ or as a Colonial housewife might say: it was very dear. Despite its costliness, it was quite edible. That is why it was a thing that might be put up for wager. If the speaker of the phrase won the bet (and the thing being wagered against failed), then the extravagance of the sugar would not be wasted and lost. The speaker's integrity would remain intact. On the other hand, if the speaker of the phrase lost the bet (meaning the thing being wagered against actually succeeded), then the speaker would be obliged to consume his costly, though replaceable, article. The greater consequence being the embarrassment experienced by the one who had to eat his hat.
The phrase, as it passed through time and away from the original German influence, came to be understood to be that the speaker of it was vowing to eat a piece of apparel ~ a cloth, leather or felt headgear. Although perhaps not originally intended, this later interpretation made the sense of the wager all the more sincere. The idea of someone having to actually chew on a piece of clothing meant that the speaker was very confident that the other person would fail.