如何在过去进行 Git 提交?
我正在将所有内容转换为 Git 供我个人使用,并且我发现存储库中已经存在一些旧版本的文件。如何根据文件的“修改日期”以正确的顺序将其提交到历史记录,以便我拥有文件的准确历史记录?
有人告诉我这样的事情会起作用:
git filter-branch --env-filter="GIT_AUTHOR_DATE=... --index-filter "git commit path/to/file --date " --tag-name-filter cat -- --all
I'm converting everything over to Git for my own personal use and I found some old versions of a file already in the repository. How do I commit it to the history in the correct order according the file's "date modified" so I have an accurate history of the file?
I was told something like this would work:
git filter-branch --env-filter="GIT_AUTHOR_DATE=... --index-filter "git commit path/to/file --date " --tag-name-filter cat -- --all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
这对我有用:
This is what worked for me:
你得到的建议是有缺陷的。在
--env-filter
中无条件设置 GIT_AUTHOR_DATE 将重写每次提交的日期。另外,在--index-filter
中使用 git commit 是不寻常的。您在这里正在处理多个独立的问题。
指定“现在”以外的日期
每个提交都有两个日期:作者日期和提交者日期。您可以通过环境变量 GIT_AUTHOR_DATE 和 GIT_COMMITTER_DATE 为写入新提交的任何命令提供值来覆盖每个命令。请参阅git-commit(1) 中的“日期格式”或以下内容:
在正常使用期间写入新提交的唯一命令是git commit。它还具有一个
--date
选项,可让您直接指定作者日期。您预期的用法包括git filter-branch --env-filter
还使用上面提到的环境变量(这些是“env”的一部分,选项以该选项命名;请参阅 git-filter-branch(1) 中的“选项” 和底层“管道”命令 git-commit-tree(1).将文件插入到单个引用中 历史
如果您的存储库非常简单(即您只有一个分支,没有标签),那么您可能可以使用git rebase来完成这项工作,
在以下命令中,使用提交的对象名称(SHA-1 哈希)而不是“A”。
运行git commit时,不要忘记使用“日期覆盖”方法之一。
如果您想更新 A 以包含新文件(而不是在添加文件的地方创建新提交),请使用 git commit --amend 代替 git commit 。结果将如下所示:
只要您可以命名应该是新提交的父级的提交,上面的代码就可以工作。如果您确实希望通过新的根提交(无父级)添加新文件,那么您需要一些不同的东西:
git checkout --orphan
相对较新(Git 1.7.2),但是还有其他方法可以完成同样的事情,这些方法可以在旧版本的 Git 上运行。将文件插入到多引用历史记录中
如果您的存储库比较复杂(即它有多个引用(分支、标签等)),那么您可能需要使用 git 过滤分支。 在使用git filter-branch之前,您应该制作整个存储库的备份副本。整个工作树的简单tar存档(包括.git 目录)就足够了。 git filter-branch 确实会备份引用,但通常更容易从不完全正确的过滤中恢复,只需删除
.git
目录并从您的备份。注意:下面的示例使用较低级别的命令
git update-index --add
而不是git add
。您可以使用git add,但您首先需要将文件从某个外部位置复制到预期路径(--index-filter
在临时GIT_WORK_TREE中运行其命令那是空的)。如果您希望将新文件添加到每个现有提交中,那么您可以这样做:
我真的没有看到任何理由使用
--env-filter 'GIT_AUTHOR_DATE=...'< 更改现有提交的日期/代码>。如果您确实使用了它,则需要将其设置为有条件的,以便它会重写每次提交的日期。
如果您希望新文件仅出现在某些现有提交(“A”)之后的提交中,那么您可以执行以下操作:
如果您希望通过要插入到您的文件中间的新提交来添加文件历史记录,那么您需要在使用git filter-branch之前生成新的提交,并将
--parent-filter
添加到git filter-branch >:您还可以安排文件首先添加到新的根提交中:通过 git rebase 部分中的“orphan”方法创建新的根提交(在
new_commit 中捕获它)
),使用无条件--index-filter
和--parent-filter
如"sed -e \"s/^ $/-p $new_commit/\""
.The advice you were given is flawed. Unconditionally setting GIT_AUTHOR_DATE in an
--env-filter
would rewrite the date of every commit. Also, it would be unusual to use git commit inside--index-filter
.You are dealing with multiple, independent problems here.
Specifying Dates Other Than “now”
Each commit has two dates: the author date and the committer date. You can override each by supplying values through the environment variables GIT_AUTHOR_DATE and GIT_COMMITTER_DATE for any command that writes a new commit. See “Date Formats” in git-commit(1) or the below:
The only command that writes a new commit during normal use is git commit. It also has a
--date
option that lets you directly specify the author date. Your anticipated usage includesgit filter-branch --env-filter
also uses the environment variables mentioned above (these are part of the “env” after which the option is named; see “Options” in git-filter-branch(1) and the underlying “plumbing” command git-commit-tree(1).Inserting a File Into a Single ref History
If your repository is very simple (i.e. you only have a single branch, no tags), then you can probably use git rebase to do the work.
In the following commands, use the object name (SHA-1 hash) of the commit instead of “A”.
Do not forget to use one of the “date override” methods when you run git commit.
If you wanted to update A to include the new file (instead of creating a new commit where it was added), then use
git commit --amend
instead ofgit commit
. The result would look like this:The above works as long as you can name the commit that should be the parent of your new commit. If you actually want your new file to be added via a new root commit (no parents), then you need something a bit different:
git checkout --orphan
is relatively new (Git 1.7.2), but there are other ways of doing the same thing that work on older versions of Git.Inserting a File Into a Multi-ref History
If your repository is more complex (i.e. it has more than one ref (branches, tags, etc.)), then you will probably need to use git filter-branch. Before using git filter-branch, you should make a backup copy of your entire repository. A simple tar archive of your entire working tree (including the .git directory) is sufficient. git filter-branch does make backup refs, but it is often easier to recover from a not-quite-right filtering by just deleting your
.git
directory and restoring it from your backup.Note: The examples below use the lower-level command
git update-index --add
instead ofgit add
. You could use git add, but you would first need to copy the file from some external location to the expected path (--index-filter
runs its command in a temporary GIT_WORK_TREE that is empty).If you want your new file to be added to every existing commit, then you can do this:
I do not really see any reason to change the dates of the existing commits with
--env-filter 'GIT_AUTHOR_DATE=…'
. If you did use it, you would have make it conditional so that it would rewrite the date for every commit.If you want your new file to appear only in the commits after some existing commit (“A”), then you can do this:
If you want the file to be added via a new commit that is to be inserted into the middle of your history, then you will need to generate the new commit prior to using git filter-branch and add
--parent-filter
to git filter-branch:You could also arrange for the file to be first added in a new root commit: create your new root commit via the “orphan” method from the git rebase section (capture it in
new_commit
), use the unconditional--index-filter
, and a--parent-filter
like"sed -e \"s/^$/-p $new_commit/\""
.您可以照常创建提交,但在提交时,请将环境变量
GIT_AUTHOR_DATE
和GIT_COMMITTER_DATE
设置为适当的日期时间。当然,这将使提交发生在分支的顶端(即当前 HEAD 提交之前)。如果你想在仓库中将其向后推得更远,你必须变得有点花哨。假设您有这样的历史记录:
并且您希望新提交(标记为“X”)出现在第二:
最简单的方法是从第一个提交分支,添加新提交,然后变基所有其他提交均位于新提交之上。就像这样:
You can create the commit as usual, but when you commit, set the environment variables
GIT_AUTHOR_DATE
andGIT_COMMITTER_DATE
to the appropriate datetimes.Of course, this will make the commit at the tip of your branch (i.e., in front of the current HEAD commit). If you want to push it back farther in the repo, you have to get a bit fancy. Let's say you have this history:
And you want your new commit (marked as "X") to appear second:
The easiest way would be to branch from the first commit, add your new commit, then rebase all other commits on top of the new one. Like so:
这是一个老问题,但我最近偶然发现了它。
所以它看起来像这样:
git commit --date='2021-01-01 12:12:00' -m "message"
工作正常,并在GitHub
和GitLab< /代码>。
This is an old question but I recently stumbled upon it.
So it would look something like this:
git commit --date='2021-01-01 12:12:00' -m "message"
worked properly and verified it onGitHub
andGitLab
.要使提交看起来像是过去完成的,您必须设置
GIT_AUTHOR_DATE
和GIT_COMMITTER_DATE
:其中
date -d'...' 可以是精确日期,如
2019-01-01 12:00:00
或相对日期,如5 个月前 24 天前
。要查看 git log 中的两个日期,请使用:
这也适用于合并提交:
To make a commit that looks like it was done in the past you have to set both
GIT_AUTHOR_DATE
andGIT_COMMITTER_DATE
:where
date -d'...'
can be exact date like2019-01-01 12:00:00
or relative like5 months ago 24 days ago
.To see both dates in git log use:
This also works for merge commits:
以下是我用来将
foo
上的更改提交到过去N=1
天的内容:如果您想提交到更早的日期,例如 3 天前,只需更改
date
参数:date -v-3d
。例如,当您昨天忘记提交某件事时,这非常有用。
更新:
--date
还接受诸如--date "3天前"
或什至--date "yesterday"<之类的表达式/代码>。所以我们可以将其简化为一行命令:
The following is what I use to commit changes on
foo
toN=1
days in the past:If you want to commit to a even older date, say 3 days back, just change the
date
argument:date -v-3d
.That's really useful when you forget to commit something yesterday, for instance.
UPDATE:
--date
also accepts expressions like--date "3 days ago"
or even--date "yesterday"
. So we can reduce it to one line command:就我而言,随着时间的推移,我保存了许多版本的 myfile 作为 myfile_bak、myfile_old、myfile_2010、backups/myfile 等。我想使用它们的修改日期将 myfile 的历史记录放入 git 中。因此,将最旧的文件重命名为 myfile,
git add myfile
,然后git commit --date=(ls -l 的修改日期) myfile
,将下一个最旧的文件重命名为 myfile,另一个 git使用 --date 提交,重复...要自动化此操作,您可以使用 shell-foo 来获取文件的修改时间。我从
ls -l
和cut
开始,但 stat(1) 更直接In my case over time I had saved a bunch of versions of myfile as myfile_bak, myfile_old, myfile_2010, backups/myfile etc. I wanted to put myfile's history in git using their modification dates. So rename the oldest to myfile,
git add myfile
, thengit commit --date=(modification date from ls -l) myfile
, rename next oldest to myfile, another git commit with --date, repeat...To automate this somewhat, you can use shell-foo to get the modification time of the file. I started with
ls -l
andcut
, but stat(1) is more direct您正在寻找的简单答案:
注意时区字符串并为您的时区设置合适的字符串。即+0200、-0400
The simple answer you are looking for:
Mind the timezone string and set a proper one for your timezone. i.e. +0200, -0400
就我而言,在使用 --date 选项时,我的 git 进程崩溃了。也许我做了一些可怕的事情。结果出现了一些index.lock文件。因此,我手动从 .git 文件夹中删除了 .lock 文件并执行,以便所有修改的文件在过去的日期内提交,并且这次有效。感谢这里的所有答案。
In my case, while using the --date option, my git process crashed. May be I did something terrible. And as a result some index.lock file appeared. So I manually deleted the .lock files from .git folder and executed, for all modified files to be commited in passed dates and it worked this time. Thanx for all the answers here.
git --date
仅更改GIT_AUTHOR_DATE
,但许多 git 应用程序(例如 GitHub)显示GIT_COMMITTER_DATE
。请确保也更改GIT_COMMITTER_DATE
。OS X 中的完整示例(将
GIT_COMMITTER_DATE
和GIT_AUTHOR_DATE
更改为 4 小时前):git --date
changes onlyGIT_AUTHOR_DATE
but many git apps, e.g., GitHub showsGIT_COMMITTER_DATE
. Make sure to changeGIT_COMMITTER_DATE
too.Complete example in OS X (Change both
GIT_COMMITTER_DATE
andGIT_AUTHOR_DATE
to 4 hours ago):或者只是使用 fake-git-history 为特定数据范围生成它。
Or just use a fake-git-history to generate it for a specific data range.
因此,如果您想在过去的日期在 Git 上提交某些内容,您只需使用这些命令即可帮助您完成此操作。
确保您必须根据您的喜好更改日期和时间。这将创建对过去特定日期的提交,并且您不会丢失 GitHub 记录。
So if you want to commit something on Git in the past date, you simply use these commands that help you to do so.
git add .
git commit -m "Your commit message"
.git commit --amend --no-edit --date="Sat Jun 5 20:00:00 2021 -0600"
command after a commit to amend the last commit with the timestamp noted. The --no-edit will leave the message as-is.Make sure you have to change the date and time according to your preference. This will create a commit to the particular date in the past and you do not lose your GitHub streak.
前期步骤。
将所有数据从远程拉取到本地存储库。
我们正在使用 --amend 和 --date 开关。
确切的命令如下:
$ git commit --amend --date="YYYY-MM-DD HH:MM:SS"
Pre-Step.
Pull all data from the remote to the local repository.
we are using the --amend and --date switches.
The exact command is as follows:
$ git commit --amend --date="YYYY-MM-DD HH:MM:SS"
您始终可以更改计算机上的日期,进行提交,然后更改回日期并推送。
You can always change a date on your computer, make a commit, then change the date back and push.
我发现最适合我的是这个。
What I've found works best for me is this.