Skip to content

Git: get a file from another commit or branch

Normally, you would use git checkout to change branches or change to a previous commit. How do you git checkout a single file from another commit or branch?

git branch

git branch lists all the branches that you’ve checked out in your working directory. Let’s say that I have two branches:


Let’s say that I am on the master branch. But I wanted slothparadise.txt from the devel branch.

git checkout devel -- slothparadise.txt

You can also get a file from a previous commit. To see your previous commit messages, type:

git log

Every commit message has a hash value next to the commit. You can use a similar command to checkout a file from a previous commit. Look at the git log and find the commit with the hash value where you want to grab the file.

git checkout 8ae67 -- slothparadise.txt

You will still be on the master branch, but you have gotten the file from a different branch or commit! Very useful.