0

I know that undoing a single git reset or git reset --hard command has been asked and answered, but I have really messed things up and am not sure how to proceed because I need to undo several mistakes. I was actually following some of the existing answers and seemed to have messed things up even more, and it is very important that I recover these files. Now I'm too nervous and just need some exact help and the exact command to do what I need.


My original issue is that I checked out a branch locally. I thought that my local branch was the out of date one and that the remote branch was the most up to date. (I was working on two separate computers on the same branch at different times.) So I did a git reset and then a git reset --hard because I forgot to include the --hard the first time. However, I really messed up because it was my local branch that had the most up to date code on it, and so I inadvertently wiped out a day's work.

Here are the exact commands that I ran, in sequence:

> git checkout cleanup-client             <-- I want to return to the point of time directly after this command
> git reset origin/cleanup-client
> git reset --hard origin/cleanup-client
> git status
> git reflog
> git reset 5011960                       <-- I tried reading existing answers and performed this
> <I unstaged and discarded all changes via VS Code Git integration>
> git reflog
> git reset bc6968c                       <-- Another attempt to recover
> <I unstaged and discarded all changes via VS Code Git integration>

Here is the output of git reflog now:

> git reflog
bc6968c (HEAD -> cleanup-client, origin/cleanup-client) HEAD@{0}: reset: moving to bc6968c
5011960 (origin/BACKUP-cleanup-client) HEAD@{1}: reset: moving to 5011960
bc6968c (HEAD -> cleanup-client, origin/cleanup-client) HEAD@{2}: reset: moving to origin/cleanup-client
bc6968c (HEAD -> cleanup-client, origin/cleanup-client) HEAD@{3}: reset: moving to origin/cleanup-client
5011960 (origin/BACKUP-cleanup-client) HEAD@{4}: checkout: moving from migrate-client-to-poetry to cleanup-client
7b2242a (origin/migrate-client-to-poetry, migrate-client-to-poetry) HEAD@{5}: commit: Renaming existing clients to use `snake_case`
91797f6 HEAD@{6}: commit: Update server README

What I am confused about and hesitant about is which SHA to actually use from git reflog. Are the SHAs listed the SHA for before or after the command to the right was executed?

In other words, is git reset --hard 5011960 the command I now need? (Note: I have not ran this, yet, after all the above commands. I.e., the above commands are where I'm at now.)

6
  • 1
    Each hash in the reflog refers to a commit. The message you see next to it is the commit message. You should be able to move between them and examine each if you are unsure which one you want. Commented Apr 5, 2024 at 16:48
  • Does this answer your question? stackoverflow.com/questions/2092810/… Commented Apr 5, 2024 at 16:50
  • 1
    Yes, 5011960 looks like the correct commit from your description and matches your commands Commented Apr 5, 2024 at 17:48
  • 1
    I feel like no one has actually answered this question yet, including the existing answers. The way I'm reading the question, the answer to: "is git reset --hard 5011960 the command I now need?" Is yes, and you need the --hard part to get your files back to how they were at the time of that first checkout. Commented Apr 5, 2024 at 20:09
  • 1
    I'm confident enough in my comment that I made that an answer. Commented Apr 5, 2024 at 21:37

3 Answers 3

2

Concerning the output of git reflog:

Are the SHAs listed the SHA for before or after the command to the right was executed?

The SHA1s are those after the command was completed. You can clearly see this in the first entry:

bc6968c (...) HEAD@{0}: reset: moving to bc6968c

Obviously, you gave the command

git reset bc6968c

as mentioned by the message. This command will repoint HEAD to bc6968c, which is exactly the SHA1 you see at the beginning of the line.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for explaining this! I was really panicking earlier. Thanks for pointing out the entry that corroborates as well.
2

is git reset --hard 5011960 the command I now need?

Yes, that's exactly right. It's the --hard part that you were missing in the commands you already attempted. Take a look at this question and answers to see the difference between --soft, --mixed (which is the default if you don't specify), and --hard.

Only --hard will actually change your working directory files, which is what you're trying to achieve here.

In general, be careful when using git reset --hard, because it will blow away any changes you've currently made that are not committed yet. In your current scenario, your working state is unwanted, and you simply want your local files to go back to a previously committed state.

1 Comment

Thank you! Yea, I realized later that in my panic I wasn't using the --hard option, and so the results of the git reset were confusing me. In the pressure of needing to deliver things plus this and other mistakes, I was bogging myself down in confusion and panic.
2

git reflog shows commits just like git log. You can get the equivalent with git log -g --oneline.

If we reverse the order of your commands, and only keep the ones that move HEAD, it's easier to read.

> git reset bc6968c                       <-- Another attempt to recover
> git reset 5011960                       <-- I tried reading existing answers and performed this
> git reset --hard origin/cleanup-client
> git reset origin/cleanup-client
> git checkout cleanup-client             <-- I want to return to the point of time directly after this command
  1. bc6968c
  2. 5011960
  3. origin/cleanup-client
  4. origin/cleanup-client
  5. ???

And we can see the matching lines in the reflog.

bc6968c (HEAD -> cleanup-client, origin/cleanup-client) HEAD@{0}: reset: moving to bc6968c
^^^^^^^
5011960 (origin/BACKUP-cleanup-client) HEAD@{1}: reset: moving to 5011960
^^^^^^^
bc6968c (HEAD -> cleanup-client, origin/cleanup-client) HEAD@{2}: reset: moving to origin/cleanup-client
                                 ^^^^^^^^^^^^^^^^^^^^^
bc6968c (HEAD -> cleanup-client, origin/cleanup-client) HEAD@{3}: reset: moving to origin/cleanup-client
                                 ^^^^^^^^^^^^^^^^^^^^^
5011960 (origin/BACKUP-cleanup-client) HEAD@{4}: checkout: moving from migrate-client-to-poetry to cleanup-client
^^^^^^^

The commit you checked out is 5011960 which is referenced by origin/BACKUP-cleanup-client.

5 Comments

git log and git log -g --oneline not at all equivalent. The former summarizes the action that caused the change of commit, the latter shows the commit subject line.
@j6t Correct. git reflog and git log -g --oneline are equivalent.
Sorry, that was a typo in my previous comment. I meant to say that git reflog and git log -g --oneline are not equivalent due to the reasons given in my comment.
Oh yes, indeed. Don't know what I was looking at when I tested it the other day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.