Given $A\subset X$ closed in $X$ and $G\subset A$ closed in $A$ is $G$ closed in $X$?

I'm trying to prove a lemma that says:

Given $X = A \cup B $ and two continuous functions $f : A \to Y,g : B \to Y$ such that $f|_{A\cap B}=g|_{A\cap B}$ we consider $h : X \to Y$ given by $$ h(x)=\begin{cases}f(x) \quad &\text{if} \, x \in A \\ g(x) \quad &\text{if} \, x \in B\end{cases}$$

Then if $A$ and $B$ are closed then $h$ is continuous.

The way I've approached this has been:

Given $F\subset Y$ closed, we have that $$\begin{aligned}h^{-1}(F)&=\{x \in X : f(x) \in F\text{ if } x \in A \text{ or } g(x) \in F \text{ if } x \in B\} \\&= (A \cap f^{-1}(F))\cup (B\cap g^{-1}(F))\, ,\end{aligned}$$ where as $f$ and $g$ are continuous then $f^{-1}(F)$ is closed in $(A, \tau _A)$ and $g^{-1}(F)$ is closed in $(B, \tau _B)$. Therefore, $(A \cap f^{-1}(F))$ is closed in $(A, \tau _A)$ and $(B \cap g^{-1}(F))$ is closed in $(B, \tau _B)$ for being intersections of closed sets.

Now, all there is left to prove is that given $A$ a closed subset in $X$ and $G$ a closed set in $A,$ then $G$ is closed in $X$ which is the part I'm struggling with.

I haven't been able to get that last part, so I'm considering I might have made a mistake on my previous reasoning, but I fail to find any mistakes.


$G$ closed in $A$ means there is some closed set $F\subseteq X$ such that $G=A\cap F$. If $A$ is closed in $X$ then $G$ is simply an intersection of two closed subsets of $X$, thus closed.