2


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
  • 674
  • 1
  • 7
  • 18
  • 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

4

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
  • 674
  • 1
  • 7
  • 18
0

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\""