This is not something grep does. The name "grep" itself is an acronym for "globally search a regular expression and print", which is what the ed command g/re/p does (for a given regular expression re).
ed is an interactive line editor from 1969, but it's most likely installed on your system today nonetheless (it's a standard POSIX tool). We got grep from ed, and it can be seen as a shortcut or alias for a specific functionality of ed, and sed, which is "stream-ed", i.e. a (non-interactive) stream editor.
Let's use sed instead:
$ sed -n '1p' file.txt
The 1p string is a tiny sed "script" that prints (p) the line corresponding to the given address (1, the first line).
The editing command 1p would (no surprise) do the same thing in the ed editor by the way.
The -n suppresses the output of anything not explicitly printed by the script, so all we get is the first line of the file file.txt.
Alternatively:
$ sed '1q' file.txt
This prints all lines of the file, but quits (q) at line 1 (after printing it). This is exactly equivalent to head -n 1 file.txt.
In the POSIX standard (in the rationale section for the head command) it says (generalizingparaphrasing) that head -n N is much the same as sed 'Nq', i.e. "print every line, but quit at line N". The reason head was included in the standard at all was due to symmetry with tail (andand backwards compatibility with existing Unix implementations).