How to Pull a Branch from Another User's Fork using Git

Published Feb 25, 2021

Suppose there is a repository called repo_name which both I and some_user have forked.

Generally, each time I make a change, I’ll pull from upstream, which is the project we want to contribute to as well as our source of truth. In this command, I pull master from upstream.

git pull upstream master

I’ll merge any conflicts that may appear and push my changes to origin, which is my own fork of the repository. It’s at this point when I’ll submit a pull or merge request.

git push origin some_branch

But what if I wanted to pull from a branch on another user’s fork of repo_name?

We can run through these terminal commands:

git remote add some_user git@github.com:some_user/repo_name.git
git fetch some_user
git checkout -b my_name_for_their_branch some_user/their_branch

The URI used in the first step above can be either an SSH-based URI or an HTTP URI. Either will work depending on how your system is set up.

  • SSH-based URI: git@github.com:some_user/repo_name.git
  • HTTP URI: https://github.com/some_user/repo_name.git

More CLI Articles