Git: Bulk change of commit dates

Viewed 1043

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

3 Answers

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

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.

Related