Using Human-friendly dates in Git
Git is a de-facto standard for version control these days. And there’s no doubt that it’s a powerful tool that can help you manage your code and collaborate with others.
Git provides many nifty features that can help you better understand your code history. But recently, I’ve stumbled upon a feature called “relative date” that can be used to specify dates in a human-friendly way to extract various information from the Git history.
For instance, the git show
command can be used to show the log message and textual diff of a commit or a specific version of a file.
git show <commit-hash>
Here’s how the relative date format can be used to see the state of the repository at a specific point in time in a human-friendly way.
For instance, here’s how we can see the state of the repository one week ago.
git show master@{1.week.ago}
# or
git show master@{"1 week ago"}
As you can tell, this command will show the log and diff of the commit made one week ago if that commit exists.
Similarly, we can specify a date range in a human-friendly way.
# List log of all commits from
# one month ago to the current date
git log @{1.month.ago}..HEAD
This command will show the log of all commits from one month ago to the current date.
Or see what changes you’ve made in the last one month.
# Changes made in the last one month
git diff @{1.month.ago}
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}
This command will checkout the master branch as it was three week ago.
Other formats for specifying dates are also supported such as @{yesterday}
, @{2024-08-17}
, @{"1 month 5 days ago"}
, etc.
Like this article?
Buy me a coffee👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.