Accidentally deleted xcode project files via Git commands - xcode

I accidentally deleted all my files by committing a new build, then deleting that commit. (git commit -m, git reset -hard HEAD^). Then I tried to use (git merge ) to undo the delete. I noticed that all the missing files were back, but all the contents were missing. To clarify, all the files that were missing after I deleted the commit was back, but contents of those files only included default Xcode markups and all my coding was still missing. Is anyone familiar with an issue like this? Any guidance would be greatly appreciated..
Steps:
git commit -m
git reset -hard HEAD^
git merge

You should be able to use git fsck to see any dangling blobs or commits which remain after a reset.
git fsck --cache --no-reflogs --lost-found --unreachable HEAD
You'll see something like this with any dangling objects:
Then just do git show with the blob id to inspect
git show fd2274ea24e214457fa865e6aa74a0a1b036291a
If it's the file you want, you can then write it to a file using git show {blob id} > filename. e.g.
git show fd2274ea24e214457fa865e6aa74a0a1b036291a > test.txt

Related

git clone did not pull all the merged files

Another developer created a branch, worked on it, and checked in code. He also did a merge from that branch to the master. Before cloning I see the merged files in the master. But after cloning from master via xcode, it did not pull the files that were checked into branch and subsequently merged into master.
I thought after merge anyone should be able to checkout master and clone and get all the merged files. But that is not happening. How to pull the entire merged code?
When I run git status, I get this output:
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: xyz/xyz.xcodeproj/project.pbxproj
modified: xyz/xyz.xcodeproj/project.xcworkspace/xcuserdata/HCCS.xcuserdatad/UserInterfaceState.xcuserstate
modified: xyz/xyz/Base.lproj/Main.storyboard
Untracked files:
(use "git add <file>..." to include in what will be committed)
MyPlayground.playground/playground.xcworkspace/
MyPlayground2.playground/playground.xcworkspace/
compare.playground/playground.xcworkspace/
no changes added to commit (use "git add" and/or "git commit -a")
I am at loss to understand the error and why it is not pulling all the files when cloned.
Some more details:
git pull
error: Your local changes to the following files would be overwritten by merge:
r2nr/r2nr.xcodeproj/project.pbxproj
r2nr/r2nr/Base.lproj/Main.storyboard
Please commit your changes or stash them before you merge.
error: The following untracked working tree files would be overwritten by merge:
MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata
MyPlayground2.playground/playground.xcworkspace/contents.xcworkspacedata
compare.playground/playground.xcworkspace/contents.xcworkspacedata
Please move or remove them before you merge.
Aborting
I ran git stash
git stash
'Saved working directory and index state WIP on master: 6d9b3d2 Merge branch 'branch01' Added ProviderApiCaller class to the code
HCCS#CEASJ311-4293 green-synapse % git pull
Updating 6d9b3d2..35d2b7e
error: The following untracked working tree files would be overwritten by merge:
MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata
MyPlayground2.playground/playground.xcworkspace/contents.xcworkspacedata
compare.playground/playground.xcworkspace/contents.xcworkspacedata
Please move or remove them before you merge.
Aborting
So I removed the three files:
MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata
MyPlayground2.playground/playground.xcworkspace/contents.xcworkspacedata
compare.playground/playground.xcworkspace/contents.xcworkspacedata
Next
git pull
That seemed to work
Then I enter xcode project
I get error
The project ‘r2nr’ is damaged and cannot be opened due to a parse error. Examine the project file for invalid edits or unresolved source control conflicts. Path: /Users/HCCS/myproj/r2nr/r2nr.xcodeproj.
How to resolve conflicts?
*
While git stash will save (and then git reset --hard to remove) modified files, it does nothing about untracked files by default. It seems likely that your "Another developer" committed the untracked files, which was probably a mistake on his or her part; that's what produced the errors with the untracked files.
(You can use git stash -u, but I prefer to avoid git stash in general, and git stash -u is particularly nasty to work with, so I would suggest not doing that.)
In any case, after removing your own untracked files, your git pull appears to have worked. Remember that git pull means:
run git fetch; then
run a second Git command of your choice, either git merge or git rebase
and—assuming the git fetch itself works, which it usually does—the second command may stop in the middle, or complete. The output from the command tells you which of those happened. But assuming it completed successfully, all your Git problems are now solved.
[but xcode now says]
The project ‘r2nr’ is damaged and cannot be opened due to a parse error. Examine the project file for invalid edits or unresolved source control conflicts. Path: /Users/HCCS/myproj/r2nr/r2nr.xcodeproj.
Given that your "another developer" appears, from what we know above, not to understand how to use Git, perhaps this same person committed unresolved conflicts, rather than resolving them. This is now an xcode problem, but solving it may require that you discard the other developer's work and re-do it yourself, or repair anything he or she damaged. You cannot use normal Git tools to resolve a conflict here as the conflict is already resolved (incorrectly, apparently).
In general, I recommend since Git 2.23+
git config --global pull.rebase true
git config --global rebase.autoStash true
That way, a simple git pull would stash your work in progress for you, pull, rebase your local commits on top of the updated branch, and unstash.
Then you can start resolve any conflict.
But if those are too complex regarding, you can force your own version with:
git stash show -p | git apply && git stash drop

