3

I want to revert a file in Git to a previous commit. The file is ignored by my .gitignore file. In my most recent commit, I accidentally forced it in.

This question answers how to revert any specific file: Reset or revert a specific file to a specific revision using Git?

Running:

git checkout 234234234b234234234c123424 build/includes/initialize.php

Error:

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

Any ideas?

Community
  • 1
  • 1
Don P
  • 60,113
  • 114
  • 300
  • 432
  • Have you tried removing the file from your .gitignore? – Code-Apprentice Mar 15 '13 at 01:11
  • Are you sure you're in the correct directory under the git repostitory ? Can you confirm that `git show 234234234b234234234c123424` returns a valid output ? Note that you need to give the relative (from your current location) or absolute path of the file to the `git checkout` command and you need to be in a directory which belongs to the git repository. – Tuxdude Mar 15 '13 at 01:21
  • If the file was ignored, wasn't it untracked? If you force `add` it, I think you should just `git reset FILE` for reverting the add. I don't know if you want to also delete the file or not. – mgarciaisaia Mar 15 '13 at 01:28
  • 1
    BTW even if a file is in gitignore, once you add and commit the file (say forcibly) for the first time, git will automatically track changes to the file without doing a `force` for subsequent adds. – Tuxdude Mar 15 '13 at 01:31
  • To expand on Tuxdude's comment, see the NOTES section of https://www.kernel.org/pub/software/scm/git/docs/gitignore.html – Dietrich Epp Mar 15 '13 at 01:41

2 Answers2

2

You must be specifying the wrong commit or the wrong path.

$ 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

If I type in the wrong commit, I get this error message:

$ git checkout 6ed8815 test.txt
error: pathspec 'test.txt' did not match any file(s) known to git.

I also get the error message if I type the wrong pathname:

$ git checkout b23a130 other.txt
error: pathspec 'other.txt' did not match any file(s) known to git.
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • The file did exist already. I need to go back to the version of `initialize.php` from before the commit. – Don P Mar 15 '13 at 01:16
  • @DonnyP: You must be specifying the wrong commit then. `git checkout` works fine. – Dietrich Epp Mar 15 '13 at 01:19
  • Is it possible, that since this file has been ignored for say 10 commits, that I need to go back 11 commits and revert there? – Don P Mar 15 '13 at 01:36
  • @DonnyP: Well, first of all, ignoring doesn't work the way you think it does--it only allows you to ignore files that aren't checked in. And no, if the file has been in the past 10 commits, then you can check out a copy from any one of those 10 commits. – Dietrich Epp Mar 15 '13 at 01:39
  • I think the OP's commit hash is valid (at least some hash corresponding to a commit known by git in the context of the repo), otherwise `git` would also complain `error: pathspec 'MYHASH' did not match any file(s) known to git.` – Tuxdude Mar 15 '13 at 01:40
  • @Tuxdude: What I meant by "wrong commit" is "valid commit, but not the one you want." – Dietrich Epp Mar 15 '13 at 01:42
0

The error message:

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

implies you're either not in the correct directory within the repository which contains the file build/includes/initialize.php or that file build/includes/initialize.php actually does not exist anywhere in the history of the repo.

You can run git log -- build/includes/initialize.php to verify you're able to see commit logs where this file was modified (even if the file is currently deleted). If this returns no output, it confirms that you're in the wrong directory.

BTW, once you add a file to git, git automatically tracks changes to the file irrespective of the file being listed in the .gitignore or not. In other words, subsequent adds does not require specifying the -f anymore.

Tuxdude
  • 47,485
  • 15
  • 109
  • 110