编程

在 Git 中使用对人类友好的日期格式

48 2024-10-20 15:23:00

Git 是当今版本控制的事实标准。毫无疑问,它是一个强大的工具,可以帮助你以及管理代码并与他人协作。

Git 提供了许多绝妙的功能,可以帮助你更好地理解代码历史。但最近,我偶然发现了一个名为“相对日期”的功能,它可以用来以一种对人类友好的方式指定日期,从而从  Git 历史中提取各种信息。

比如,git show 命令可用于显示提交或文件特定版本的日志消息和文本差异。

git show <commit-hash>

以下是如何使用相对日期格式以对人类友好的方式查看存储库在特定时间点的状态

比如,以下是我们如何查看一周前存储库的状态。

git show master@{1.week.ago}
# or
git show master@{"1 week ago"}

如你所见,如果一周前提交的存在,此命令将显示该提交的日志(log)和差异(diff)。

同样,我们可以以对人类友好的方式指定日期范围。

# List log of all commits from 
# one month ago to the current date
git log @{1.month.ago}..HEAD

此命令将显示从一个月前到当前日期的所有提交日志。

或者说查看你在过去的一个月里做了什么修改

# Changes made in the last one month
git diff @{1.month.ago}

我们还可以切换(checkout)到特定时间点的某个特定分行。

We can also checkout a specific branch as it was at a specific point in time.

# Checkout the master branch as 
# it was at three week ago
git checkout master@{3.week.ago}

此命令将像三周前一样切换到 master 分支。

还支持其他指定日期的格式,如 @{yesterday}@{2024-08-17}@{"1 month 5 days ago"} 等。

 

下一篇