I was in the Navy.
If you serve your term and get an honorable discharge before transitioning, your benefits are safe. You'll have to be sure to keep them updated on your name, and there's going to be a process to go through for changing your gender in their records, but there are no rules against transitioning after you get out, and you keep everything you earned.
I would caution you not to join after becoming aware that you're trans unless you have very strong reasons to believe that you are stable and able to function as female in a rigid, stressful environment where you're constantly reminded of your gender in ways that are very likely to trigger dysphoria (close living quarters with women, segregated from your male coworkers, being called a "female" on a regular basis, fairly rampant sexism, and being held to different physical standards and standards of appearance). If you have a gender crisis while in the military, you *must not* seek help or confess your feelings to *anyone* connected with the military, including but not limited to military psychologists, chaplains, and lawyers. If you do, it will be reported, and you will be discharged and probably lose your benefits.