Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 389
kevingoos Avatar asked Feb 16 '15 08:02

kevingoos


People also ask

Can you change the date of a git commit?

Changing Git DatesYou can change the author date of a given commit by passing the --date flag to git commit .

Does git commit amend change author?

Using --amend for the Very Last CommitThis effectively replaces the last commit with your "edited" version, correcting the wrong author information.


2 Answers

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.

like image 138
jsphpl Avatar answered Oct 15 '22 12:10

jsphpl


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.

like image 34
torek Avatar answered Oct 15 '22 10:10

torek