0

I had a problem with fetch and rebase.

I have a branch named oybek. Then, I typed commands: git fetch, git rebase origin/main.

Then all my code was changed. How do I return to my previous code?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • The immediate solution in your case would have been `git checkout oybek@{1}` or `git reset --hard oybek@{1}`. If you didn't want your code to be changed with the code from GitHub you shouldn't have used `git rebase origin/main`. – mkrieger1 Jan 31 '23 at 17:47
  • Does this answer your question? [Undoing a git rebase](https://stackoverflow.com/questions/134882/undoing-a-git-rebase) – mkrieger1 Jan 31 '23 at 17:48

1 Answers1

0

Firstly, commit/stash every changes first in case you mess things up. Once things are committed you can recovery them more easily if you go wrong.

You can see the commits you visited in git reflog. Search for rebase (start) pattern, the commit below that commit is the one you want. Copy its hash.

Let's say the hash is xxxxxxxxx. You can now git checkout -b my-previous-place xxxxxxxxx. Then there will be two branch, one before rebase and one after rebase.

This workflow is very safe because you lose nothing compare to using git reset.