Easy way but not necessarily the best: the capo. Learn the tune in G. Put the capo across the second fret and use your 5th string capo to tune to A. Now play the tune exactly is you did in G. Use the very same fingering and the very same chord shapes but think of the capo as the nut and measure from there. You just have to get used to doing this so … do it a lot… every day. But READ ON - - - The above is not really the point…
The above is not the only way to do this. Start this learning process: Take some time to learn a tune starting with the D chord at 2,3,4. What do you have to do? Put in TIME. You should know that the primary chords in the key of D are D (l), G (IV) and A (V), So if you choose “Comin’ Round the Mountain” you will notice that the first phrase comes right out ot the D chord if you use the ring finger now and then on the third or fourth string, fourth fret to get notes as needed. (You should be able to locate the melody for the words,“She’ll be comin’ 'round the mountain when she comes” WITHOUT moving your pointer, middle or pinkie from the D chord formation (using the ring as the “utility infielder” on the third and fourth strings as needed for missing notes). Do it now! (I said, “Do it now!”) The notes for the following “She’ll be” will come out of the that D chord also. Now move up to the barred D chord at fret 7 and, using you ear, you will easily find the notes for “comin’ 'round the mountain when she”. Next comes the word “comes”. Just move to the A chord at 5,6,7 for that note. Now, take that portion of the tune and play it 30 times. You know the notes are basically going to occur in the three chords, so chord through the whole tune strumming and singing so you will know on which words to change chords. NOW, move on from where you were to the next phrase with the correct chords and JUST ROLL them with a forward roll or a forward/reverse roll. You will hear something that “sorta” sounds like the melody. Fool with it a while to find the missing notes - - they will be nearby as they were when you started working on the first phrase. THIS IS THE POINT: Playing like this in D has you playing all “closed” strings. No “open” strings. THAT MEANS that if you started playing in E (fret 4,5,6) without a capo (except for adjusting the 5th string to a B) as you did in D using the same fingering as chord shapes as you used in D, you will be playing in E. It is the same as you work your way up the neck - - always the same fingering and chord shapes. This is why using the I, IV, V designations are so very helpful. You can learn the spacing between the l, IV, and V chords and train yourself to play in ANY key without a capo. All the neat flashy licks you learned up the neck will work too if you find the right starting point for the key you are in.
A simple tune like “Comin’ 'Round the Mountain” is where you should start. You are NOT trying to learn the song. You are using the song to learn a TECHNIQUE.
This was a long lecture. Sorry. But I spent MANY years not knowing this until a new teacher started me playing in D.