Git

How to Checkout/Update a Single File From Remote Origin Master?

While working on Git, developers often need to add changes to their projects. Sometimes, they encounter situations where all changes are not yet ready to be added to the remote repository. So, it is required to update only a single file from the Git remote repository. To do so, the “$ git checkout origin/<branch-name> — <file-name>” command can be used.

This guide will provide the procedure for updating a single file from the remote origin master.

How to Checkout/Update a Single File From Remote Origin Master?

To checkout/update a single file from the remote origin master, follow the below-given steps:

    • Move to the required Git repository.
    • Generate and add a file to the staging index.
    • Update remote repository.
    • Set the remote URL and verify it.
    • Push file content to the remote repository.
    • Download file content from the remote repository.
    • Execute the “$ git checkout origin/<branch-name> — <file-name>” command.

Let’s implement the above-discussed scenario!

Step 1: Move to Git Repository

Run the “cd” command and go to the desired Git repository:

$ cd "C:\Git\test_repo"

 

Step 2: Create a File

Create a new file by executing the “touch” command:

$ touch TestFile.txt

 

Step 3: Add File to Git Index

Next, run the “git add” command along with the file name, and move it to the Git staging area:

$ git add TestFile.txt

 

Step 4: Commit Changes

Save all the added changes and update the Git repository:

$ git commit -m "File is added"

 

Step 5: Set Remote URL

Next, execute the “git remote set-url” command along with the remote name and remote repository URL:

$ git remote set-url origin https://github.com/laibayounas/demo.git

 

Step 6: Check Remote URLs List

To verify whether the changes are added or not, execute the below-stated command:

$ git remote -v

 
It can be observed that the specified remote URL is added successfully:


Step 7: Upload File Content to Remote Repository

To update the Git commit changes, use the “git push” command. This command will send the modified source code files from the Git local repository to the GitHub hosting service repository, including all branches:

$ git push

 

Step 8: Download Content From Remote Repository

In order to get recent changes from the remote repository, download its content using the “git fetch” command:

$ git fetch

 

Note: Fetch command only downloads the recent changes without merging them.

Step 9: Checkout/Update File From Remote Origin Branch

To update single file content from the remote repository, utilize the given command along with the remote branch and file name:

$ git checkout origin/alpha -- TestFile.txt

 
As a result, the specified remote file is ready for modifications:


We have efficiently explained the procedure of updating a single file from the remote origin master.

Conclusion

To checkout/update a single file from the remote origin master, first, go to the required Git repository. Then, generate a text file, track it to the Git staging index, and update Git repository. After that, set the desired remote URL and push added local changes to the remote repository. Next, download file content from the remote Git repository. Finally, utilize the “$ git checkout origin/<branch-name> — <file-name>” command. This write-up explained the method of checkout/update of a single file from the remote origin master.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.