Getting your file in Git back
Lost your changes don’t worry we will get them back
First of all, you need to get your logs checked for that file
1] View logs in Git
Rish@PC MINGW64 ~/Documents/workspace-spring-tool-suite-4–4.16.0.RELEASE/project-management (master)$ git log src/main/resources/templates/projects/new-project.htmlcommit 983de0bd1013128a25de6352409a1a8ada412b54 (HEAD -> master, origin/master)Author: rishab <agsikahs@gmail.com>Date: Sat Oct 1 05:15:00 2022 +0530Changes for Project Management Application
You can also specify a commit hash
(often also called commit ID
) with the git show
command.
In a nutshell
git show <commitHash>:/path/to/file
Let's copy commit hash from above and see what is inside that file
$ git show 983de0bd1013128a25de6352409a1a8ada412b54:src/main/resources/templates/projects/new-project.html<!DOCTYPE html><html xmlns:th=”http://www.thymeleaf.org"><head th:replace=”layouts ::header”></head><body><nav th:replace=”layouts ::foo”></nav><div class=”container”><form action=”/projects/save” th:object=”${project}” method=”POST”><input type=”text” placeholder=”Project Name” th:field=”${project.name}”><select th:field=”${project.stage}”><option th:value=”NOTSTARTED”> Not Started</option><option th:value=”INPROGRESS”> In Progress</option><option th:value=”COMPLETED”> Completed</option></select><textarea type=”text” placeholder=”Enter Project Description” th.field=”${field.description}”></textarea><button type=”submit”>Create Project</button></form>
So my version of the file I can see there associated with that particular commit
or you can also view
Step by step
- Show the log of all the changes for a given file with
git log /path/to/file
- In the list of changes shown, it shows the
commit hash
such ascommit 06c98...
(06c98... being the commit hash) - Copy the
commit hash
- Run the command
git show <commitHash>:/path/to/file
using thecommit hash
of step 3 & thepath/to/file
of step 1.
References from stack overflow
or you can also get your changes back but of 90 period commit by using below command.
2] Git How to Get Changes Back within 90 days period
$ git show HEAD@{2022–10–01}:src/main/resources/templates/projects/new-project.html<!DOCTYPE html><html xmlns:th=”http://www.thymeleaf.org"><head th:replace=”layouts ::header”></head><body><nav th:replace=”layouts ::foo”></nav><div class=”container”><form action=”/projects/save” th:object=”${project}” method=”POST”><input type=”text” placeholder=”Project Name” th:field=”${project.name}”><select th:field=”${project.stage}”><option th:value=”NOTSTARTED”> Not Started</option><option th:value=”INPROGRESS”> In Progress</option><option th:value=”COMPLETED”> Completed</option></select><textarea type=”text” placeholder=”Enter Project Description” th.field=”${field.description}”></textarea><button type=”submit”>Create Project</button></form>
Thank you, Let me know if any corrections are required!