Most software development teams (not just in game development) solve this issue using version control software. Examples are
All these tools have some differences, but the basic workflow is usually like this: There is one central repository for the project with the complete codebase. When a developer wants to join the project, they perform a "checkout". The version control software copies the codebase to their local machine. The software remembers the current version ("revision") of the codebase. When a developer made their changes and wants to put them onto the main repository, they perform a "commit". Their changes are uploaded to the central repository, and a new revision number is created.
When another developer now wants to commit their changes but the revision they once checked out is no longer the most recent one, the version control system won't let them. The developer first needs to "pull" the revisions which happened in the meantime. This updates their local copy to the most recent version on the central repository. When there are conflicts (intermediate revisions made changes to a file they also changed), the software might ask them to resolve the conflict by editing the conflicting files manually (a "merge") in case it doesn't manage to automatically do that. After they have done that, they can commit their changes as a new revision.