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.