4


I am quite new with GIT and looking for advice. Accidentally I had set incorrect time and all my commits are with wrong time/date and I would like to shift commit times/dates (f.e +8 hours/ +10 days). I have found solution for one commit but I was wondering whether it can be done for many commits in one branch. I have managed to figure out how to change date but I am lost with rebasing :

COMMITS=($(git rev-list $COM~..HEAD))
for COMMIT in "${COMMITS[@]}"
do
   COMMIT_DATE=$(git log $COMMIT -n1 --format=%aD)
   NEW_DATE=$(date -d "$COMMIT_DATE+30 days" -R)
   echo "I: $COMMIT FROM $COMMIT_DATE TO $NEW_DATE"
   GIT_COMMITTER_DATE="$NEW_DATE" GIT_AUTHOR_DATE="$NEW_DATE" git commit --amend --no-edit --date "NEW_DATE"
   ...... rebase command
done

Can somebody advise me how to correctly rebase?
Thanks in advance

Community
  • 1
  • 1
Kucera.Jan.CZ
  • 714
  • 1
  • 7
  • 19
  • Possible duplicate of [Use 'git filter-branch' to correct committer dates in last N commits?](http://stackoverflow.com/questions/24819850/use-git-filter-branch-to-correct-committer-dates-in-last-n-commits) – Joe Sep 23 '16 at 09:21

2 Answers2

6

Thanks to Joe's hint I was able to write exactly what I wanted, therefore I will post it here for other viewers.

git filter-branch --env-filter '
COMMIT_DATE=$(git log $GIT_COMMIT -n1 --format=%aD);
NEW_DATE=$(date -d "$COMMIT_DATE+1 day" -R);
GIT_COMMITTER_DATE="$NEW_DATE"
export GIT_COMMITTER_DATE
GIT_AUTHOR_DATE="$NEW_DATE"
export GIT_AUTHOR_DATE
' SHA..HEAD
Community
  • 1
  • 1
Kucera.Jan.CZ
  • 714
  • 1
  • 7
  • 19
2

The following will rebase the last 3 commits with the current time and date:

NEW_DATE="$(date -R)"
NUM_COMMITS_TO_REBASE=3
GIT_SEQUENCE_EDITOR=: git rebase -i HEAD~${NUM_COMMITS_TO_REBASE} --exec "git commit --amend --date \"$NEW_DATE\""
  • worked fine for me when executed as a one liner, thanks a lot ! – Brice B Aug 01 '22 at 13:04
  • Is it possible to do this for all commits after a particular commit id rather than the last N commits? (would save counting). – baxx Nov 15 '22 at 10:40
  • Yes, Just replace HEAD~${NUM_COMMITS_TO_REBASE} with the commit id – Alan Hayward Nov 16 '22 at 11:04
  • I added `--no-edit` to git commit, so I don't need to put an action save for each commit. And for my case, I wanted to change the date from specific commit up to the HEAD, so for that, I calculated the distance from the head using `git rev-list COMMIT_HASH.. --count`. – fsevenm Apr 03 '23 at 00:23