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:
Step 2: List Repository Content
To view the repository content list, run the following command:
Step 3: Create and Checkout Branch
To create and immediately switch to the new branch, run the “git checkout” command with the “-b” option:
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:
Step 5: Track File
Track the newly created file into the Git staging area:
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:
Step 7: Switch Branch
Next, run the “git switch” command and switch to the existing Git local branch:
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:
Step 9: Check Status
To ensure the copied file operation, run the “git status .” command:
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:
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.