View or Extract a file from different git branch

View or Extract a file from different git branch

I have three different ways of getting files from other branches.

1) Checkout the file from the other branch

git checkout other-branch app/views/customers/index.html.erb

This brings a copy file from branch other-branch and place it in the staging area. I only do this if I know that I plan to commit this file on the current branch. Because it in the staging area, it could be accidentally committed if I’m not paying attention.

2) Show the file from the other branch

A safer approach is to view the file on the command line:

git show other-branch:app/views/customers/index.html.erb

3) Copy to temporary local file

If I need to open the file locally, I copy it to a local file with a different name. For me, it is usually a good idea to put it in the same directoy.

git show other-branch:app/views/customers/index.html.erb > app/views/customers/index-GOOD.html.erb

Note:

If all I need is to see is a difference between the two files, I do:

git diff other-branch app/views/customers/index.html.erb