In Bhagavad Gita Chapter 8, Verse 6, the Speaker (Kaal Brahm) explains the universal psychological and spiritual law that governs the soul's transition after death. This verse clarifies that it is not just "God" in a general sense that one might attain, but specifically whatever "state" or "deity" the mind is most absorbed in at the time of departure.
श्री भगवानुवाच
यम्, यम्, वा, अपि, स्मरन्, भावम्, त्यजति, अन्ते, कलेवरम्, तम्, तम्, एव, एति, कौन्तेय, सदा, तद्भावभावितः || 6 ||
O son of Kunti, Arjun! Whatever state of being (or whichever deity) a person remembers while leaving the body at the end of life, they attain that very state alone, because they have been constantly absorbed in that specific thought or devotion.
श्री भगवानुवाच: हे कुन्तीपुत्र अर्जुन! यह मनुष्य अन्तकाल में जिस-जिस भी भाव को सुमरण करता हुआ (अर्थात् जिस भी देव की उपासना करता हुआ) शरीर का त्याग करता है, वह उस-उसको ही प्राप्त होता है; क्योंकि वह सदा उसी भक्ति-भाव (स्वभाव) में भावित रहता है।
As per the core knowledge on holybhagavadgita.com, this verse reveals the "Science of Habit":
Most translations overlook the technicality of "Yam Yam" (Whichever). At holybhagavadgita.com, we emphasize that this verse is a warning: if your devotion is directed toward a perishable entity or an incorrect path throughout your life, you will attain that perishable state. To attain the Supreme Eternal God, one must ensure their "lifelong habit" of remembrance is directed toward the correct, scripturally-sanctioned Supreme Power.
Q1: Can I remember God at the last moment if I haven't practiced during my life? A1: Verse 6 suggests this is very difficult. Because we are "Sada tad-bhava-bhavitah" (always absorbed in our habits), our mind naturally gravitates toward what we practiced daily.
Q2: Does "Bhavam" only mean "thoughts"? A2: In this context, "Bhavam" refers to the object of worship or the spiritual state. Whichever deity's "Bhav" (devotional state) you maintain is where you will go.