Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.4k points)

I have a Git repository and I'd like to see how some file looked a few months ago. I found the revision at that date, and it's 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8. I need to see what did one file look like and also save that to a file.

I managed to see the file using gitk, but it doesn't have an option to save it. I tried with command-line tools, the closest I got was:

git-show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8 my_file.txt

However, this command shows a diff and not the file contents. I know I can later use something like PAGER=cat and redirect output to a file, but I don't know how to get to the actual file content.

Basically, I'm looking for something like svn cat.

1 Answer

0 votes
by (27.5k points)

To replace or overwrite the content of a file in your current branch with the content of the file from a previous commit or a different branch, you can do so with these commands:

$ git checkout <commit-id-of-previous-commit> path/to/file.txt


$ git checkout <branchname> path/to/file.txt

For these changes to be effective in the current branch, you will then have to commit those changes.

Browse Categories