Git:还原my.gitignore中的文件

Git:还原my.gitignore中的文件,git,gitignore,Git,Gitignore,我想将Git中的文件还原为以前的提交。该文件被my.gitignore文件忽略。在我最近的一次提交中,我意外地将其强行提交 此问题回答了如何还原任何特定文件: 运行: git checkout 234b234234c123424 build/includes/initialize.php 错误: 错误:pathspec'build/includes/initialize.php'与git已知的任何文件都不匹配。 有什么想法吗 您必须指定错误的提交或错误的路径 $ git init Initial

我想将Git中的文件还原为以前的提交。该文件被my.gitignore文件忽略。在我最近的一次提交中,我意外地将其强行提交

此问题回答了如何还原任何特定文件:

运行:

git checkout 234b234234c123424 build/includes/initialize.php

错误:

错误:pathspec'build/includes/initialize.php'与git已知的任何文件都不匹配。


有什么想法吗

您必须指定错误的提交或错误的路径

$ git init Initialized empty Git repository in /.../.git/ $ echo test.txt > .gitignore $ git add .gitignore [master (root-commit) 6ed8815] Commit 1 1 file changed, 1 insertion(+) create mode 100644 .gitignore $ git commit -m "Commit 1" $ echo 'Version 1' > test.txt $ git add test.txt The following paths are ignored by one of your .gitignore files: test.txt Use -f if you really want to add them. fatal: no files added $ git add -f test.txt $ git commit -m "Commit 2" [master b23a130] Commit 2 1 file changed, 1 insertion(+) create mode 100644 test.txt $ echo 'Version 2' > test.txt $ git add test.txt $ git commit -m "Commit 3" [master 60c1f24] Commit 3 1 file changed, 1 insertion(+), 1 deletion(-) $ cat test.txt Version 2 $ git checkout b23a130 test.txt $ cat test.txt Version 1 $git init 已在/...Git中初始化空Git存储库/ $echo test.txt>.gitignore $git add.git忽略 [master(root提交)6ed8815]提交1 1个文件已更改,1个插入(+) 创建模式100644.gitignore $git提交-m“提交1” $echo'Version 1'>test.txt $git add test.txt 以下路径被一个.gitignore文件忽略: test.txt 如果确实要添加它们,请使用-f。 致命:未添加任何文件 $git add-f test.txt $git提交-m“提交2” [master b23a130]提交2 1个文件已更改,1个插入(+) 创建模式100644 test.txt $echo'Version 2'>test.txt $git add test.txt $git提交-m“提交3” [master 60c1f24]提交3 1个文件已更改,1个插入(+),1个删除(-) $cat test.txt 版本2 $git checkout b23a130 test.txt $cat test.txt 版本1 如果键入错误的提交,则会收到以下错误消息:

$ git checkout 6ed8815 test.txt error: pathspec 'test.txt' did not match any file(s) known to git. $git checkout 6ed8815 test.txt 错误:pathspec“test.txt”与git已知的任何文件都不匹配。 如果键入错误的路径名,我还会收到错误消息:

$ git checkout b23a130 other.txt error: pathspec 'other.txt' did not match any file(s) known to git. $git checkout b23a130 other.txt 错误:pathspec“other.txt”与git已知的任何文件都不匹配。 错误消息:

$ git checkout 6ed8815 test.txt error: pathspec 'test.txt' did not match any file(s) known to git.
错误:pathspec'build/includes/initialize.php'与git已知的任何文件都不匹配

表示您不在包含文件
build/includes/initialize.php的存储库中的正确目录中,或者该文件
build/includes/initialize.php
实际上在回购历史记录中的任何位置都不存在

您可以运行
git log--build/includes/initialize.php
来验证是否能够看到修改该文件的提交日志(即使该文件当前已被删除)。如果没有返回任何输出,它将确认您在错误的目录中


顺便说一句,一旦你向git添加了一个文件,git就会自动跟踪对该文件的更改,而不管该文件是否在
.gitignore
中列出。换句话说,后续添加不再需要指定
-f

是否尝试从.gitignore中删除该文件?是否确定位于git repository下的正确目录中?能否确认
git show 234b234234c123424
返回有效的输出?请注意,您需要为
git checkout
命令提供文件的相对路径(从当前位置)或绝对路径,并且您需要位于属于git存储库的目录中。如果忽略了该文件,它不是未被跟踪吗?如果您强制添加
它,我认为您应该只
git reset FILE
来恢复添加。我不知道您是否还想删除该文件。顺便说一句,即使文件在gitignore中,一旦您第一次添加并提交该文件(比如强制),git将自动跟踪对该文件的更改,而无需对后续添加执行
强制
。要展开Tuxdude的注释,请参阅该文件的“注释”部分。我需要从提交之前返回到
initialize.php
的版本。@DonnyP:那么您一定指定了错误的提交
git checkout
工作正常。有没有可能,因为这个文件已经被忽略了10次提交,所以我需要返回11次提交并在那里恢复?@DonnyP:首先,忽略并不像你想象的那样工作——它只允许你忽略没有签入的文件。不,如果文件在过去10次提交中,那么您可以从这10次提交中的任何一次中检出一份副本。我认为OP的提交哈希是有效的(至少有一些哈希对应于git在回购上下文中已知的提交),否则,
git
也会抱怨
错误:pathspec“MYHASH”与任何文件都不匹配git已知。