This is a card in Dave's Virtual Box of Cards.

Git - Checkout a remote branch

Page created: 2023-05-23

Back to git

Here’s something I can never remember how to do: check out a remote branch from somebody else’s repo. Specifically, I need to do this when somebody issues a pull request to one of my projects and I want to see it in action on my local machine first.

Well, here it is:

# clone the repo into a different dir
$ git clone https://example.com/clown/pooper.git clown-pooper

# create the local branch tracking the remote branch
$ git branch new-feature origin/new-feature

# switch to it
$ git checkout new-feature

Done!

Aside: Check out the bewildering ride at How do I check out a remote Git branch? (stackoverflow.com). This is usually the first thing that comes up when I’m done flailing around on the command line and do a low-effort search.

I love and completely agree with the highly-rated comment by my fellow traveler, dwanderson:

"I feel like I’m taking crazy pills. I’m trying to checkout a branch from an upstream, not just origin, and every recommended answer doesn’t do anything remotely helpful (pun-intended). EDIT - excuse me, the multitude of suggestions contained in the top 2 answers were useless; 3rd one (git branch test origin/test) is what works. Glad the top 2 have 20x the number of votes…​"

I agree with that 100% on all points.