How can I attach a gallery to a post with a short code?

The gallery is the shortcode. It is not, so far as I know, saved in any other place or in any other way than in that shortcode.

You can print/process shortcodes anywhere you want with tricks like this:

echo do_shortcode('');

Or this:

$gallery_shortcode="
';
print apply_filters( 'the_content', $gallery_shortcode );

Both from the Codex.

You can see in the source how that gallery is constructed. It is actually parsed from the values passed in the shortcode.

If you need to save that shortcode and retrieve it some other way you will need to construct your own mechanism for doing so.