如何使用git rebase同时编辑作者/提交者和时间戳?

huangapple go评论90阅读模式
英文:

How to use git rebase to edit both author/committer and timestamps at the same time?

问题

我一直在尝试重写Git仓库的历史,以整合提交、删除大文件和许多其他更改。git filter-branch 不被推荐作为工具,我想手动重写历史以获得更精确的控制。

我遇到了一个重大障碍。当我重写提交时,重写的提交显示为由我提交(作者编写并由我提交),而我不想要这样。

我可以通过在每次rebase之前运行git config user.namegit config user.email来绕过这个问题。这使它看起来像是原始作者提交的。

到目前为止还好,但是当我这样做时,提交者的时间戳总是“现在”。我可以使用git commit --amend --date更改作者时间戳,但提交者时间戳无法更改。

我可以使用选项--committer-date-is-author-date,这样可以工作(尽管会更改时区,这不是我喜欢的)……但似乎在交互式rebase中无效,它什么也不做。自动rebase不允许我使用git config user.name/email使提交的原始作者独立显示。

我尝试使用set GIT_COMMITTER_DATE=后跟git commit --amend --no-edit。一开始它是有效的,但突然间出于未知的原因停止工作。现在它根本不起作用 - 提交者的时间戳不受影响。

这让我面临一个选择,要么将我添加为提交者,要么在具有多年历史的项目中具有不准确的“现在”时间戳。

我正在寻找一种方法,使查看历史记录的人可以看到原始作者和原始时间戳(或我指定的任何时间戳),并且与交互式rebase兼容,因为我将进行大量的rebase,因此整个历史记录将从过去越来越远的地方重写。

是的,这可能是一个疯狂的项目,但是否有人有关于如何实现这一目标的建议?

(是的,我正在一个单独的分支上进行此操作。在完成项目后,我将强制推送到主分支。)

编辑:当我尝试在rebase的一部分设置环境变量时会发生以下情况:https://gyazo.com/d6189169809023e2b722fedc03169011

(使用以下输入:https://gyazo.com/8a38c243dac215c4a333394ef8fae78e)

我可以使用CMD设置环境变量,但Git无法做到:https://gyazo.com/39644a586b78cb933988e90880c67286

我正在使用Windows 10 Home Edition。

英文:

I've been trying to rewrite the history of a Git repository to consolidate commits, remove large files, and many other changes. git filter-branch is not recommended as a tool and I want the precision of rewriting the history manually.

I've run into a major obstacle. When I rewrite the commits, the rewritten commits show up as being committed by me (author authored and I committed), which I don't want.

I am able to get around this by running git config user.name and git config user.email before picking each commit in rebase. This makes it look like the original author committed it.

So far so good, but when I do this the committer timestamp is invariably "now". I can change the author timestamp with git commit --amend --date, but the committer timestamp is unchangeable.

I can use the option --committer-date-is-author-date, and that works (although it changes the timezone, which I don't like)...but seemingly not with an interactive rebase, where it does nothing. The automatic rebase does not permit me to use git config user.name/email to make the original author of the commit show up by themselves.

I have tried using set GIT_COMMITTER_DATE= followed by git commit --amend --no-edit. It was working at first, but then suddenly stopped working for an unknown reason. It simply does nothing now - the committer timestamp isn't affected.

This leaves me with a choice of having me added to the commits as the committer, or having an inaccurate timestamp of "now" in a project with years of history.

I'm looking for a way so that people looking at the history can see the original author and the original timestamp (or whatever ones I specify), and that is compatible with interactive rebase because I'm going to be doing a lot of rebasing, so the entire history will be rewritten from further and further in the past.

Yeah, this might be a crazy project, but does anyone have any suggestions on how to accomplish this?

(Yes, I am doing this on a separate branch. I will force push to the main branch after I have finished the project.)

EDIT: This is what happens when I try to set the environment variables as part of the rebase: https://gyazo.com/d6189169809023e2b722fedc03169011

(Using the following input: https://gyazo.com/8a38c243dac215c4a333394ef8fae78e)

I can set the environment variables using CMD, but Git can't do it: https://gyazo.com/39644a586b78cb933988e90880c67286

I am using Windows 10 Home Edition.

答案1

得分: 1

根据文档git commit 识别以下环境变量:

GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL
GIT_AUTHOR_DATE
GIT_COMMITTER_NAME
GIT_COMMITTER_EMAIL
GIT_COMMITTER_DATE

它们的值优先于配置值。

所以在你的情况下,你需要设置 GIT_COMMITTER_* 变量。

你可以使用以下命令检查 git 是否识别这些变量:

git var GIT_COMMITTER_IDENT

如果它没有识别它们,请确保你确实设置了它们。设置和读取环境变量取决于操作系统,并可能会有一些技巧。

英文:

According to the documentation git commit recognizes the following environment variables

GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL
GIT_AUTHOR_DATE
GIT_COMMITTER_NAME
GIT_COMMITTER_EMAIL
GIT_COMMITTER_DATE

Their values take precedence over the configuration values.

So in your case you need to set the GIT_COMMITTER_* variables.

You can check if git recognizes the variables by using the command

git var GIT_COMMITTER_IDENT

If it does not recognize them, make sure you are actually setting them. Setting and reading environment variables is OS dependent and can be tricky.

huangapple
  • 本文由 发表于 2023年8月5日 04:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定