0

I have a workflow pattern that crops up quite often and I wondered how to achieve this in git.

I have a file which has changed over time. I may have deleted a method or changed it in some way but I now realise some of the code could be useful.

How do I check the history of individual files and checkout and compare this previously committed file?

TommyD
  • 913
  • 3
  • 17
  • 32
  • 1
    Did you try a simple `git log -- `? Could be enough, depending on the complexity of what you're searching for. – Romain Valeri Dec 04 '18 at 11:16
  • Yes I can see the log, however I just want a quick look at that file at commit dea11dfddc6512c0c09eabf20622ac5f9df8c731 for example – TommyD Dec 04 '18 at 11:19
  • 1
    Oh ^^ So you could also do `git checkout dea11df -- `. Be sure to commit any pending changes to the file beforehand, though. Then if needed you'll get your file back in its actual state with a `git checkout -- ` afterwards. – Romain Valeri Dec 04 '18 at 11:23
  • https://stackoverflow.com/search?q=%5Bgit-log%5D+file – phd Dec 04 '18 at 15:19
  • Note that, technically, Git doesn't *have* "file history". It just has "history" which is really "all the commits". Each commit is a complete snapshot, so if you ask Git for "the history of file X", what Git does is walk the commit graph, backwards, one commit-pair at a time, comparing the two snapshots. If snapshot $OLDER vs snapshot $NEWER has *differences* in X, then Git proclaims that $NEWER "changed" X. – torek Dec 04 '18 at 16:54
  • Hence, rather than viewing the history of file X, you're viewing "the subset of all history in which, from $OLDER to $NEWER, X changed". Which seems like the same thing, but for `--follow`, it gets complicated. – torek Dec 04 '18 at 16:55

2 Answers2

3

If you want to look at file's simple history(i.e. which commit(s) changed the file):

git log (filename)

for example git log testdir/test.py


For a file's detailed history(including diff), add -p(abbreviation for --patch):

git log -p (filename)
git log --patch (filename)

For showing filechange in a commit:

git show (SHA Hash) (filename)
ik1ne
  • 1,041
  • 15
  • 20
0

You can do a git diff for a specific file: https://git-scm.com/docs/git-diff Although it is much easier to use some IDE (IntelliJ, Eclipse, etc.) or git GUI tools (gitk, SourceTree, Gitkraken) too do the diffing/comparing between file versions.

Hubidubi
  • 850
  • 4
  • 18
  • 34