When changing branches in git, how files are replaced?

Solution 1:

When you run git branch xxx, Git only creates the new branch, but doesn't actually switch to it – the working tree is not modified at all. (Note that the new branch will have exactly the same files and history as the old one.)

When you run git checkout xxx to switch branches, files from the new branch are extracted from the "object database", which is the archive kept under .git/objects/ that contains compressed originals of every file in every commit. (That's also where commits themselves are stored.)

Files belonging to the old branch but missing from the new one are simply deleted (because they already exist in the object store, so Git can just re-extract them if you switch back).

When some files are identical between branches, git checkout doesn't touch them at all (no deletion/re-extraction). This way your uncommitted changes can remain across checkouts.