问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/28536980/git-change-commit-date-to-author-date