Git branch not working for me in Xcode - xcode

I am working off a github branch, call it "main12"
I then do a git checkout -b newFeature.
I do some work on newFeature
I then need to go back to main12 to do some work
I close down Xcode.
I switch to main12 via terminal git checkout main12
Open xcode and verify xcode is on the main12 branch.
All my changes made in newFeature are now in my main12 branch
What gives? Did I miss a step? Why wouldn't my code in main12 be the way I left it?

Did you commit or stash the changes made to newFeature? If not, probably the command git checkout main12 have failed. Closely watch the command line output of that command and verify.
Any changes you make to the current branch should be committed or stashed(discard temporarily, store it back whenever you need) before checking out another branch.
Try the following command before you checkout main12
git commit -am "some commit message here"

It is likely the case that you forgot to commit your code before checkout to main12 branch. Currently, you should have uncommitted code (marked in Xcode as M/A besides the file name). You can switch back to newFeature branch, commit the code, and switch back to main12 branch.

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

Why doesn't Git track files modified in Xcode?

I'm using Git on the terminal to commit changes & push them to a remote repo.
Git can recognize when I create a file or modify a file via VS Code. However, when I work on my files on Xcode and save them, Git doesn't track any changes. It tells me:
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
What's the problem and how can I fix it?

Xcode 9.2 - Version control - how to revert back to earlier committed version

Im using Xcode 9.2 with bitbucket for version control. I have the past 4-5 hours worked in the master branch. I realise now that I want to keep the work that I have done today but not part of the master branch but rather as a new branch.
I have been able to push the existing version to the new branch, but it required me to commit to master first (locally).
Now I want to revert the master back to the version from yesterday, and commit this at the master. This is where the problem occur. I am able to revert back to the previous committed version by checkout the commit - but then I am not on the master branch anymore, and I can't commit to master or push to master.
How can I revert back to the master to a previous build, and basically remove the changes done the past 4-5 hours?
For the next time you realise before you commit:
Open up terminal
cd ${path to your project}
run git stash command
When you stash the not commited changes, now you should be safe to switch and make new branch
now change to the new branch and apply stash: git stash apply
Enjoy your changes.
Since I am confused as hell and this happens to me from time to time, I created alias in my .bash_profile file to delete the local commits by simply making one command in terminal:
alias fixFuckup="git reset HEAD~1"
simply run the command and you will be reset to the state before the commits. Actually this resets one commit.
git reset HEAD~1
to reset to the state before doing lot of commits, prefer using this.
git reset --hard HEAD^
for clarifying the changes please take further look at this:
what is difference between 'git reset --hard HEAD~1' and 'git reset --soft HEAD~1'?

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 ?

Can't Push to Git Repository

I am trying to push my recent commits to a Git repository that I have set up on Bitbucket, and for some reason it never pushes.
Xcode just gets stuck on the screen saying "Pushing Changes..." and "Preparing to Push Changes..." but it never completes. Then ends with "The working copy of 'Master' is currently up to date."
But it's not.
I have made changes.
Also tried adding more commits, but that didn't work either.
Any suggestions?
Git doesn't always work with Xcode, usually because of the crap files Xcode generates (eg xcworkspace etc), and it's pretty buggy in general, so I would just try pushing from the command line instead of through Xcode (which should also give you a more detailed error message if it doesn't work).
Try doing a git add and then git commit

Resources