2

When I need to change the commit dates of various commits, I use an interactive rebase and change them one by one.

How could I change them all in a single command ? In other words, I need to apply a given command to all commits that would be listed in an interactive rebase.

Thanks

HagridV
  • 33
  • 5

3 Answers3

3

Filter-Repo

git filter-branch is deprecated. Instead, use git filter-repo. You will need to install it.

Here is an excellent article about how to use git-filter-repo to modify the commit date. The git-filter-repo documentation explains the concept of --commit-callback pretty well.

A very simple example

Let's reset the timezone of all commit dates to zero.

# Save this as ../change_time.py
def handle(commit):
    "Reset the timezone of all commits."
    date_str = commit.author_date.decode('utf-8')
    [seconds, timezone] = date_str.split()
    new_date = f"{seconds} +0000"
    commit.author_date = new_date.encode('utf-8')

handle(commit)

# You need to be in a freshly-cleaned repo. Or use --force.
git clone <...> your_repo
cd your_repo
# First just a dry run.
git filter-repo --dry-run --commit-callback "$(cat ../change_time.py)"
# And now do it for real
git filter-repo --commit-callback "$(cat ../change_time.py)"
Unapiedra
  • 13,615
  • 10
  • 59
  • 86
0

Adapted from https://stackoverflow.com/a/750182/7976758:

#!/bin/sh

git filter-branch --env-filter '
GIT_AUTHOR_DATE="2000-12-21 23:45:00"
GIT_COMMITTER_DATE="`date`" # now
export GIT_AUTHOR_DATE GIT_COMMITTER_DATE
' --tag-name-filter cat -- --branches --tags

See https://git-scm.com/docs/git-filter-branch for reference.

phd
  • 69,888
  • 11
  • 97
  • 133
  • git-filter-branch is deprecated. Prefer `git filter-repo`. – Unapiedra Mar 26 '20 at 18:00
  • Here's what the [official filter-branch docs](https://git-scm.com/docs/git-filter-branch) have to say: "Please use an alternative history filtering tool such as git filter-repo." (2) When running "git filter-branch" (Git 2.25.2), you get a similar warning and recommendation to use git filter-repo. – Unapiedra Mar 27 '20 at 18:03
0

git rebase supports the --exec option, which will do exactly that.

-x <cmd>

--exec <cmd>

Append "exec <cmd>" after each line creating a commit in the final history. <cmd> will be interpreted as one or more shell commands. Any command that fails will interrupt the rebase, with exit code 1.

user3840170
  • 22,750
  • 2
  • 21
  • 50