Xcode project shows two git repositories (only one is correct); commits go to both
Asked Answered
M

3

5

I'm new to git. So, I'm confused and might not be using the correct terminology.

I have two separate Xcode projects, each with it's own local repository. Although the projects have similar names, each project has separate and distinct source files. I've done a couple of commits to each, but when I commit in one project, the commit shows up in both. My new project shows both projects in the git navigator (which might be related to the problem). When I made the new commit, the old project was not open in Xcode.

Each project has it's own .git folder.

Details:

Old Project 1 ("Scheduler"): Shows one repository, but the last commit I did in the new project appears there.

XCode UI showing Scheduler project

New Project 2 ("Scheduler6"): Shows two repositories, and the last commit I did there shows up in both.

XCode UI showing Scheduler and Scheduler6 projects

Why does Project 2 show both repositories, and is that the reason my commit ended up in both? How to correct that? This has happened before, and I did "git reset HEAD^" in the old repository to remove the incorrect commit. I want to stop this behavior forever. (And I'm hesitant about fooling around with git commands in Terminal.)

Mandarin answered 23/10, 2019 at 21:57 Comment(9)
are you using github or another git repository hosting service? Or, is all of your work completely local?Begun
When you select that wrong commit, what does it show in term of branch and files?Objection
@MCI, I was using GitHub for Project 2, but I had never done a push, and when I ran into this problem, I deleted the GitHub repository and removed it from Xcode. At this time both projects are completely local.Mandarin
@VonC: When I open Project 1 in Xcode, one file shows a couple of recent changes. The other files show no changes (correct). When I open Project 2 in Xcode, the git navigator shows both projects (as in my second screenshot). The files listed for Project 1 are Project 1 files and I see the same couple of changes there as when I open Project 1 in Xcode. So, both projects appear okay, but Project 2 shows both local repositories, and when I did the commit in 2, both projects were committed using the same message.Mandarin
@Mandarin When you click on the wrong commit, do you see files from the other project? The fact that Project2 shows both projects is certainly the issue: the commit View is a filter which will show all commits from all repos in the current project.Objection
@Von: When I look at either of the two sections in the Project 2 git navigator, I only see files for the respective project. One more tidbit, before I started to use git, I had several folders of Project 1 files, each representing older versions of Project 1. I did a commit in the oldest version, then moved the .git folder to the next newer version, did another commit, and repeated once again. My intent was to build the .git repository up sfrom the old, old versions. But I didn't do that in Project 2 -- it has always been just one folder of files.Mandarin
@Mandarin maybe you have too much .git folder laying around. Note: to import older working tree, you can do so from one Git repo: cd /path/to/my/unique/repo; git --work-tree=/path/to/old/worktree1 add .; git commit -m "Import worktree v1"; repeat for v2 and so on.Objection
It's still happening: for every commit in project 2, I get a commit in both projects, then I do "git reset HEAD^" in the project 1. @Objection thanks for the tip. Unless I learn of a solution, my plan is to (1) delete the hidden .git folder in project 1 (I'm happy to let it go), (2) do a new "initial" commit in project 1, and hope the two projects stay separate. The connection between the projects remains a mystery. Apparently mine is a unique situation.Mandarin
More: If I remove the .git folder in project 1, then project 1 shows no git and project 2 shows only its own git. If I create a (new) git repository in project 1, it appears with an initial commit in project 1 AND project 2. I've tried various combinations of deleting the .git folders, creating new repositories, etc., but whatever I do, the same thing happens: Project 1 only shows its own git repository, and commits there affect that only repository, whereas Project 2 always shows both repositories and commits there affect both. Project 2 should be ignorant of project 1, but it's not.Mandarin
M
10

Mystery solved. I created Project 2 by copying numerous files from Project 1, but three image files ended up as references to the Project 1 files, not copies. When I removed those references and added copies instead to Project 2, the GIT navigator stopped showing the Project 1 repository.

Summary: Project 2 showed both GITs because Project 2 referenced some files in Project 1.

There are two ways to fix the trouble:

A. Manually: Open Project 2, select each file (one by one), and check that the Full Path shown in the Identity and Type inspector is a Project 2 path. Fix any files that reference Project 1.

B. Using Terminal: The Xcode project control file is a text file, so checking all Full Paths can be automated using Terminal commands, thusly:

(I assume the two projects are named Project_1 and Project_2, and that their project folders share the same parent folder.)

  1. Duplicate Project_2.xcodeproj (which is actually a bundle).

  2. Change the duplicate's filename suffix from .xcodeproj (I used .xxx). The duplicated "file" becomes a subfolder containing four items, one of which is a text file ("project.pbxproj").

  3. Launch Terminal.

  4. In Terminal, execute "cd path_to_project_2_subfolder". Shortcut: type 'cd', space, drag the folder that as created in step 2 to Terminal, and hit return.

x. (Verification step.) In Terminal, execute "ls" to show the four items. They should be:

project.pbxproj  project.xcworkspace/  xcshareddata/  xcuserdata/
  1. In Terminal, execute "strings project.pbxproj | grep ../Project_1/". This shows all lines in project.pbxproj that reference Project_1. '|' is a pipe command that sends the strings output to grep which filters the strings. Because my two project names differed by only one character, I surrounded the project name with '../' and '/' to limit the output to directories and files that belong to Project_1, but you could just use the bare project name if your two names are dis-similar. The commands and arguments are case-sensitive. You should see some lines that reference Project_1. You could also open project.pbxproj in a text editor and search for lines containing Project_1.

One of my files (sharing.png) appeared thusly:

1F4E32C32343DE1A0053C239 /* sharing.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = sharing.png; path = ../../../../Project_1/sharing.png; sourceTree = ""; };

  1. Open Project_2 in Xcode and fix the files that show as references in the strings command. Along the way, use Method A (above) to verify your work.

  2. Check that the GIT navigator only shows Project 2. You might have to close Xcode and relaunch.

Mandarin answered 15/11, 2019 at 7:49 Comment(1)
finally rename Project 2 -> Project 2 bak, XCode clean, then rename back, really worked !!!Fabrice
C
0

I had the same problem, I think it happened because I Initialised git twice on one project. First when I wanted to create the project folder I executed "git init" in the terminal, and later when I wanted to create the project itself by Xcode (a swift project), I did not remove the check mark from initiate a git repo for project. So I have initiated two git repos for one project.

Carce answered 24/9, 2020 at 12:16 Comment(0)
C
0

None of the above worked for me as there were no file references to the other project.

What did work was deleting the derived data folder using Xcode -> Settings -> Locations

Croze answered 17/9 at 23:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.