The ash knowledge is a reference to the classic Sherlock Holmes from the novels. The punch is for being drunk and obnoxious.
Spark Notes have an article on classic Sherlock Holmes references, and they have a small section that covers the ash discussion perfectly, so I'll quote it in its entirety:
During a particularly memorable scene in [The Sign of Three], Sherlock
declares proudly that he “knows ash!” This references the fact that
Holmes has penned a monograph on the subject of tobacco ash, first
mentioned in A Study in Scarlet. Holmes uses this skill-set to figure
it all out in The Hound of the Baskervilles and The Valley of Fear.
This reference might also refer to the real-life Sherlock Holmes fan
society The Adventuresses of Sherlock Holmes (ASH for short), a scion
of The Baker Street Irregulars, which was founded by female fans of
the great detective, since the original Irregulars didn’t originally
allow ladies!
Whilst I believe the reference to the real-life fan society is a step too far, the reference to the historic novels is certainly what resonated with me. Whilst their article mentions The Hound of the Baskervilles and The Valley of Fear, Holmes also used his knowledge of ash to solve the crime in The Adventure of the Resident Patient
He also says in the Boscombe Valley Mystery
"I have, as you know, devoted some attention to this, and written a
little monograph on the ashes of 140 different varieties of pipe,
cigar, and cigarette tobacco."
So, firstly, he clearly knows his ash!
As to why he is hit, it's due to his drunken, obnoxious behaviour. To quote from the script:
NEXT PUB. Sherlock is plastered. In the smoking area outside the pub,
he is loudly and drunkenly gesticulating and sounding off to a male
customer over the very loud music.
SHERLOCK: I know ash!
(John is
sitting at a nearby table, looking fairly legless himself. He covers
his face with his hand.)
SHERLOCK: Don’t – Tell – Me – I – Don’t!
(***On
each word he pokes the man in the upper chest with one finger, and on
the last word he puts his hand on the man’s shoulder and pushes him.*
Sighing, John looks up as the man swings a punch at Sherlock’s face.
Sherlock sways back – possibly more by luck than judgement – and
avoids it.)**
JOHN (jumping up): Oh ...
(Thrown off-balance by his
swing, the man stumbles forward and almost falls onto a nearby table.
One of his mates helps him up. John grabs Sherlock from behind and
pulls him away while Sherlock flails wildly towards the man.)
JOHN:
All right, enough! That’s ...
(Grunting with the effort and slurring
the rest of his words [possibly saying ‘Come on’], he drags Sherlock a
few feet away, supporting most of his weight, before propping him onto
his feet.)
JOHN: Stand up straight.
(Sherlock turns round towards him.
John points towards the exit to Sherlock’s left.)
JOHN: Yeah.
(Sherlock points back towards the customer.)
SHERLOCK (slurring):
Ashtray. I know ashtray.
It's generally not a good idea, late at night, when drunk in a pub, to start poking and pushing an already quite annoyed person!
Therefore, he got punched.