Home > database >  How to return to my previous git branch code
How to return to my previous git branch code

Time:01-31

Good afternoon, I am new to Git Hub, so 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 is changed, my question is how do return to my previous code?

Please, is anybody can help me?

I tried with : git reset, git revert, git reflog....

CodePudding user response:

You can use the git reflog command to find the previous state of your branch and return to it. Here's an example of how to do this:

git reflog

This will list all the reference changes in your Git repository, including branch updates and other Git operations. Look for the entry that corresponds to your branch before the rebase operation, and make a note of the reference hash (a long string of characters).

Next, run the following command to reset your branch to the previous state:

python

git reset --hard <reference hash>

Replace with the actual reference hash you obtained from the git reflog command. This will reset your branch to the state it was in before the rebase operation, effectively undoing the rebase and returning you to your previous code.

Note that this operation is irreversible, so be sure to double-check the reference hash before resetting your branch.

CodePudding user response:

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.

  • Related