Assume that $(X,\mathcal{O}_X)$ and $(Y,\mathcal{O}_Y)$ be two ringed spaces and $U\subset X, V\subset Y$ be two open sets with an isomorphism of sheaves $\varphi: (U,\mathcal{O}|_U)\rightarrow (V,\mathcal{O}|_V)$. Then we can glue these two topological spaces along these open subsets with $Z:=X\sqcup Y/\sim$ where $x\sim y$ iff $x=y$ or $x\in U, y\in V$ and $\varphi(x)=y$.
Now my textbook says that we can define a new sheaf by defining $\mathcal{O}_{Z,p}:=\mathcal{O}_{X,p}$ if $p\in X$ and $\mathcal{O}_{Z,p}:=\mathcal{O}_{Y,p}$ if $p\in Y$ where $\mathcal{O}_{X,p}$ denotes the stalk of $(X,\mathcal{O}_X)$ on $p\in X$.
Showing that this gives a sheaf is left as an exercise. I have a problem with the identity axiom. Namely, I take an open cover $Z=\bigcup_{i\in I} U_i$ and two functions $f,g\in\mathcal{O}(Z)$ with res$_{Z,U_i}(f)=$res$_{Z,U_i}(g)$ on $U_i$ for all $i\in I$. I need to show that $f=g$. Using the identity axiom of $X$ and $Y$, it was easy to show that $f=g$ on $X$ and $Y$. But I have no idea how I can show that $f=g$ on whole $Z$? Thanks.