1

I have a codebase containing close to a thousand files. I'm working in a local branch with uncommitted changes. At one point earlier today, I added an error_log statement to one file that returned a very useful piece of info. I thought I was done with it so removed the log, returning the file to its "unchanged" state as far as git is concerned. I need to put that log back in, but can't for the life of me remember which file or function it was part of.

Is there any git command that can show me a list of files that have had changes made to them, even if the changes have been removed?

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
EmmyS
  • 11,892
  • 48
  • 101
  • 156
  • possible duplicate of [Git recover uncommitted changes](http://stackoverflow.com/questions/3240436/git-recover-uncommitted-changes). Different way of getting there, but unfortunately git can't tell you that. [Commit often](http://en.wikibooks.org/wiki/Commit_Often,_Perfect_Later,_Publish_Once:_Git_Best_Practices/Commiting_early_and_often)! – cmbuckley Sep 26 '13 at 20:19

4 Answers4

4

I don't think there is a way to do it in git, but I would list the files recursively in the order that they were modified. So in linux you should be able to do something like

ls -ltrR

You might be able to find the file this way. Which is better than nothing.

Johnny Z
  • 14,329
  • 4
  • 28
  • 35
  • Thanks. Unfortunately we're on Windows, not Linux. And the files aren't in a single folder; there are a ton of subfolders, so I can't just sort the entire codebase by modification date. – EmmyS Sep 26 '13 at 20:36
  • 1
    @EmmyS This approach should work just fine from Windows too. Windows Explorer lets you search for files, including subdirectories, and gives you one big list of everything it finds. In that window, you should still be able to sort by modification time. –  Sep 26 '13 at 20:44
0

Expanding on Johnny's answer, and borrowing from How to recursively find and list the latest modified files in a directory with subdirectories and times?:

find . -path './.git' -prune -o -type f -print \
    -exec stat --format '%Y :%y %n' {} \; | sort -nr | cut -d: -f2- | head

When run from the repo root, this will show you the most recently modified files.

Community
  • 1
  • 1
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
0

If at any point in time you committed the error_log line to your repo, then that line's existence still exists in your history, and you can recover it in several different ways. The most straight forward is to simply use patch output in the git log, and assuming that the output is being passed to the less pager, you then simply hit / and enter a regex for that line:

git log -p
# While in `less`
/error_log

You can then search the output for the regex by hitting n to advance to the next hit, or N to go back to the last one.

Another way to find the existence of the line (assuming it was committed) is to use the -S or -G option to git log. The former searches for the addition or deletion of text using a fixed string, while the latter also uses regexes:

# Fixed string
git log --name-status -S "error_log"

# Regex
git log --name-status -G "error_log"

The output will be a list of files that have either had the string error_log added or removed from them at some point in time.

-1

Uncommitted changes are always bad. You should always work in small steps, and commit after you finished each step. - Commits are only local and can later be squashed without any problems.

With small committed steps you can always go back to each step. - Without any commits git cannot help you in recovering changes.

Therefore you have to look outside of git. Like timestamps of the file system or maybe some logs of your editor.

michas
  • 25,361
  • 15
  • 76
  • 121
  • Thanks for the lecture. That doesn't actually answer the question, and it's not an issue. My commits are fine. I don't see the point in committing when I haven't solved anything. – EmmyS Sep 26 '13 at 20:35
  • You asked for a git command. - The home truth is: There is none. (git just does not know anything about your changes unless you commit them.) – michas Sep 26 '13 at 20:41
  • Then all you had to do was add a comment saying that there is no git command. Your "answer" is not an answer. – EmmyS Sep 26 '13 at 20:48