我如何在过去进行 Git 提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3895453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a Git commit in the past?
提问by unknown
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 the file's "date modified" so I have an accurate history of the file?
我正在将所有内容都转换为 Git 供我个人使用,并且我发现存储库中已经存在一些旧版本的文件。如何根据文件的“修改日期”以正确的顺序将其提交到历史记录中,以便获得准确的文件历史记录?
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
采纳答案by Chris Johnsen
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 commitinside --index-filter
.
你得到的建议是有缺陷的。无条件地设置 GIT_AUTHOR_DATE--env-filter
会重写每次提交的日期。此外,这将是不寻常的使用git的承诺中--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:
每个提交都有两个日期:作者日期和提交者日期。您可以通过环境变量 GIT_AUTHOR_DATE 和 GIT_COMMITTER_DATE 为写入新提交的任何命令提供值来覆盖每个。请参阅git-commit(1) 中的“日期格式”或以下内容:
Git internal format = <unix timestamp> <time zone offset>, e.g. 1112926393 +0200
RFC 2822 = e.g. Thu, 07 Apr 2005 22:13:13 +0200
ISO 8601 = e.g. 2005-04-07T22:13:13
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 includes git 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).
在正常使用期间写入新提交的唯一命令是git commit。它还有一个--date
选项可以让您直接指定作者日期。您预期的用法还包括git filter-branch --env-filter
使用上面提到的环境变量(这些是“env”的一部分,选项在其后命名;请参阅git-filter-branch(1) 中的“选项”和底层的“管道”命令git-commit -树(1)。
Inserting a File Into a Single refHistory
将文件插入单个引用历史记录
If your repository is very simple (i.e. you only have a single branch, no tags), then you can probably use git rebaseto do the work.
如果您的存储库非常简单(即您只有一个分支,没有标签),那么您可能可以使用git rebase来完成这项工作。
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.
在以下命令中,使用提交的对象名称(SHA-1 哈希)而不是“A”。运行git commit时不要忘记使用“日期覆盖”方法之一。
---A---B---C---o---o---o master
git checkout master
git checkout A~0
git add path/to/file
git commit --date='whenever'
git tag ,new-commit -m'delete me later'
git checkout -
git rebase --onto ,new-commit A
git tag -d ,new-commit
---A---N (was ",new-commit", but we delete the tag)
\
B'---C'---o---o---o master
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 of git commit
. The result would look like this:
如果你想更新 A 以包含新文件(而不是在添加它的地方创建一个新的提交),那么使用git commit --amend
而不是git commit
. 结果如下所示:
---A'---B'---C'---o---o---o master
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:
只要您可以命名应该是新提交的父级的提交,上述内容就可以工作。如果您确实希望通过新的根提交(没有父提交)添加新文件,那么您需要一些不同的东西:
B---C---o---o---o master
git checkout master
git checkout --orphan new-root
git rm -rf .
git add path/to/file
GIT_AUTHOR_DATE='whenever' git commit
git checkout -
git rebase --root --onto new-root
git branch -d new-root
N (was new-root, but we deleted it)
\
B'---C'---o---o---o master
git checkout --orphan
is relatively new (Git 1.7.2), but there are other ways of doing the same thingthat work on older versions of Git.
git checkout --orphan
相对较新(Git 1.7.2),但还有其他方法可以在旧版本的 Git 上做同样的事情。
Inserting a File Into a Multi-refHistory
将文件插入多参考历史记录
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 tararchive of your entire working tree (including the .git directory) is sufficient. git filter-branchdoes 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.
如果您的存储库更复杂(即它有多个引用(分支、标签等)),那么您可能需要使用git filter-branch。在使用git filter-branch之前,您应该制作整个存储库的备份副本。整个工作树(包括 .git 目录)的简单tar存档就足够了。git filter-branch确实创建了备份引用,但是通过删除.git
目录并从备份中恢复它通常更容易从不太正确的过滤中恢复。
Note: The examples below use the lower-level command git update-index --add
instead of git 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).
注意:下面的示例使用较低级别的命令git update-index --add
而不是git add
. 您可以使用git add,但首先需要将文件从某个外部位置复制到预期路径(--index-filter
在空的临时 GIT_WORK_TREE 中运行其命令)。
If you want your new file to be added to every existing commit, then you can do this:
如果您希望将新文件添加到每个现有提交中,则可以执行以下操作:
new_file=$(git hash-object -w path/to/file)
git filter-branch \
--index-filter \
'git update-index --add --cacheinfo 100644 '"$new_file"' path/to/file' \
--tag-name-filter cat \
-- --all
git reset --hard
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.
我真的看不出有任何理由使用--env-filter 'GIT_AUTHOR_DATE=…'
. 如果您确实使用了它,您将使其成为有条件的,以便它为每次提交重写日期。
If you want your new file to appear only in the commits after some existing commit (“A”), then you can do this:
如果你希望你的新文件只出现在一些现有提交(“A”)之后的提交中,那么你可以这样做:
file_path=path/to/file
before_commit=$(git rev-parse --verify A)
file_blob=$(git hash-object -w "$file_path")
git filter-branch \
--index-filter '
if x=$(git rev-list -1 "$GIT_COMMIT" --not '"$before_commit"') &&
test -n "$x"; then
git update-index --add --cacheinfo 100644 '"$file_blob $file_path"'
fi
' \
--tag-name-filter cat \
-- --all
git reset --hard
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-branchand add --parent-filter
to git filter-branch:
如果您希望通过将插入到历史记录中间的新提交来添加文件,那么您需要在使用git filter-branch之前生成新提交并添加--parent-filter
到git filter-branch:
file_path=path/to/file
before_commit=$(git rev-parse --verify A)
git checkout master
git checkout "$before_commit"
git add "$file_path"
git commit --date='whenever'
new_commit=$(git rev-parse --verify HEAD)
file_blob=$(git rev-parse --verify HEAD:"$file_path")
git checkout -
git filter-branch \
--parent-filter "sed -e s/$before_commit/$new_commit/g" \
--index-filter '
if x=$(git rev-list -1 "$GIT_COMMIT" --not '"$new_commit"') &&
test -n "$x"; then
git update-index --add --cacheinfo 100644 '"$file_blob $file_path"'
fi
' \
--tag-name-filter cat \
-- --all
git reset --hard
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 rebasesection (capture it in new_commit
), use the?unconditional --index-filter
, and a --parent-filter
like "sed -e \"s/^$/-p $new_commit/\""
.
你也可以安排文件是在一个新的根先加入承诺:创建新的根通过从“赵氏孤儿”的方法提交git的变基节(捕获它new_commit
),使用无条件的?--index-filter
和--parent-filter
喜欢"sed -e \"s/^$/-p $new_commit/\""
。
回答by mipadi
You can create the commit as usual, but when you commit, set the environment variables GIT_AUTHOR_DATE
and GIT_COMMITTER_DATE
to the appropriate datetimes.
您可以创建提交像往常一样,但是当你提交,设置环境变量GIT_AUTHOR_DATE
,并GIT_COMMITTER_DATE
以适当的日期时间。
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:
当然,这将使提交在您的分支的顶端(即,在当前 HEAD 提交之前)。如果你想在 repo 中把它推回更远的地方,你必须有点花哨。假设你有这样的历史:
o--o--o--o--o
And you want your new commit (marked as "X") to appear second:
并且您希望您的新提交(标记为“X”)出现在第二个:
o--X--o--o--o--o
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 checkout -b new_commit $desired_parent_of_new_commit
$ git add new_file
$ GIT_AUTHOR_DATE='your date' GIT_COMMITTER_DATE='your date' git commit -m 'new (old) files'
$ git checkout master
$ git rebase new_commit
$ git branch -d new_commit
回答by Hyder B.
I know this question is quite old, but that's what actually worked for me:
我知道这个问题已经很老了,但这实际上对我有用:
git commit --date="10 day ago" -m "Your commit message"
回答by skierpage
In my case over time I had saved a bunch of versions of myfileas 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
, then git commit --date=(modification date from ls -l) myfile
, rename next oldest to myfile, another git commit with --date, repeat...
在我的情况下,随着时间的推移,我已经将myfile的一堆版本保存为 myfile_bak、myfile_old、myfile_2010、backups/myfile 等。我想使用它们的修改日期将 myfile 的历史记录放在 git 中。因此,将最旧的重命名为 myfile, git add myfile
然后git commit --date=(modification date from ls -l) myfile
,将次旧的重命名为 myfile,另一个 git commit 与 --date,重复...
To automate this somewhat, you can use shell-foo to get the modification time of the file. I started with ls -l
and cut
, but stat(1) is more direct
为了在某种程度上自动化,您可以使用 shell-foo 来获取文件的修改时间。我从ls -l
and开始cut
,但 stat(1) 更直接
git commit --date="`stat -c %y myfile`" myfile
回答by Vilson Vieira
The following is what I use to commit changes on foo
to N=1
days in the past:
下面是我使用的更改提交foo
到N=1
在过去的日子:
git add foo
git commit -m "Update foo"
git commit --amend --date="$(date -v-1d)"
If you want to commit to a even older date, say 3 days back, just change the date
argument: date -v-3d
.
如果你想提交一个更早的日期,说 3 天前,只需更改date
参数: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:
UPDATE:--date
也接受像--date "3 days ago"
or even这样的表达式--date "yesterday"
。所以我们可以将其缩减为一行命令:
git add foo ; git commit --date "yesterday" -m "Update"
回答by JstRoRR
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.
就我而言,在使用 --date 选项时,我的 git 进程崩溃了。可能是我做了什么可怕的事情。结果出现了一些 index.lock 文件。所以我手动从 .git 文件夹中删除了 .lock 文件并执行,所有修改过的文件都在传递的日期提交,这次它起作用了。感谢这里的所有答案。
git commit --date="`date --date='2 day ago'`" -am "update"
回答by mx0
To make a commit that looks like it was done in the past you have to set both GIT_AUTHOR_DATE
and GIT_COMMITTER_DATE
:
要进行看起来像过去完成的提交,您必须同时设置GIT_AUTHOR_DATE
和GIT_COMMITTER_DATE
:
GIT_AUTHOR_DATE=$(date -d'...') GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" git commit -m '...'
where date -d'...'
can be exact date like 2019-01-01 12:00:00
or relative like 5 months ago 24 days ago
.
wheredate -d'...'
可以是确切日期 like2019-01-01 12:00:00
或 relative like 5 months ago 24 days ago
。
To see both dates in git log use:
要在 git log 中查看两个日期,请使用:
git log --pretty=fuller
This also works for merge commits:
这也适用于合并提交:
GIT_AUTHOR_DATE=$(date -d'...') GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" git merge <branchname> --no-ff
回答by rel1x
Or just use a fake-git-historyto generate it for a specific data range.
或者只是使用fake-git-history为特定的数据范围生成它。
回答by Nik
You can always change a date on your computer, make a commit, then change the date back and push.
您可以随时更改计算机上的日期,进行提交,然后将日期改回并推送。