-3

I somehow have been committing all changes with Xcode somewhere not on any branch if I understand the message (in the title) correctly. I do not remember how and when it happened, probably when I was trying to checkout a version from the master while being on a branch.

Now, in the source control navigator in Xcode, if I click one of the branches or the master, I only see very old commits. I get to see recent commits only if I click the project/app name at the very top in the source control navigator in Xcode.

Also, if I checkout a recent version (within last 3 months), all the later ones get lost (I do not see them anywhere anymore). And if I try to cherry pick code Xcode tells me that the working copy is currently not on a branch.

Another strange thing is git status says "HEAD detached from 0c3e248" (marked with 3 in a white circle in the screenshot below). This point is only about a week ago and still doesn't seem to be on any branch, while the latest "proper" commit was on "reserve" branch (marked with 5 in a white circle in the screenshot).

I want to get everything back to normal. if that is not possible, I want to be able

  1. to go back to an earlier version (marked with 2 in the screenshot) without losing later ones
  2. to cherry pick the code

Tried searching the internet for this issue but wasn't very lucky. Please, help

Am not an expert in git, so please, bear with me if I didn't explain it right and tell me what to do at a beginner level.

below are screenshots of the git tree produced by "git log --graph --all --oneline" with the following marks:

  1. -- the latest commit (not on any branch)
  2. -- the commit I want to get back to (or to the one right before it, they are not on any branch either)
  3. -- the commit for which git status returns "HEAD detached from" (not on any branch)
  4. -- last commit on master branch
  5. -- latest commit on any branch at all

1st bit of the tree

2nd bit of the tree

3rd/last bit of the tree

TRusty
  • 1
  • 1
  • Are you in detached head mode? – matt Jan 06 '20 at 05:05
  • I think you should use better tools. Study the situation with SourceTree for example. – matt Jan 06 '20 at 17:53
  • @matt, to your first comment: You are right, thank you! But the strange thing is git status says "HEAD detached from 0c3e248". And this point is **only** a couple of days ago, while the latest "proper" commit was on "reserve" branch on Sept 20 – TRusty Jan 06 '20 at 18:01
  • @matt, thanks for the advice. I will look at it. Would you let me know how to straighten up the situation? – TRusty Jan 06 '20 at 18:02
  • I produced a tree-like structure using "git log --graph --all --oneline". Am reluctant to migrate to any 3rd-party git mgmt software, as am afraid to mess the repo and to lose what I have now. If moderators still do not like this, I'll just have to go through all commits starting from the latest backwards and copy all the modified files, find the working copy and pick chunks of code from the created files (a very bad way of cherry picking). This will save some nerves for me, though. Thank you moderators. Nice "talking" to you @ElTomato – TRusty Jan 07 '20 at 17:33
  • Ok so you do have a detached head, as I asked you. No problem, just check out a branch and merge. See for example https://stackoverflow.com/questions/10228760/fix-a-git-detached-head – matt Jan 07 '20 at 17:47
  • Also https://stackoverflow.com/questions/7124486/what-to-do-with-commit-made-in-a-detached-head/7124513#7124513 – matt Jan 07 '20 at 17:58
  • By the way: “Am reluctant to migrate to any 3rd-party git mgmt software” That’s backwards. Xcode _is_ the 3rd party software that messes up the repo. Its git support is atrocious. I never use it. Sourcetree or command line is the “real” way to talk to git. – matt Jan 07 '20 at 18:53
  • Thanks, @matt. May I clarify a couple of things with you, please? – TRusty Jan 07 '20 at 19:08
  • @matt, thanks again, noted your last comment and will stick to the Terminal for now – TRusty Jan 07 '20 at 19:10
  • @matt so, re the detached head. will it work if I modify the second option you mentioned as follows: 1. get the hash for the earliest detached head commit, right above the one marked 5 (git log -n X, where X will be close to 85 to get the full hash, lets say ABC123...). 2. switch to master (git checkout master). 3. branch at the commit from step 1 (git branch version2 ABC123..). 4. merge with master (git merge version2). Will that bring all the commits on detached head to the branch "new-version2"? – TRusty Jan 07 '20 at 19:26
  • What you want to merge to master is whatever is the head commit NOW. Look again at all the answers on this topic. – matt Jan 07 '20 at 19:32
  • You need to stop and learn what is git. A branch is just a name pointing to a commit. A commit is preserved as long as a name points to it OR it is the parent of a commit that is preserved. So branch names are at the END of the chain, not the start. And that is what preserves the whole chain. Merely branching from right where you are now is sufficient to save all your work if that’s all you care about. If there is no upstream repo you could just do that and move on (and forget master). – matt Jan 07 '20 at 19:38
  • Thanks @matt, now it's clear. sorry for too may questions – TRusty Jan 07 '20 at 19:41
  • I repeat, try Sourcetree. It shows clearly where you are and why. And you would never have gotten detached because you’d have seen a big warning. – matt Jan 07 '20 at 19:48
  • I understand now, that quite some time ago I checked out an earlier commit and started changing it without branching out and that caused all the mess. Thank you @matt for your help and advice. Will have to figure out the Sourcetree. – TRusty Jan 07 '20 at 21:40

1 Answers1

0

As it was kindly noted by @matt the problem was detached head, so the link he provided (What to do with commit made in a detached head) did help me. Thank you Matt for all you advice and patience.

TRusty
  • 1
  • 1