Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for "lost" file/changes in Git?

Tags:

git

I seem to have messed up my local repository.

I know I had some content in a specific file that I worked on a few weeks ago. After multiple merges, commit squashing and other types of history rewrites I now noticed that this content is missing. I also can't find the commit where I added these changes any more.

I read before that there are local copies of "orphaned" branches and commits. Is there a way to search these for specific files or keywords somehow?

like image 337
janpio Avatar asked Sep 27 '17 16:09

janpio


People also ask

How do I find file change history?

Right-click a file or folder in the project and click Show History. In the Change Explorer view, open a change set, right-click a file or folder in the change set, and select Show History.

How do you see what files were changed in git?

Find what file changed in a commit To find out which files changed in a given commit, use the git log --raw command.

How do you check the history of a file in git?

Git file History provides information about the commit history associated with a file. To use it: Go to your project's Repository > Files. In the upper right corner, select History.


2 Answers

The git fsck command can be used to list out commits that are either --unreachable or --lost-found.

As the output of this can be hard to read at time, I suggest giving the found hashes to git log so that they are displayed in reference to the rest of your commits.

git log --graph --all --numstat --oneline --decorate --full-history --date-order --color <Put Refs Here>

The --numstat command will show you which files were changed in each commit, and hopefully help you find the file/data.

like image 115
unDeadHerbs Avatar answered Oct 21 '22 09:10

unDeadHerbs


Thanks to some comments on my questions I knew that reflog was one of possible command to look for these commits.

I usually use a GUI to use Git (SourceTree or GitKraken) so I was a bit lost on the command line. Theoretically, I knew how to find these files: git reflog, then probably git show to get the changes files and the diff to sift through all of them (I'm sure there is a better way to do this, but I have no experience here with the CLI). But as I also had no idea when in the last month I made these changes and where they got lost, this didn't look like my solution.


After some googling I found SmartGit. It's another git GUI, but it actually supports showing these lost commits and their context. Here are my instruction I wrote down in a blog post:

We are looking for the „Recyclable Commits“ branch in the „Log“ view: Open the repository in SmartGit, right click on the repository in the „Repositories“ panel, select „Log“ in the context menu, then in the opened „Log“ view, select „Recyclable Commits“ in the „Branches“ panel additionally to the already selected branch or „HEAD“:

example of "recaclyble commits" output

The light orange-dot entries (vs. the bold black-dot ones) are the „orphans“ that are not part of the current tree any more.

You can filter these down by using a „Filter“ input, which optionally can search for File name and even File content! (So I could just type the filename I knew I had put my content into, and look only through these relevant commits – and even search for a few words I knew I wrote about in the file – how awesome is that!)

Important and super useful is that these commits are shown in the context of the current tree, so you can click the individual commits and see the file changes it includes, click the files to see the diff as you normally would. So much easier than on the command line.

After you found the missing commit, either copy the text over to your editor manually or cherry pick that commit with all its changes to your current branch by right clicking on it and selecting „Cherry-Pick…“. Then commit as usual (and dance a bit as you just saved a few days of work).

Commits found, content restored.

like image 20
janpio Avatar answered Oct 21 '22 07:10

janpio