简体   繁体   English

删除存储库中最旧的文件

[英]Remove Oldest File in Repository

I've got a git repo, and I have revisions of a file, dated with a unix timestamp. 我有一个git repo,我有一个文件的修订版,其日期为unix时间戳。 I want to get the oldest file (IE the one with the smallest timestamp) and remove it. 我想获取最旧的文件(即时间戳最小的文件)并将其删除。

Is this possible with Bash alone and if so, how? 仅靠Bash是否有可能?如果可以,怎么办?

If you want to find the oldest file in a directory tree, you can do something like this: 如果要在目录树中找到最旧的文件,可以执行以下操作:

ls -tr $(find . -type f) | head -1

This works as long as the number of files isn't too large. 只要文件数量不太大,此方法就起作用。 It's even easier if you're willing to delete "all files older than a certain number of days". 如果您愿意删除“所有早于特定天数的文件”,则更加容易。 For example, if you want to get rid of anything older than 5 days: 例如,如果您想清除5天以上的东西,请执行以下操作:

find . -type f -mtime +5 -print | xargs rm

You wouldn't want to run this verbatim; 您不想逐字逐句地运行; you'd want to provide the appropriate filters to find -- or root it at the approriate directory -- so that you're only deleting files you really want to delete. 您需要提供适当的过滤器以find -或将其根植在适当的目录中-这样您就只删除真正要删除的文件。

Obviously you'll need to commit these deletes to git as well. 显然,您还需要将这些删除也提交给git。 You could do something like this: 你可以这样做:

find . -type f -mtime +5 -print | xargs git rm
git commit -m "deleted things"

...although note that this could commit changes that you've previously staged with "git add". ...尽管请注意,这可能会提交您先前使用“ git add”进行的更改。 Some things are best done by hand. 有些事情最好由手工完成。

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

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