Before the shell starts processing any data, it needs to make sure all the input and output is squared away.
So in your case using
> foo.txt basically tells the system: "create a (new) file named
foo.txt and stick all the output from this command into that file".
The problem is, as you found out, that that wipes out the previous contents.
>> will append to an existing file.
Here's a solution using
sed, handle with care:
sed -i '2,$d' foo.txt
It will delete lines 2 to "last" in-place in file
foo.txt. Best to try this out on a file you can afford to mess up first :)
This slightly modified version of the command will keep a copy of the original with the
sed -i.bak '2,$d' foo.txt
You can specify any sequence of characters (or a single character) after the
-i command line switch for the name of the "backup" (ie original) file.