简体   繁体   English

在git repo中查找最后编辑的文件

[英]Find last edited file in git repo

Is there a way (possibly with git) to get the last edited file in the git repo? 有没有办法(可能使用git)在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. 例如:从周五下午4:59离开去湖边度过一个周末以来,我有很多未提交的更改,不记得确切的离开时间。

It sounds like you want to simply find the newest file in your project, which is not something that Git tracks directly. 听起来您只是想在项目中找到最新文件,而Git并没有直接跟踪它。 As far as Git is concerned, you have some staged changes, and some unstaged changes, but it has no idea of recency. 就Git而言,您有一些阶段性的更改,也有一些未阶段性的更改,但是它不了解新近度。

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). 而实际上这是一个简单的问题的哪个文件是最新的我们也许能够提高使用Git的一些知识这个问题的答案(也使其成为跨平台-的Mac OS X版本find没有一个-printf选项)。

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: 如果您尚未暂存您的更改,那么它们必须比Git索引(用于存储暂存的文件信息)新,因此我们可以使用以下命令找到所有较新的文件:

find . -type f -newer .git/index

And we can query the file time using the stat command: 我们可以使用stat命令查询文件时间:

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

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 . 我不知道找到最后编辑的文件的方法,但是如果您更改了文件并保存了它们,则应该可以使用git status来查看上次中断的当前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). 默认情况下,它将向您显示已更改但未添加的内容(工作目录)以及已添加但未提交的内容(暂存区)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM