简体   繁体   中英

How can I go go back to my master branch without losing all previous commits?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM