GIT: change commit date to author date

Is it possible to change the commit date from my commit to the author date?

I adapted some commits and now the dates are all the same. I want to set it back to the old dates (or the author dates). Is this possible?

I am using Sourcetree so I have the git command line but I am not a pro at that. My external repository is bitbucket.


Solution 1:

Short Answer:

git filter-branch --env-filter 'export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"'

Explanation:

filter-branch lets you rewrite your git history. It can apply transformations to each commit or filter out commits based on certain criteria. See git filter-branch --help for a comprehensive description and usage instructions.

--env-filter allows you to set the environment variables that are present during the creation of the new history. It is evaluated for each commit separately.

Solution 2:

Since git 1.6.3 git rebase has --committer-date-is-author-date for this purpose.

git rebase --committer-date-is-author-date

Original answer:
There's no easy way to set the committer dates (edit: but see "edit 2" below). The author dates are easy to adjust (at commit time) since --date will let you specify each one as you go.

The environment variable GIT_COMMITTER_DATE can be used to force a different time stamp at the time you make the commit. Note, however, that you'd need to adjust this for each commit you "replay". The resulting new commit will have a different SHA-1 (because you've changed some bits in it, namely, the committer date field), which means you must redo all its descendent commits.

This is what git filter-branch does (re-create some, many, or all commits with changes made along the way, keeping a mapping from old SHA-1 IDs to new SHA-1 IDs and adjusting the parents of even-otherwise-untouched commit copies so that the "new" DAG of new SHA-1 IDs matches the "old" DAG in every possible way, i.e., in every way except for SHA-1 IDs and any other changes made by your filter(s)).

In other words, to do this, you must use git filter-branch to rewrite history, with all that this implies. [Edit: you can literally do it without git filter-branch, e.g., by doing it in git rebase -i instead, but the effect is the same.]

Edit 2: as eis noted in a comment (since removed), git rebase has --committer-date-is-author-date for this purpose. It still does the same history rewriting, but it's a lot more convenient than doing it with the raw git filter-branch command.

Solution 3:

I wanted to piggyback on the answer from jsphpl because I was looking for a way to do this, but with two differences:

  • I only wanted to change the commits in my current branch
  • I wanted the rewritten commits to be signed

First you will need the ID of the first commit in your branch:

git log master..your-branch-name --pretty="%H" | tail -1

The output of that will look something like this: c04b195ca1ed4c8a18c6a98d67b8a764a3006afc

Take that ID and plug it into the command below:

git filter-branch -f \
--env-filter 'export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"' \
--commit-filter 'git commit-tree -S "$@";' \
c04b195ca1ed4c8a18c6a98d67b8a764a3006afc^...your-branch-name

The commit date and author date of each commit should now match. You can double check by running the command below to easily view and compare the commit date values:

git log --format="%H %cI %aI %s" master...your-branch-name