Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the git commit where directory was first created

Tags:

git

The solution that works for file does not work for directory. How do I know the first git commit that created a directory for the first time?

EDIT: As one of the comment points out that a directory appears in git only when a file under that directory is added to version control. In the context of this question, I don't know which file under that directory was added first.

like image 606
Holmes.Sherlock Avatar asked Feb 13 '18 04:02

Holmes.Sherlock


2 Answers

Best way I know to check a path in a lot of commits quickly is with git cat-file --batch-check. List your commits in reverse i.e. oldest first with the path you're interested in and feed that to the batch checker, it will complain if the path is missing but it does so to stdout, so you can print the first line with an absence of complaints:

git log --reverse --pretty=$'%H:path/to/folder\t%H' \
| git cat-file --batch-check='%(rest)' \
| awk 'NF==1 { print;exit; }'
like image 187
jthill Avatar answered Sep 24 '22 02:09

jthill


You can use git bisect to find when something changed in a certain way. It operates off the assumption that the repository was right in the past, but something happened recently to make it not-right.

For this example, "good" is the directory not existing, and "bad" is the directory existing.

First you find a commit where it's "bad":

$ ls -d MyDirectory
MyDirectory/
$ git bisect start
$ git bisect bad

Then you find a commit where it's "good"

$ git checkout HEAD~1000
$ ls -d MyDirectory
ls: cannot access 'MyDirectory': No such file or directory
$ git bisect good

Then you either repeatedly do manual bisect good/bad:

$ if [ -d MyDirectory ]; then git bisect bad; else git bisect good; fi

Or you use bisect run:

$ git bisect run [ ! -d MyDirectory ]

At the end it'll tell you the first "bad" commit.

like image 20
amphetamachine Avatar answered Sep 24 '22 02:09

amphetamachine