How to get just one file from another branch?

 

To get just one file from another branch in Git, you can use the git checkout command followed by the path of the file you want to retrieve and the name of the source branch. Here's an example:

Let's say you have a branch named feature/new-feature and you want to get a file named important.txt from the master branch:

sh
# Checkout the 'important.txt' file from the 'master' branch git checkout master -- important.txt

In this command:

  • git checkout is used to update files in the working directory to match a specific version.
  • master is the source branch from which you want to get the file.
  • -- indicates the end of options and the start of file paths.
  • important.txt is the path to the file you want to retrieve.

After running this command, the important.txt file from the master branch will replace the version of the file in your current working directory.

Remember that this command only affects the file in your working directory and doesn't create a commit. If you intend to use the file in your current branch or commit it, you need to create a commit after checking out the file.

Comments