Git filter branch from HEAD
I recently forked, committed and made a pull request to a public repository where I am not a contributor. My pull request is not yet merged.
Looking through my commits, I realized that I was using the wrong author name and I want to fix it. I tried to do a rebase, which works great:
git rebase -i HEAD~7
Then I have edited
all the commits in question and based on that answer have successfully changed the author of the commits .
git commit --amend --author "James <email@example.com>" --no-edit && \
git rebase --continue
So far so good, but I noticed that this changes the commit time to the current time. I want to keep the previous timestamps. I tried following this answer , but filter-branch
it seems to have changed the hashes for the entire repository.
I probably shouldn't force it. And even if I did, it would probably make it so my pull request could not be merged. So, I tried to find a way to run filter-branch
for the last few commits on the branch, but I couldn't find anything. I tried:
git filter-branch --env-filter 'GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; export GIT_COMMITTER_DATE' HEAD~7
But it doesn't work. It exits with an error message:
What ref do you want to rewrite?
What should I do? Is it possible?
Should I look into the flag --committer-date-is-author-date
for rebase? What does it do?
source to share
I wouldn't bother with that. Note that although the commit date changes, the author's date is preserved.
If you think about it, it makes sense given the semantics of git: whenever the SHA of a commit changes, it is a new commit, and the commit date should change too.
However, the following should work:
$ git rebase --committer-date-is-author-date --onto origin/master master my-feature-branch
The above command will take all commits on my-feature-branch
that are not in master
and reinstall them on top origin/master
. Due to the provided --committer-date-is-author-date
, the author's date will be duplicated on the commit date, but only if you don't use -i
or --interactive
.
This should work even if you've rewritten your local history, although I haven't tried it.
Regarding the rebase command:
Here's a general view:
$ git rebase --onto NEW_BASE OLD_BASE SOME_BRANCH
If SOME_BRANCH
is a branch from OLD_BASE
, this will take all commits between OLD_BASE
and SOME_BRANCH
and apply them to NEW_BASE
instead OLD_BASE
.
Here's a more specific command based on a few links you've provided:
$ git rebase --committer-date-is-author-date --onto origin/master HEAD~7 my-feature-branch
source to share