Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
How to Undo Git Reset?
Next article icon

How to Undo Git Reset?

Last Updated : 03 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Next Article
How to Undo Git Reset?

H

htomarec8c
Improve
Article Tags :
  • Web Technologies
  • Git

Similar Reads

    How to Undo a Git Rebase?
    Git rebase is a powerful tool for simplifying a feature branch by integrating changes from the main branch. However, sometimes things can go wrong, and you may need to undo a rebase. Whether it’s due to conflicts, errors, or simply a change of mind, knowing how to safely undo a Git rebase is essenti
    3 min read
    How to Undo Git Reset --hard HEAD~1?
    Accidentally using git reset --hard HEAD~1 can be a stressful experience, as it discards the latest commit and the changes it contains. However, there are ways to recover from this situation. Here’s a detailed guide on how to undo a git reset --hard HEAD~1. Understanding the CommandThe git reset --h
    2 min read
    How to Reset to Head in Git?
    Git has revolutionized version control systems and made it easy the collaborative development process. However, mastering Git requires understanding the list of commands and functionalities. Among these commands, "reset to HEAD" is an important one, allowing developers to undo changes and revert to
    3 min read
    How to Undo Last Commit in Git?
    Sometimes, you might need to undo the last commit, whether it's due to a mistake, an incorrect commit message, or the necessity to rework the changes. This article will guide you through different methods to uncommit the last commit in Git, ensuring you can manage your repository effectively. Need t
    3 min read
    How to Undo a Commit in Git ?
    Git offers several ways to backtrack and correct mistakes, making it a powerful tool for developers. In this article, we will explore various methods to undo a commit in Git, ensuring that you can choose the best approach for your specific system.Below are the approaches to Undo a commit in Git:Tabl
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences