Reputation: 39456
I often work on a number of different files over some time before I get around to committing that work. The work should then often go into multiple commits, not only one.
It would be nice if those commits would not show the current time, but rather a reasonable approximation of when I actually finished the work that is in them.
One approach for getting such an approximation would be to use the mtime
file timestamp of the youngest file that goes into the commit.
Assume I have worked on files a
, b
, c
, d
.
a
and b
should go into commit 1,
c
d
into commit 2.
I would like to do something like this:
git add a b
git commit --date=files -m"modified a and b"
and have git use the timestamp of either a
or b
for the commit, whichever is younger.
As far as I can see, there is no support for this in git. Does anybody know why? It sounds like a useful functionality to me.
Can anybody recommend a helper program that would do this?
(For the use as sketched above, this is easy, but when further changes are done to a
or b
after the git add
, it becomes a lot more difficult.)
Upvotes: 2
Views: 471
Reputation: 98015
There are two different scenarios you talk about here, one of which is fairly easy to implement yourself, and the other is basically impossible, but also easily avoided.
Scenario 1 is that you've made some changes to files a
and b
, but forgotten to commit, then come back after editing other files, and want to record when you actually did the work. As you say, this is relatively straight-forward to write a helper for. For instance using stat -c '%Y' filename
will give a Unix timestamp, which can be sorted and passed to git commit
:
git commit --date="$(stat -c '%Y' file1 file2 | sort -n | tail -n1)"
Scenario 2 is that you've made the changes, run git add
, and then carried on working on the same files without committing. Since a git tree (including the Staging area) doesn't store modification timestamps, there is no way that any tool can answer the question "what was this file's timestamp when I ran git add
?"
However, what does have a timestamp is a git commit, and commits are cheap, so you just need to adjust your habits so that after running git add
, you run something like:
git commit -m WIP
Now you have a snapshot of the files, with a timestamp against it! Later, you can run git rebase -i
and for each "WIP" commit, set it to either:
Upvotes: 4