Commit Structure

Asked

Viewed 174 times

2

I wonder if you have how to change a commit structure, in the case before it is made a predetermined structure, example:

GIT_AUTHOR_NAME = '$name(Previamente registrado)'
GIT_AUTHOR_DATE = '$date(data do sistema)'
GIT_COMMITTER_DATE = '$date(data do sistema)'

From this doing a commit would automatically change the time to the system where the repository is and not the source system. It would be possible to pre-determine these things through a pre-commit?

  • 1

    You want to change the commit time on the server or it can be on the developer’s machine itself?

1 answer

2

Git by default saves the Unix timestamp (amount of seconds from 1970) and Timezone (specifically, the commit offset) in which the user is at the moment.

For example, a commit right now would cause Git to save the commit with the following information:

commit c00c61e48a5s69db5ee4976h825b521ha5bx9f5d
Author: Seu Nome <[email protected]>
Date:   Fri Dec 14 09:22:00 2018 -0200 # Horário local e offset. Estou no horário de versão brasileiro, caso contrário seria -0300

Mensagem de commit aqui

What you want is to use the local time and offset from the repository location and not from the user’s machine. So let’s say the server repository is in UTC (offset 0000).

You can even specify the date manually in the commit, using the ISO 8601 format:

git commit --date=2018-12-14T01:00:00+0000

But it is not feasible to do this at each commit. The best way is to do it automatically. So, you can get this current date using the command date in UTC ISO 8601 format and pass to git commit command, this way:

git commit --date="$(date --utc +%Y-%m-%dT%H:%M:%S%z)"

To not need to apply this command every time, you can create an alias for this all commit command and always use it:

git config --global alias.utccommit '!git commit --date="$(date --utc +%Y-%m-%dT%H:%M:%S%z)"'

And to commit:

git utccommit -m "Mensagem de commit com data em UTC, igual do repositório"

Now, if idea is to actually use Git Hook, reading the documentation i did not find an easy way to do it. I understand that it would be possible to use the post-commit, capture the last local commit (the commit that was just made) and change the date using some hook script like this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.