Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkout forked branch of repo without cloning fork

Tags:

git

github

I like to do github code reviews, by checking out the code locally in my IDE.

If someone forks a github repo and submits a pull request, is there a way for me to checkout their code without cloning their public forked repo?

my-repo - I usually just git checkout branch

my-repo-forked - Here I need to git clone my-repo-forked and then git checkout branch

like image 211
ClickThisNick Avatar asked Nov 15 '25 18:11

ClickThisNick


2 Answers

You may add the forked repository as a second remote to your local repo:

cd my-repo
git remote add forked-version [fork-url-here]
git fetch forked-version
git checkout [branch-name-here]

This way, you only have one local git for both your original repository and the fork.

More information about remotes can be found in the Pro Git Book: 2.5 - Git Basics - Working with Remotes.

like image 120
Mehdi Avatar answered Nov 18 '25 10:11

Mehdi


I believe this is the way:

git fetch origin pull/<PR_ID>/head:<BRANCH_NAME>
git switch BRANCH_NAME

Where you <BRANCH_NAME> can be any local branch name you want to give it.

Docs: GitHub.com Docs Checking out pull requests locally

like image 44
Melroy van den Berg Avatar answered Nov 18 '25 11:11

Melroy van den Berg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!