Jake
Jake

Reputation: 379

Find last edited file in git repo

Is there a way (possibly with git) to get the last edited file in the git repo?

Example: I have a bunch of uncommitted changes from Friday when I left at 4:59pm to go spend the weekend on the lake and don't remember exactly where I left off.

Upvotes: 0

Views: 41

Answers (2)

Edward Thomson
Edward Thomson

Reputation: 78673

It sounds like you want to simply find the newest file in your project, which is not something that Git tracks directly. As far as Git is concerned, you have some staged changes, and some unstaged changes, but it has no idea of recency.

Thankfully, the filesystem does. And really this is simply a question of which file is the newest, but we might be able to improve on that answer using some knowledge of Git (and also making it cross-platform - the Mac OS X version of find does not have a -printf option).

If you have not yet staged your changes, then they must be newer than the Git index (which stores the staged file information), so we can find all files newer with:

find . -type f -newer .git/index

And we can query the file time using the stat command:

find . -type f -newer .git/index -exec stat -f '%c %N' {} \;

And finally we want to sort the output (to get the newest first) and limit it to a single file:

 find . -type f -newer .git/index -exec stat -f '%c %N' {} \; | sort -r | head -1

Here I've changed a few files in my repository:

% git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   unstaged_edit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    unstaged_add

no changes added to commit (use "git add" and/or "git commit -a")

And if I want to find the newest:

% find . -type f -newer .git/index -exec stat -f '%c %N' {} \; | sort -r | head -1
1466437626 ./staged_edit

Upvotes: 1

Briana Swift
Briana Swift

Reputation: 1217

I don't know of a way to find the last edited file, but if you changed your files and saved them, you should be able to see the current state of where you left off with git status.

By default, it will show you what has been changed but not added (working directory) and what has been added but not committed (staging area).

Upvotes: 0

Related Questions