[git] How can I view an old version of a file with Git?

WAY 1: (I prefer this way, no ability to lose uncommitted data)

  1. Find commit id with: git reflog

  2. List files from commit git diff-tree --no-commit-id --name-only -r <commitHash>

    Example:

    git diff-tree --no-commit-id --name-only -r d2f9ba4
    d2f9ba4 is commit id from step 1.

  3. Open required file with following command:

    git show <commitHash>:/path/to/file

    Example:

    git show d2f9ba4:Src/Ext/MoreSwiftUI/ListCustom.swift
    Src/... is file path from step 2.


WAY 2: (Ability to lose uncommitted data)

  1. Find commit id with: git reflog

  2. Make hard reset to this commit: git reset --hard %commit ID%

    Example:

    git reset --hard c14809fa

  3. Make necessary changes and do a new commit into required branch