git pull added tons of untracked files and modified files

I am not very familiar with git as I just use it in the most basic manner. Lately I've been running into this strange behavior which makes no sense to me and causes a huge disturbance in my productivity. For certain branches when I run git pull instead of just getting the latest commits from the remote repository like I expect, I get an ENORMOUS list of modified/deleted/added files as well as an enormous list of untracked files. The branch I am pulling from is our team's master branch. I am just trying to keep my local copy of master in sync. What are these other untracked files showing up as well as these modified files? I didn't touch any of these other files.
What makes the problem worse is that I can't delete these untracked files or anything. I've tried `git clean -fx' and it only removes 3-4 files but still leaves hundreds. At this point I just want to get rid of all of these files, delete the branch, and pull the branch from remote again.
I tried doing the following:
git fetch --all
git reset --hard origin/<remote_branch_name>
but I received an error because Git couldn't create a symbolic link. I am using Git on Windows.
Why is git pull doing this, and what should I do?
EDIT: I was finally able to run the git reset command when I ran my prompt as an admin, but it still doesn't explain this weird git pull behavior.
It seems to me that the 'git pull' action did not complete successfully.
It is possible that you modified some of the files as an admin, and when pulling as non-admin user git tries to modify or delete a file and has no permissions to it.
Therefore you stay in some half baked state.
What do you see when you run git status?
Is there any error in the git pull?

Merge conflict while trying to merge stashed code

So I stashed my changes for my Swift project so that I could make a pull request and then merge the changes I made with the new pull. However, whenever I try to merge my changes I get a merge conflict, regarding the UserInterfaceState.xcuserstate XCode file. Nothing I'm trying is allowing me to merge my stashed code, and I'm worried that I'm going to lose an afternoon's worth of code.
I tried adding *.xcuserstate to .gitignore, but that didn't seem to do anything. I then tried deleting the file, which created a modify/delete conflict from the version in the stash. Here's the error I'm getting when I try to get the stashed code:
$ git stash apply
CONFLICT (modify/delete): myProj.xcodeproj/myProj.xcworkspace/xcuserdata/lnd.xcuserdatad/UserInterfaceState.xcuserstate deleted in Updated upstream and modified in Stashed changes. Version Stashed changes of myProj.xcodeproj/myProj.xcworkspace/xcuserdata/lnd.xcuserdatad/UserInterfaceState.xcuserstate left in tree.
Trying what Flows recommended this was the output I received:
$ git reset --hard
HEAD is now at e509ffa Fixed bugs
$ git stash pop
myProj.xcodeproj/myProj.xcworkspace/xcuserdata/lnd.xcuserdatad/UserInterfaceState.xcuserstate: needs merge
unable to refresh index
$ git rm myProj.xcodeproj/myProj.xcworkspace/xcuserdata/lnd.xcuserdatad/UserInterfaceState.xcuserstate
myProj.xcodeproj/myProj.xcworkspace/xcuserdata/lnd.xcuserdatad/UserInterfaceState.xcuserstate: needs merge
rm 'myProj.xcodeproj/myProj.xcworkspace/xcuserdata/lnd.xcuserdatad/UserInterfaceState.xcuserstate'
$ git stash pop
CONFLICT (modify/delete): myProj.xcodeproj/myProj.xcworkspace/xcuserdata/lnd.xcuserdatad/UserInterfaceState.xcuserstate deleted in Updated upstream and modified in Stashed changes. Version Stashed changes of myProj.xcodeproj/myProj.xcworkspace/xcuserdata/lnd.xcuserdatad/UserInterfaceState.xcuserstate left in tree.
Is there anything that I can do to fix this? Nothing seems to be working, so any help would be greatly appreciated, thanks.
You can try this
Reset master with --hard option to origin/master
Apply the stash with stash pop
Git should tell you there is a conflict. Edit UserInterfaceState.xcuserstate file to see the conflict and correct it.
git commit
If it doesn't work, could you paste all git output of the commands ?

Issues with git branch checkout due to untracked working tree files

I thought I put the days of Xcode + git issues behind me. Guess not. I am getting this git error when trying to checkout another branch.
error: The following untracked working tree files would be overwritten by checkout:
RCAlpha.xcodeproj/project.xcworkspace/xcuserdata/andrewjl.xcuserdatad/UserInterfaceState.xcuserstate
RCAlpha.xcodeproj/xcuserdata/andrewjl.xcuserdatad/xcschemes/RCAlpha.xcscheme
RCAlpha.xcodeproj/xcuserdata/andrewjl.xcuserdatad/xcschemes/xcschememanagement.plist
Please move or remove them before you can switch branches.
Aborting
Very well I say, let me remove these files:
andrewjl$ git rm --cached RCAlpha.xcodeproj/project.xcworkspace/xcuserdata/andrewjl.xcuserdatad/UserInterfaceState.xcuserstate
fatal: pathspec 'RCAlpha.xcodeproj/project.xcworkspace/xcuserdata/andrewjl.xcuserdatad/UserInterfaceState.xcuserstate' did not match any files
At this point I'm not sure what to do. These files are all listed in my .gitignore and I also gave git clean -f -d a try as well. No dice. Anyone know what's going on here?
The files are untracked: git rm --cached cannot find them because there are not in the index. Just delete them, using your file manager or rm. Then checkout should work as expected. Note that git status will show you what git sees the files as (tracked, changed, untracked; with an additional option it will also display ignored files).
they arent in git but there locally .. that means when you switch those would be lost and git doesnt allow that
remove them locally:
rm RCAlpha.xcodeproj/project.xcworkspace/xcuserdata/andrewjl.xcuserdatad/UserInterfaceState.xcuserstate RCAlpha.xcodeproj/xcuserdata/andrewjl.xcuserdatad/xcschemes/RCAlpha.xcscheme RCAlpha.xcodeproj/xcuserdata/andrewjl.xcuserdatad/xcschemes/xcschememanagement.plist
they are recreated by xcode anyway. nothing important in there!
Try this
git rm --cache */xcschemes/xcschememanagement.plist
git commit -m "Good bye xcschememanagement.plist"
Git stash and git checkout "yourBranch"
git clean -f
solve the problem for me, they are untracked files when run clean they get remove.

Github. How do I make changes to what is ignored via .gitignore?

I have an Xcode project that uses git for version control. I have a .gitignore file to ignore the build subdirectory:
build/*
I recently added a subdirectory that contains an Xcode project and forgot to update the .gitignore file before checking it in.
Is there any way to make git ignore the build subdirctory now, after the fact?
Thanks,
Doug
git rm --cached dirToignore
echo dirToignore >>.gitignore
From there, a new commit will record that:
dirToignore is no longer par of versioned data
dirToIgnore won't show up anymore in git status
See this SO question for similar advices.
If you want to amend previous commit in order to remove said subdirectory from an old commit, see this SO question:
git commit --amend
can help you remove it from at least the last commit.

Resources