2013-03-15 66 views
1

我想将Git中的文件恢复到先前的提交。该文件被我的.gitignore文件忽略。在我最近的提交,我无意中迫使它在Git:恢复我的.gitignore中的文件

这个问题回答如何恢复任何特定的文件:Reset or revert a specific file to a specific revision using Git?

运行:

git checkout 234234234b234234234c123424 build/includes/initialize.php

错误:

error: pathspec 'build/includes/initialize.php' did not match any file(s) known to git.

有没有想法?

+0

您是否尝试从.gitignore中删除该文件? – 2013-03-15 01:11:07

+0

您确定您位于git repostitory下的正确目录中吗?你能确认'git show 234234234b234234234c123424'返回一个有效的输出吗?请注意,您需要将文件的相对位置(从当前位置)或文件的绝对路径提供给'git checkout'命令,并且您需要位于属于git存储库的目录中。 – Tuxdude 2013-03-15 01:21:35

+0

如果该文件被忽略,是不是未跟踪?如果你强制'添加'它,我认为你应该'git重置FILE'来恢复添加。我不知道你是否也想删除文件。 – mgarciaisaia 2013-03-15 01:28:05

回答

1

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

 
$ 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 checkout 6ed8815 test.txt 
error: pathspec 'test.txt' did not match any file(s) known to git. 

我也得到了错误消息,如果我输入了错误的路径:

 
$ git checkout b23a130 other.txt 
error: pathspec 'other.txt' did not match any file(s) known to git. 
+0

该文件已经存在。我需要从提交之前返回到'initialize.php'版本。 – 2013-03-15 01:16:19

+0

@DonnyP:然后你必须指定错误的提交。 'git checkout'工作正常。 – 2013-03-15 01:19:33

+0

是否有可能,因为这个文件被忽略了10次提交,我需要返回11次提交并恢复? – 2013-03-15 01:36:33

0

错误消息:

error: pathspec 'build/includes/initialize.php' did not match any file(s) known to git

意味着您要么不在包含文件build/includes/initialize.php的存储库中的正确目录中,要么文件build/includes/initialize.php在回购的历史记录中的任何地方都不存在。

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

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