Use the rule, as it often works, but you will need to learn the exceptions.
The rule of soft and hard "g" works quite well for words derived from French or Italian (cage, page, giant, large, image are French; giraffe and gel are Italian)
It works less well for words from Old English or Germanic roots (girl, get, give are from Anglo Saxon; gift and gear are from Norse; geek is from Low German)
Of course, it is impossible to know if a word is from a Romance or Germanic root without checking it in a dictionary. So this doesn't help in practice. And there are always exceptions to the exceptions: "Gene" is from (modern) German (but ultimately from Latin), and Tiger comes from French (where it is spelled Tigre, showing the hard g before r).
Now, for a native speaker or advanced learner, if you come across a word that you do not know, it is more likely to be from Latin or French than from Germanic (since most of the Old English Germanic words part of the core vocabulary). So it is a good idea to guess that for an unknown word, the "g" is soft if followed by an i,e or y. And if you get it wrong, someone will surely correct you.