0

I have made a terrible mistake.

Lets start when I had a local repo (used for developing), and a remote bare repo used for backup: no one else use this repository, just me.

Let's start from version A, pushed from local to remote. Version B was also pushed (call it Br, B Remote), but later I get back to version A locally, and made a version Bl locally.

I have done serveral other versions locally; when I tryied to push them I have encountered some difficulties since remote branch was at Br (A->Br), and local branch was at D... (A->Bl->C->D).

I have followed some online guides and I typed the command: git reset srv/DevelopBranch

Now,

  • both branches, local one and remote one are pointing to Br,
  • some uncommitted changes are pending but they seems only to be relative C->D changes, not all changes since A,
  • all commit after Bl seems to be erased
  • if I check the tree, I have A->Br (for both local and remote branch) -> some pending changes, I also have A->Bl, and Bl seems to be in a detached state (no branch label attached)

What I hope to do: restore local and remote branches as before: A->Bl->C->D

Thank you so much... Roberto

Roberto
  • 33
  • 6
  • Does this answer your question? [Can I recover a branch after its deletion in Git?](https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git) Although you didn't explicitly *delete* the branches, the same method of recovery applies: find the commits you want, and create branches pointing at them. – IMSoP May 26 '22 at 13:39
  • 1
    Incidentally, `git reset` on its own would not have the far-reaching effects on multiple branches that you describe; so either you've run other commands you haven't told us about, or the situation isn't as bad as you think. – IMSoP May 26 '22 at 13:41
  • 1
    Git reset only operates on the branch currently checked out, so if you run "git reset srv/DevelopBranch" you'd be resetting the current branch back to the same commit currently referenced by "srv/DevelopBranch"; and hence leave the accumulated previous changes in your Staging Area. – Alexis Määttä Vinkler May 26 '22 at 14:06

1 Answers1

0

FULLY SOLVED (thank you a lot for your suggestion):

Recaps, before reset I had:

Local Branch    A -> Bl -> C -> D -> E
Remote Branch     \->Br

Target:

Local and remote A -> Bl -> C -> D -> E

Instead of getting the "target" branch status, i did a git reset obtaing something like the following

Local and remote A -> Br -> some uncommited changes

Trying to work as safe as possible, at first I made a new branch "git branch RescueVer", then I saved each modified file committing to this branch (this branch will be omitted in the following status three). Let's get back to the working branch "git checkout [working_branch]" that actually points to Br, B Remoto.

Get back to version A with "git reset --hard Head^", I have:

Local branch    A
Remote Branch    \-> Br

In the log file I pick all the sha1 id from version A to the last ver, each rows has "preceding_sha1 this_ver_sha1 This_ver_commit_title(example: A)". Use this_ver_sha1 to restore each version on current branch whit git merge, for example first type "git merge [sha1_ver_Blocal]" and get

Local branch     A -> Bl
Remote branch     \-> Br

Repeat "git merge [sha1_ver]" to restore version C, then D, then E... At the end I had the same situation I had before git reset:

Local Branch    A -> Bl -> C -> D -> E
Remote Branch     \->Br

and an extra branch with some other files, not better identified.

Ok, this solves a lot of troubles, but still missing the last step: remove Br from Remote branch and duplicate local branch to remote one, as this:

Local and remote Branch    A -> Bl -> C -> D -> E

EDITED:

Simpler than I can imagine, I have to do no more than a forced push... this overwrites remote branch with local one!!!

Wonderful, doing this at the end I finally get the desired structure:

Local and remote Branch    A -> Bl -> C -> D -> E

Thank you again.

Roberto
  • 33
  • 6