when using git on macOS, duplicate files appeared after switching branches.
move your git repo to somewhere not tracked by iCloud
iCloud watches registered directories. When git sweeps files to change branches etc, iCloud will also detect a file change, and try to its own synching routine. Underlying process depends on its synching routine/algorithm.
Don't use together a file synching service (iCloud, Dropbox, Google Drive eetc.) and a version control (git, svc etc.). Will run into duplicates. if you use repository hosting service like github, gitlab, Bitbucket), you alreaday get avilability and security you are getting from iCloud.
https://stackoverflow.com/questions/59308049/icloud-drive-desktop-sync-vs-git-deleted-files-reappear-and-duplicates-with-n
https://stackoverflow.com/questions/62385234/git-keeps-duplicating-files-and-appending-2-at-end-of-filename
Apple devices are very easy to use, as it turns out. To be honest, I’m one of those people who used Android for a long time and was skeptical about more expensive Apple devices. However, now I have a Mac, and it’s really convenient. If any difficulties suddenly arise, I usually look for information on SetApp, for example, https://setapp.com/how-to/select-multiple-files-on-mac . Such articles will be especially useful for beginners like me who have just switched to using MacOS.