0

I'm using git through Xcode. A few days ago I added a remote repository to my project. I didn't notice it then but for some reason Xcode deselected the master branch as my current branch, and for the last two days all my commits are (...) <- where? That's the question. Now, every time I try to push my commits I get "The current branch could not be determined" message. I can checkout master but then I'll lose two days worth of commits.

How can I now choose the master branch as my current branch without losing the progress?

I tried to branch from the last master commit but it still gets me nowhere, since there's no way to save more recent commits.

jscs
  • 63,694
  • 13
  • 151
  • 195
Bartosz Kunat
  • 1,485
  • 1
  • 23
  • 23
  • 1
    First make a backup of the entire project folder (it will have a `.git` folder in it, so it'll backup your git too).. Then try the following: `git checkout -b Temporary`, `git commit -u "Backup Branch"` and `git push -u origin Temporary` . This creates a temporary branch, commits all your stuff to it, and then pushes with tracking to the remote to Temporary branch. Now you can checkout master and merge with Temporary. If everything fails, you have your backup project folder.. Make another backup of that and try something else.. – Brandon Dec 26 '17 at 19:54
  • 1
    I don't use Xcode (I've touched it once or twice but never enough to get sticky with it :-) ) but I suspect you are in "detached HEAD" mode. From the command line, run `git branch` to list your branches, which will list the *current* branch with a `*` next to it. If the starred line says `* (HEAD detached ...)` you have this detached-HEAD mode. – torek Dec 26 '17 at 19:55
  • If you've been making commits, then they're committed. You're not going to lose work by switching to another branch. That's the whole point of SCM. You'll probably have to do some work in the command line, though. – jscs Dec 26 '17 at 20:03
  • Thanks @Brandon. When I created a temp branch trough terminal all of my previous commits magically appeared there and after that I just merged the new branch into master. In the future I'll think twice before I start solving my version control problem trough Xcode ^^ – Bartosz Kunat Dec 26 '17 at 20:19
  • 1
    There are a few Git GUIs that do a much better job than Xcode. Some are free. I use GitUp myself, which is open-source. – jscs Dec 26 '17 at 20:20

1 Answers1

1

Moving my comment to an answer:

One solution is to make a temporary branch (via command-line) which will have all your commits on it, and then merge that into master.. XCode-9 is quite a bit buggy with the new git tools it has incorporated (and it doesn't show current branch anymore)..

To do the above, you can do:

git checkout -b "Temporary"

which will switch you to a branch called "Temporary".

Then you do:

git commit -m "Your Changes".

to commit the changes (if any) to "Temporary".

Then you do:

git push origin -u "Temporary"

which will push the "Temporary" branch to the upstream and track changes on it. Finally, you can just merge that branch into master or git pull origin master to merge master into that branch.

Since it's already based off master though, there might not be a need to pull unless someone else commit changes into master while you were working on it.

Before doing unfamiliar git operations, I always make a backup of the entire project folder which has a .git hidden folder inside it. This will backup your commits and project.

Not sure if I should mention this, but typically you always want to work on another branch other than master.. IE: Feature/BlahBlah and then merge into master.

Brandon
  • 22,723
  • 11
  • 93
  • 186