Git

How to Fetch Only One Branch of a Remote Git Repository?

The “$ git fetch” command is most commonly used among developers for downloading the latest version of the remote repository. After doing so, all the existing content of the GitHub remote repository is downloaded into the local repository as a copy of it but not merged with local repository content. Moreover, developers can fetch the specific remote branch if they don’t want to get the whole remote repository.

This post will provide the process of fetching the particular remote Git repository branch.

How do Fetch Only One Branch of a Remote Git Repository?

Try out the below-given instructions if you want to fetch the particular remote branch of a Git remote repository:

  • Move to the Git local repository.
  • Set the new remote URL for tracking.
  • Check the added remote URL by checking its list.

Run the “$ git fetch <remote-name> <remote-branch-name>” command and check the all branches list.

Let’s move ahead and check the demonstration of the above-discussed scenario!

Step 1: Navigate to Git Repository
Go to the desired local repository by executing the “cd” command:

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

Step 2: Add Remote URLs
Next, use the “git remote add” command along with the remote name and remote repository URL for tracking changes:

$ git remote add origin https://github.com/GitUser0422/demo5.git

Step 3: Verify Remote URLs List
Now, check the newly added remote URL in Git by running the following command:

$ git remote -v

It can be seen that the remote URL has been added successfully:

Step 4: Fetch Particular Remote Branch
Finally, execute the “git fetch” command with the remote name and the desired remote branch name:

$ git fetch origin master

Here, we have specified the remote branch name as “master”:

Step 5: Verify Fetch Remote Branch
Lastly, run the “git branch” command along with the “-a” flag to list all branches including the local and remote:

$ git branch -a

As you can see the particular remote branch has been fetched successfully:

That was all about fetching the particular remote Git repository branch.

Conclusion

To fetch the particular remote branch of a Git remote repository, first, move to the Git repository and add the remote URL for tracking. After that, verify the added remote URL by checking its list. Next, execute the “$ git fetch <remote-name> <remote-branch-name>” command and check the all branches list. This post illustrated the process of fetching the particular remote Git repository 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.