Wieland Gmeiner
Wieland Gmeiner

Reputation: 57

Files lost in git

My development team recently switched from SVN to Git and I'm having some problems, e.g.:

I'm looking for a Java file that defines the package "com.acme.service.filter" (because maven says the package does not exist). A quick find tells me maven is right:

$ find . -name '*.java' -exec grep -Hn 'package.*com.acme.service.filter' '{}' \;
$

So I wanted to find out in which branch I could find such a file, after some reading I resorted to:

$ git grep 'package.*com.acme.service.filter' $(git rev-list --all)

which after some time really found a couple of occurences:

f1b183d1cc1ee43a16621a812ff0c924f4aa45d7:xxxxxxx/src/main/java/com/acme/service/filter/SomeFilter.java:package com.acme.service.filter;

and others that differ only in the hash.

The git command

$ git branch -a --contains f1b183d1cc1ee43a16621a812ff0c924f4aa45d7
* development
  remotes/origin/development

tells me that

1) The file is contained in the local branch 'development' and in the remote branch 'origin/development' 2) I have checked out locally the branch 'development' (the asterisk)

So why can't I find the file? Can somebody tell me what I'm missing?

Upvotes: 1

Views: 114

Answers (2)

das-g
das-g

Reputation: 9994

The hashes you got from git grep '<pattern>' $(git rev-list --all) are important. They're not the checksums of the found files. They are the IDs of the commits in which the named files match your pattern.

You then use such a commit ID as argument to git branch -a --contains. From the git branch documentation (emphasis mine):

[--contains <commit>] shows only the branches that contain the named commit (in other words, the branches whose tip commits are descendants of the named commit).

So what you get are branches that have commit f1b183d1cc1ee43a16621a812ff0c924f4aa45d7 in their history. That doesn't mean that they have all of f1b183d1cc1ee43a16621a812ff0c924f4aa45d7's files in their tips. (A branch's "tip" is its "last" commit: The one that doesn't have any descendants of its own, yet.)

If you want to recover the file in with the content it had in f1b183d1cc1ee43a16621a812ff0c924f4aa45d7, try:

git checkout f1b183d1cc1ee43a16621a812ff0c924f4aa45d7 -- xxxxxxx/src/main/java/com/acme/service/filter/SomeFilter.java

That will resurrect the file and add it to git's staging area (a.k.a. "the index").

Upvotes: 3

Dimitri Merejkowsky
Dimitri Merejkowsky

Reputation: 1060

matti is probably right.

You can do something like:

git log -- src/main/java/com/acme/service/filter/

to see only the commits that involve the given directory (even if the path no longer exist)

Upvotes: 1

Related Questions