Getting your file in Git back

Lost your changes don’t worry we will get them back

Rishab Batra
2 min readOct 4, 2022

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

  1. Show the log of all the changes for a given file with git log /path/to/file
  2. In the list of changes shown, it shows the commit hash such as commit 06c98... (06c98... being the commit hash)
  3. Copy the commit hash
  4. Run the command git show <commitHash>:/path/to/file using the commit hashof step 3 & the path/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!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Rishab Batra
Rishab Batra

No responses yet

Write a response