Git: Bulk change of commit dates

自作多情 提交于 2020-04-16 08:33:48

问题


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


回答1:


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.




回答2:


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.




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


来源:https://stackoverflow.com/questions/60863773/git-bulk-change-of-commit-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!