artagnon
artagnon

Reputation: 3709

Which file is older in history?

I want to compare two files in a git repository and tell which one was introduced into the repository earlier (by ancestry, not timestamp). The naive way to do this is to find the individual commits that introduced the files (rev-list HEAD -- <filename> | tail -n 1), and query whether there are commits between them (rev-list commit1 ^commit2). However, this is slow and stupid. What about bisecting the history until we find a tree that contains one file but not the other?

Upvotes: 1

Views: 60

Answers (3)

artagnon
artagnon

Reputation: 3709

Use diff-filter=A

git log --reverse --format=%H --name-status --diff-filter=A -- file1 file2 | grep ^A | cut -f2

For any number of files, you'll get the required ordering in one traversal.

Upvotes: 0

vonbrand
vonbrand

Reputation: 11791

Unless the files where introduced in the same linear branch (i.e., one in an ancestor of the other) there is no way of knowing. Say you introduce file A in your repo, and I introduce file B in mine. If later I clone from your repo, there is no way of knowing which came first. Sure, we could synchronize clocks, but that is outside of git. The only notion of "time" for git is the ancestorship relation, others are just not comparable.

Upvotes: 1

Highway of Life
Highway of Life

Reputation: 24331

Based on what I understand you're after, this may be what you're looking for:

git log --topo-order --reverse --oneline --name-only --pretty=format:"%Cgreen%ci %Cblue%cr %Cred%cn %Cgreen%h %Creset" <file1> <file2> | head -n10

--topo-order
Show no parents before all of its children are shown, and avoid showing commits on multiple lines of history intermixed.

--reverse
Output the commits in reverse order.

See: git-log for many more options and possible combinations.

Upvotes: 0

Related Questions