How can I get a specific file from a branch in Git?

To get a specific file from a (different) branch in Git, you can use the command:

git checkout my_branch -- somefile.c

This command allows you to retrieve the file named somefile.c from the branch my_branch in Git. It will replace the version of somefile.c in your current working directory with the version from my_branch. Make sure to replace my_branch with the actual name of the branch you want to get the file from, and the same for somefile.c.

This also works with submodules. So for example imagine you have a git submodule in the folder called /public/assets, and you realised you have commited it by mistake. You know the correct version is on the branch called preproduction. You can retrieve that version by typing:

git checkout preproduction -- /public/assets
Social
Made by kodaps · All rights reserved.
© 2024