John is correct that the control software deals with this but I'll explain a little more.

When your g-code calls for a tool change it use's a M6 command. This then calls a macro, often named M6, thou some software like Linux Cnc can map M6 command to some other macro name, but most just use a macro named M6 or like mach3, for instance, it's M6start along with M6end. It's in these macro files you can put commands to do pretty much whatever you like. However, it's not G-code and depending on which software it will use some programming language like Visual Basic or C++ or Python etc and you will need to know control software specific functions and commands etc.

That said Which software is it.? I'm sure between us we can help you make it do what you want.