I am so confused

I have been liking this guy for a while now, we always talk and joke around. my friends would even make joke about it in front of him. i thought he had liked me too but i recently found out that he has a girlfriend. i immediately backed alway and stopped talking to him. i’m so confused because i swear he was always flirting and he seemed to like me back. should i ask him about it or just stay away?