git,

Git: Recovering files from previous commit or branch

Oct 08, 2022 · 1 min read · Post a comment

As I continue on my Git journey, today I want to show you how to recover files from some old commits or branches. This would be closely related to Restore deleted files in Git.

Prerequisites

  • Git

Solution(s)

Recovering files from an old commit

Step 1. First identify the commit you want to restore the file from, by listing all git commits or filter them by commit message. For instance:

git reflog
or
git reflog --grep "<some_commit_message_you_want_to_recover_file_from>"

Step 2. Copy the commit ID and restore a single file or multiple from the same commit:

git checkout <commit-SHA1> -- <absolute_path_filename_1> <absolute_path_filename_2>

Recovering files from a branch

Step 1. Much easier task to do if you know which branch you need to recover from. For instance:

git checkout develop -- <absolute_path_filename_1>

Conclusion

To find more neat Git commands and hacks, simply browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.

git