(1) I've been looking a little more. My belief is now:

**Conjecture.** *Let $G$ be a generalized Baumslag-Solitar group (i.e. the Bass-Serre fundamental group of a nonempty finite graph of groups in which every vertex and edge group is infinite cyclic). Let $\langle x\rangle$ be one vertex group. Then we have one of the following:*

*
*(a) for some $n\ge 1$, $\langle x^n\rangle$ is a normal subgroup [this can be read on the graph, see below]. Then $G$ is virtually direct product of $\mathbf{Z}$ and a free group (and hence linear).

(b) $G$ is a solvable, Baumslag-Solitar group, and in particular is residually finite [assuming the graph is reduced, this means that there is a single vertex, at most a single self-loop with one inclusion being surjective]

*(c) Otherwise, $G$ is not residually finite.*

In (a) this can be read as follows: the partial isomorphism defined by any loop (including self-loops) is $\pm$ a partial identity. See also (4) below.

Reduced means that for every non-self loop, both inclusions are proper. If the (finite) graph of groups is not reduced, one can collapse non-reduced edges until one gets a reduced graph, without changing the Bass-Serre fundamental group.

(2) Now let me prove (sketch) the conjecture when the graph is a tree (with self-loops allowed).

Let me start with a graph with a single vertex (and finitely many self-loops). Such a group is thus defined by a family of pairs $(n_i,m_i)_{i\in I}$ of nonzero integers with $I$ finite.

Let us specify (a),(b),(c) to this case:

*(a) for some $n\ge 1$, $\langle x^n\rangle$ is a normal subgroup: this means that $|m_i|=|n_i|$ for all $i$;*

*
*(b) $G$ is a solvable, Baumslag-Solitar group: this means that $|I|=1$ (say $I=\{1\}$) and $\min(|m_1|,|n_1|)=1$;

*(c) other cases.*

Since the case $|I|\le 1$ is already settled, we deal with $|I|\ge 2$. Assume $G$ is residually finite. By the case $|I|=1$, we know that for each $i$, either $|n_i|=|m_i|$ or $\min(|n_i|,|m_i|)=1$. We can assume that whenever $\min(|n_i|,|m_i|)=1$, we have $1\in\{n_i,m_i\}$.

Just to illustrate, a typical example would then be $|I|=3$, with the pairs $(1,3),(-2,1),(4,-4)$. The corresponding presentation is
$$\langle t_1,t_2,t_3,x\mid t_1xt_1^{-1}=x^3,t_2x^{-2}t_2^{-1}=x,t_3x^4t_3^{-1}=x^{-4}\rangle.$$

Suppose that $(1,m)$ and $(n,1)$ both occur among the $(n_i,m_i)$, for some $|n|,|m|\ge 2$. So we have the subgroup with presentation
$$\langle t,u,x\mid txt^{-1}=x^m,u^{-1}xu=x^n\rangle$$

In every finite quotient of this group, $g\mapsto tgt^{-1}$ defines an injective endomorphism of $\langle x\rangle$, hence bijective. Similarly for $u$. Hence $\langle x\rangle$ is normal in every finite quotient. Hence $[t^{-1}xt,u^{-1}xu]$ is trivial in every finite quotient. But it is not trivial in the group itself, by standard facts on HNN extensions.

A similar argument holds if $(1,n_i)$, $(1,n_j)$ both occur among the pairs with $i\neq j$ and $|n_i|,|n_j|\ge 2$: then $[t_i^{-1}xt_i,t_j^{-1}xt_j]$ is not trivial but vanishes in every finite quotient. (Same with $(n_i,1)$ and $(n_j,1)$.)

Next, if $(1,n)$ and $(m,m)$ both occur with $|n|\ge 2$: this corresponds to relators $txt^{-1}=x^n$, $[u,x^m]=1$: then $[u,t^{-1}x^mt]$ is nontrivial but vanishes in every proper quotient. Finally if $(1,n)$ and $(m,-m)$ both occur with $|n|,|m|\ge 2$, then $[u,t^{-1}x^{2m}t]$ is nontrivial but vanishes in every proper quotient (to check that it is nontrivial one has to check separately the case $|n|=2$).

If all these are excluded and $|I|\ge 2$, this means that we are in case (a).

(3) Here is the simplest case with a non-self loop: two vertices joined with two edges. This corresponds to a presentation
$$\langle t,x,y\mid x^k=y^\ell,x^m=ty^nt^{-1}\rangle.$$

We get $x^{kn}=y^{\ell n}=t^{-1}x^{\ell m}t$. Indeed the discussion is:

If $|kn|=|\ell m|$, then $\langle x^{kn}\rangle$ is normal cyclic and we are in case (a). Otherwise, an argument similar to the Baumslag-Solitar case (and to the ones above) implies that the group is not residually finite.

(4) In general, let me clarify "this can be read on the graph" in (a): given a sequence of oriented edges

$e_1,\dots,e_p$ forming a loop (

$p\ge 1$). Let the left and right inclusion of

$e_j$ be given by multiplication by

$n_j$,

$m_j$ respectively. Say that the loop is unimodular if

$\prod m_j=\prod n_j$. Say the GBS graph of groups is unimodular if every loop is unimodular. Then (a) means that the graph is unimodular. (It is enough to check loops in a generating subset of the (ordinary, i.e; Poincaré) fundamental group of the graph.)

~~I haven't checked, but believe it's easy~~, [Edit: one easily checks] that if the graph is unimodular then $G$ is virtually direct product of $\mathbf{Z}$ and a free group. Indeed, after modding out by a normal cyclic subgroup contained in every edge group, the resulting group is Bass-Serre fundamental group of a finite graph of finite (cyclic) groups, and hence is virtually free.

And if the graph is not unimodular (and reduced with at least 2 edges) then one has to check the failure of residual finiteness. I did it above if the lack of unimodularity is witnessed by a self loop, and more quickly with it is witnessed by a loop of size 2. But I think there is no serious difficulty in general.

Eventually I believe the conjecture follows using routine arguments. Thus the linear examples are "obviously linear" (virtually $\mathbf{Z}\times$free or solvable BS groups) and the other ones are non-residually finite for a reason similar to the Baumslag-Solitar case, while the solvable examples appear as an exception.

4more comments