Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a commit date / time to a file

I have a repository at GitHub and update it with TortoiseGit.

I don't want to create version number on every commit / push. But I would like insert a date / time into the Readme.md file before committing automatically.

Is this possible?

like image 862
Tahtu Avatar asked Feb 12 '17 13:02

Tahtu


People also ask

How do I set commit time?

Just do git commit --amend --reset-author --no-edit . For older commits, you can do an interactive rebase and choose edit for the commit whose date you want to modify.

Does git commit have timestamp?

There are actually two different timestamps recorded by Git for each commit: the author date and the commit date. When the commit is created both the timestamps are set to the current time of the machine where the commit was made.


1 Answers

You can do it in the pre-commit hook placing a below file, modifying the .sample file, located in /.git/hooks/pre-commit.sample. and renaming it to pre-commit.

Something like this

   #!/bin/sh
   #
   # An example hook script to verify what is about to be committed.
   # Called by "git commit" with no arguments
   # blah...
   date >> README.md
   git add README.md
   echo "Updated the time in README"
   exit 0

So every time you make a commit using git commit, README.md file will be update with the time. P.S: You can improve the date command using sed to update the time, the ex. here just updates it every time you commit. Also, this will only work if you're using GIT BASH desktop app.

like image 108
chinmay Avatar answered Oct 17 '22 01:10

chinmay