1

I accidentally run git branch <branchA> <branchB> -f and can't go back to previous state... As a result of that, I received too many changes...

I originally wrote the architecture using Draw.io in a branch I created a long time ago. When I thought it was time to merge it, I couldn't do it because I got the following message.

There isn't anything to compare. master and document/initial-architecture are entirely different commit histories.

enter image description here

So I looked at this URL (There isn't anything to compare. Nothing to compare, branches are entirely different commit histories).

So I ran the following code.

81906@DESKTOP-608QNA0 MINGW64 ~/Documents/slackbot-gpt3 (document/initial-architecture)
$ git branch master document/initial-architecture -f

81906@DESKTOP-608QNA0 MINGW64 ~/Documents/slackbot-gpt3 (document/initial-architecture)
$ git checkout master
Switched to branch 'master
Your branch and 'origin/master' have diverged,
and have 25 and 28 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

81906@DESKTOP-608QNA0 MINGW64 ~/Documents/slackbot-gpt3 (master)
$ git push origin master -f
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: error: GH006: Protected branch update failed for refs/heads/master.
remote: error: Cannot force-push to this protected branch
To https://github.com/Suchica/slackgpt3.git
 ! [remote rejected] master -> master (protected branch hook declined)
error: failed to push some refs to 'https://github.com/Suchica/slackgpt3.git'.

Here, 37 changes have occurred, and I want to undo them, but I'm having trouble figuring out how to do it. The Git graph looks like this.

enter image description here

And here is the result of git reflog.

enter image description here

Hiroshi Nishio
  • 222
  • 4
  • 11

2 Answers2

1

git reflog might save you if you haven't made too many moves since.

git reflog shows your movements rather than your branches history, find the SHA of the last good place and git reset --hard _YOUR_SHA_.

YMMV.

gingerbreadboy
  • 7,386
  • 5
  • 36
  • 62
  • I pasted the results of the git reflog. What SHA number should I go back to before running `git branch master document/initial-architecture -f` ? – Hiroshi Nishio Jan 16 '23 at 17:04
  • 1
    my gut says `git reset --hard 7ede281` is what you want BUT from my seat it's very hard to say for certain. I say `7ede281` because this LOOKS LIKE the most recent bit of actual commited work (`Improved the beginning of the prompt...`). Again, YMMV, proceed with caution, and godspeed. – gingerbreadboy Jan 16 '23 at 17:13
  • I would suggest reading the docs for `reflog` but git docs are notoriously dense :o – gingerbreadboy Jan 16 '23 at 17:14
  • https://git-scm.com/docs/git-reflog – gingerbreadboy Jan 16 '23 at 17:14
  • You can think of traversing the reflog as travelling back in time. In theory if you don't go passed any `commit` entries you shouldn't lose anything good, you should land back where you were when you made that commit, and you get a do-over on your rebase or whatever else you did after. – gingerbreadboy Jan 16 '23 at 17:19
  • if you change your mind after you`git reset --hard 7ede281` you can always `git reset --hard a6d71e5` to go back... to the future! – gingerbreadboy Jan 16 '23 at 17:21
1
git branch -f branchA branchA@{1}

See the reflog syntax in git help revisions.

The @{1} syntax is the pretty-much-universal backout from oopses like this.

Shell history expansion (introduced by !) has a "current line" marker #, and a "last word" selector $, making the totally-unnecessary-here shorthand for repeating the branch name

git branch -f branchA !#$@{1}

which I like too much to not mention.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • I pasted the results of the git reflog. What SHA number should I go back to before running `git branch master document/initial-architecture -f` ? – Hiroshi Nishio Jan 16 '23 at 17:04