The phrase “to eat one’s hat” means: To eat crow; to eat humble pie; to assert one’s readiness to consume such an unsavory mess if a certain event should not turn out as one predicts.
The present form of the saying is first found in Charles Dickens’ Pickwick Papers (1837): “‘If I knew as little of life as that, I’d eat my hat and swallow the buckle whole.'”
Dickens could have coined the phrase, but it is more likely that it was merely his own adaptation of the older, “I’ll eat old Rowley’s hat,” of the same general significance.
Here “old Rowley” referred to Charles II, a nickname given to him, it is said, from his favorite race horse, but cherished by his adherents from the long struggle against Oliver Cromwell, through punning connection with the familiar saying, “a Rowley (Roland) for an Oliver.”