If you're playing Bach's music, it shows you that its' key is E minor, or A major or anything. But, when you play something like:
He add sharp in his notes. First it's G major, and then down to E minor, and then straight to B major(?) then back to A minor and to G major again.

How can he composed that, I mean, is there any technique or it's the style of Baroque music?

2 Answers
2

"First it's G major, and then down to E minor, and then straight to B major(?) then back to A minor and to G major again."

No.

As Tim pointed out, this piece is firmly in E minor. The key signature is the first hint - one sharp indicates either G major or E minor. The fact that the piece starts with an Eminor chord is the second clue. (The upbeat starts with 1st inversion E minor, and then on the weak beat we pass through to start firmly on a root position E minor. In both cases the fifth is omitted from the chord, but esp. in 2 voice counterpoint this makes sense, as it is the weakest chord tone, and has the least impact on the identity of the chord.)

Now that we know it is in E minor, we should not be surprised to see a raised 6th and 7th degrees, esp. in ascending melodic lines. These altered degrees are very conventional in diatonic minor mode, this is known as "melodic minor scale".

In m2 (3 if you count the upbeat) we also see that the raised degrees are back to natural in a descending line. This too is part of the entire convention surrounding melodic minor mode. Also the raised 7th degree in measure 5 is completely expected - the raised 7th degree is approached in a descending line from the tonic e, but immediately returns back to the tonic again. This is also expected - a raised seventh like this has the effect of making it a "leading tone", which "wants" to go back to the tonic, thus again reinforcing e as the tonic. The first chord in m6 is again E minor - exactly as expected.

The only place where the center of tonality briefly changes is in measure 8 where we briefly feel a tonality of G major (the relative major of Eminor) This is achieved by the chords of m7. First beat of m7 is G, 2nd beat is C, and least 2 beats are D - so in retrospect this sounds as a I-IV-V progression in the key of G major, which then cadences into G in m8.

It is only a brief escape: since G is only the relative major of E minor, and since the harmony moves real quick, and doesn't overly stress the D chord it is easy to immediately roll back to Eminor (by easy I mean it doesn't offend the ear to continue in Eminor as if this entire cadence didn't happen). If you know this entire piece you'll note that it does - at least, the 1st time around, the 2nd time around the piece moves on in G major for at least a while.

There is no particular Baroqueness with regard to the treatment of the tonality - this kind of harmony was also common in Renaissance, and also in the classical period. What is rather Baroque, or at least, typical for Bach is the lively counterpoint, and systematic persistence of the motive (the duh-duh DUH motive that is characteristic for the Bourree).

Other popular changes in the center of tonality can be to the dominant or subdominant - keys that have all but one tone in common with the key of departure. Whether such a change in tonality persists and can be called a modulation depends on how strong the confirmation of the key is (a strong I IV V7 cadence is a strong confirmation and more likely to establish a new key), and also depends on how long the piece continues in the new key.

Note that a (temporary) change in tonality may or may not be denoted explicitly using a new key signature. In practice you'll find that in Baroque music, the tendency is not to denote those changes explicitly, esp. not within one movement.

Another prominent feature of this piece which is common in Baroque counterpoint is the contrary motion. This is actually a great example of this; the two voices are always managing to move in opposite directions except at the cadences at bars 4 and 8.
–
BobRodesApr 28 '14 at 16:57

BobRodes, the persistent contrary motion is certainly a prominent characteristic of this piece. Thanks for pointing it out!
–
Roland BoumanApr 28 '14 at 17:45

+1 for a great explanation and I'd give you a second +1 for pointing out that it's not only the Baroque in which you find that. That said, while it's not exclusively Baroque, the questioner is correct in surmising that it's exquisitely idiomatic of the Baroque (as well as other periods), and yes, immersing yourself in Baroque music is a good way to get the hang of it.
–
CodeswitcherApr 29 '14 at 3:34

When the key sig. is one sharp, the key will be Gmaj., or its relative minor Em.Here, he's using mainly Emin. with the melodic preferred, as in notes 6 and 7 raised for an upward direction of tune, and left natural when the tune descends. It was the usual way in the Baroque period.

If one is in G/Em, then one will use D, C, Am and B (or Bm) for the harmony. It's not that the piece modulates to each of these keys. It could stay in Emin., or move freely to Gmaj. as here, using the D as dominant to get to the Gmaj.