Git

How to Get Just One File From Another Branch?

While working on a Git local repository, developers might need to copy any specific file from one Git local branch to another Git local branch. Git offers several ways to do this task quickly, one of which is the “$ git checkout” command. In this command, you must specify the file name as an argument and the target Git local branch.

This article discusses the procedure to get a single Git file from another branch.

How to Get Single Git File From Another Branch?

To get the single file from another branch, first, navigate to the Git local repository and view the repository content list. Then, create a new Git branch and simultaneously switch to it. After that, create a new Git file in the repository and track it. Commit added changes and switch back to the previous branch. Lastly, execute the “$ git checkout <target-branch> — <file-name>” command.

Now move ahead and check out the implementation of the above-provided instructions!

Step 1: Navigate to Git Local Repository
Move to the Git local repository by utilizing the below-given command:

$ cd "C:\Users\nazma\Git\Demo18"

Step 2: List Repository Content
To view the repository content list, run the following command:

$ ls

Step 3: Create and Checkout Branch
To create and immediately switch to the new branch, run the “git checkout” command with the “-b” option:

$ git checkout -b dev

In the above-command, “dev” is the name of the branch which we want to create and switch to it:

Step 4: Create File
Next, execute the “touch” command to create a new file and specify its name:

$ touch file2.txt

Step 5: Track File
Track the newly created file into the Git staging area:

$ git add file2.txt

Step 6: Update Repository
Add changes to the repository and save it using the “git commit” command along with the “-m” option to add desired commit message:

$ git commit -m "2nd file added"

Step 7: Switch Branch
Next, run the “git switch” command and switch to the existing Git local branch:

$ git switch master

Step 8: Copy File From Another Branch
Now, run the “git checkout” command with the target branch and the file name to copy into the current branch:

$ git checkout dev -- file2.txt

Step 9: Check Status
To ensure the copied file operation, run the “git status .” command:

$ git status .

It can be observed that the “file2.txt” is successfully copied to the targeted branch from another branch:

Step 10: List Repository Content
Lastly, run the “ls” command to view the content list of the current branch:

$ ls

We have provided the method to get the single file from another branch.

Conclusion

To get one single file from another branch, first, navigate to the Git local repository and view the repository content list. Then, create a new Git branch and simultaneously switch to it. After that, create a new Git file in the repository and track it. Commit added changes and switch back to the previous branch. Then, execute the “$ git checkout <target-branch> — <file-name>” for fetching the required file. This article illustrated the procedure of getting a single file from another branch.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.