Horje
How to Undo Git Reset?

Did you make a mistake with `git reset –hard` and lose your uncommitted changes? Depending on the situation, there might be a way to get them back. Here’s what you can do:

What is `git reset –hard`?

This command discards any uncommitted changes and moves your project back to a specific point in history. It’s a powerful tool, but use it carefully!

Recovering Lost Work (if possible):

gitreflog

complete process

Step 1. Checking the Git Reflog:

Git keeps a log of changes to your project’s history. Use the `git reflog` command to view this log. Look for the commit hash (unique code) before the `HEAD~<number>` reset (where `<number>` indicates how many commits you went back). This is the commit you want to recover from.

git reflog

Step 2. Going Back in Time (partially):

Use `git reset –hard <commit_hash>` to detach your project to that specific commit. This essentially undo the `git reset –hard` and brings you back to the desired state.

git reset --hard <commit_hash>

Important Reminders:

  • This approach also erases any uncommitted changes you made after the commit you’re reverting to.
  • There’s no guarantee of success, especially if you haven’t used `git reflog` before or if the reset was a long time ago.
  • Consider using `git reset –soft` or `git checkout` for safer alternatives that won’t erase your work.



Reffered: https://www.geeksforgeeks.org


Git

Related
How to Ignore &#039;node_modules&#039; Folder in Git? How to Ignore &#039;node_modules&#039; Folder in Git?
Difference between .gitignore and .gitkeep Difference between .gitignore and .gitkeep
How to View Git Log of One User&#039;s Commits? How to View Git Log of One User&#039;s Commits?
How to Undo Working Copy Modifications of One File in Git? How to Undo Working Copy Modifications of One File in Git?
How to Clone Only a Subdirectory of a Git Repository? How to Clone Only a Subdirectory of a Git Repository?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
18