I am curious for both answers since a brief google didn't show any result. But I was wondering how would you handle installing a program that has the same name in two different repositories. Or changing one already installed so it receives updates from a new one.
Specifically what brought this to attention is OBS studio, since it's included in the default Ubuntu repositories, but the obsproject repo has a higher version of the program.
Every repository hold a Release file that clients will look for when preforming apt commands. The Release file holds information about the available dists and components (such as bionic main universe and etc.). It specifies the locations and hash sums of every component. Once getting the relevant dist, the client then searches for a package list, containing every available debian, its location, its full name, version, hash sums, size and more. That is how you can differ packages from one repo to another.
If you would like to request a specific version or a target repo, you'll need to use the -t option while using apt-get. If that version exists in the repo, it will be downloaded. Note: when choosing a specific repo, it first must exist in the source list.
Below a quote from apt-get man page:
In a typical install of Ubuntu, Apt will automatically try to install the highest version it can locate in any repository that it knows about.
If you are into customization, you can tweak that by setting apt priorities, apt-marking, and apt-pinning. But most folks do not need to use these advanced tools.
WARNING: There is no guarantee that a higher version from a non-Ubuntu repository will be compatible with your Ubuntu system. Some software is -- some software isn't. If you new, higher-version software causes problems, backpedal to the version in the Ubuntu repositories.